Key agreement as a service (Effect's effect/crypto/KeyAgreement)::
agreement = yield from KeyAgreement()
pair = yield from agreement.generate_key_pair(x25519)
secret = yield from agreement.derive_shared_secret(pair.private_key, peer_public_key)X25519 and ECDH on P-256 / P-384 / P-521. A peer key off the curve, of
another algorithm, or a low-order X25519 key giving an all-zero secret
fails with InvalidKey. Feed the shared secret to kdf before using
it as a key.
Re-exports#
Values#
| Attribute | Type | Description |
|---|---|---|
x25519 | X25519 | |
ecdh_p256 | Ecdh[Literal['P-256']] | |
ecdh_p384 | Ecdh[Literal['P-384']] | |
ecdh_p521 | Ecdh[Literal['P-521']] | |
family | Family | |
layer_web_crypto | Layer[KeyAgreement, Never, SecureRandom] | Key agreement on the |
Types#
| Type | Definition | Description |
|---|---|---|
Algorithm | type Algorithm = X25519 | Ecdh[Any] |
Classes#
Functions#
is_private_key#
is_private_key(u) -> bool| Parameter | Type | Description |
|---|---|---|
u | object |
Returns bool
def is_private_key(u: object) -> bool:
return isinstance(u, PrivateKey)is_public_key#
is_public_key(u) -> bool| Parameter | Type | Description |
|---|---|---|
u | object |
Returns bool
def is_public_key(u: object) -> bool:
return isinstance(u, PublicKey)private_key#
private_key[A: X25519](algorithm, data) -> Effect[PrivateKey[A], CryptoError]An X25519 private key from its 32 bytes.
| Parameter | Type | Description |
|---|---|---|
algorithm | A | |
data | Binary |
Returns Effect[PrivateKey[A], CryptoError]
def private_key[A: X25519](algorithm: A, data: Binary) -> Effect[PrivateKey[A], CryptoError]:
"""An X25519 private key from its 32 bytes."""
return _key.from_bytes(
lambda copied: PrivateKey(algorithm, copied),
"KeyAgreement",
"privateKey",
data,
_asymmetric.check_seed_length("X25519", "private"),
)public_key#
public_key[A: Algorithm](algorithm, data) -> Effect[PublicKey[A], CryptoError]An X25519 public key (32 bytes) or an uncompressed EC point.
| Parameter | Type | Description |
|---|---|---|
algorithm | A | |
data | Binary |
Returns Effect[PublicKey[A], CryptoError]
def public_key[A: Algorithm](algorithm: A, data: Binary) -> Effect[PublicKey[A], CryptoError]:
"""An X25519 public key (32 bytes) or an uncompressed EC point."""
check = (
_asymmetric.check_point(algorithm.named_curve)
if isinstance(algorithm, Ecdh)
else _asymmetric.check_seed_length("X25519", "public")
)
return _key.from_bytes(
lambda copied: PublicKey(algorithm, copied), "KeyAgreement", "publicKey", data, check
)private_key_from_crypto_key#
private_key_from_crypto_key[A: Algorithm](algorithm, crypto_key) -> Effect[PrivateKey[A], CryptoError]| Parameter | Type | Description |
|---|---|---|
algorithm | A | |
crypto_key | CryptoKey |
Returns Effect[PrivateKey[A], CryptoError]
def private_key_from_crypto_key[A: Algorithm](
algorithm: A, crypto_key: CryptoKey
) -> Effect[PrivateKey[A], CryptoError]:
return _asymmetric.from_crypto_key(
family, "privateKeyFromCryptoKey", algorithm, "private", crypto_key
)public_key_from_crypto_key#
public_key_from_crypto_key[A: Algorithm](algorithm, crypto_key) -> Effect[PublicKey[A], CryptoError]| Parameter | Type | Description |
|---|---|---|
algorithm | A | |
crypto_key | CryptoKey |
Returns Effect[PublicKey[A], CryptoError]
def public_key_from_crypto_key[A: Algorithm](
algorithm: A, crypto_key: CryptoKey
) -> Effect[PublicKey[A], CryptoError]:
return _asymmetric.from_crypto_key(
family, "publicKeyFromCryptoKey", algorithm, "public", crypto_key
)to_bytes#
to_bytes(key) -> Effect[bytes, CryptoError]| Parameter | Type | Description |
|---|---|---|
key | PrivateKey[Any] | PublicKey[Any] |
Returns Effect[bytes, CryptoError]
def to_bytes(key: PrivateKey[Any] | PublicKey[Any]) -> Effect[bytes, CryptoError]:
return _asymmetric.to_bytes(family, key)