Digital signatures as a service (Effect's effect/crypto/Signature)::
signature = yield from Signature()
pair = yield from signature.generate_key_pair(ed25519)
signed = yield from signature.sign(pair.private_key, data)
ok = yield from signature.verify(pair.public_key, signed, data)Ed25519, ECDSA on P-256 / P-384 / P-521 (the hash bound to the curve,
signatures in raw r‖s form) and RSA-PSS / RSASSA-PKCS1-v1_5.
verify is False for any problem with the signature bytes. Keys are
stored as an Ed25519 seed or raw public key, an uncompressed EC point or
EC PKCS#8, or RSA SPKI / PKCS#8; key_format reads and writes the
other forms.
Re-exports#
Values#
| Attribute | Type | Description |
|---|---|---|
ed25519 | Ed25519 | |
ecdsa_p256 | Ecdsa[Literal['P-256']] | |
ecdsa_p384 | Ecdsa[Literal['P-384']] | |
ecdsa_p521 | Ecdsa[Literal['P-521']] | |
rsa_pss_sha256 | RsaPss | |
rsa_pss_sha384 | RsaPss | |
rsa_pss_sha512 | RsaPss | |
rsa_pkcs1v15_sha256 | RsaPkcs1v15 | |
rsa_pkcs1v15_sha384 | RsaPkcs1v15 | |
rsa_pkcs1v15_sha512 | RsaPkcs1v15 | |
family | Family | |
layer_web_crypto | Layer[Signature, Never, SecureRandom] | Signatures on the |
Types#
| Type | Definition | Description |
|---|---|---|
Algorithm | type Algorithm = Ed25519 | Ecdsa[Any] | RsaPss | RsaPkcs1v15 | |
Curve | type Curve = Literal['P-256', 'P-384', 'P-521'] |
Classes#
Functions#
rsa_pss#
rsa_pss(*, hash, modulus_length, salt_length=None) -> RsaPssRSA-PSS; the salt is the hash's length unless given.
| Parameter | Type | Description |
|---|---|---|
hash | DigestAlgorithm | |
modulus_length | int | |
salt_length | int | None = None |
Returns RsaPss
def rsa_pss(
*, hash: DigestAlgorithm, modulus_length: int, salt_length: int | None = None
) -> RsaPss:
"""RSA-PSS; the salt is the hash's length unless given."""
return RsaPss(
name=f"rsaPss{name_suffix(hash)}",
hash=hash,
modulus_length=modulus_length,
salt_length=hash.output_length if salt_length is None else salt_length,
)rsa_pkcs1v15#
rsa_pkcs1v15(*, hash, modulus_length) -> RsaPkcs1v15| Parameter | Type | Description |
|---|---|---|
hash | DigestAlgorithm | |
modulus_length | int |
Returns RsaPkcs1v15
def rsa_pkcs1v15(*, hash: DigestAlgorithm, modulus_length: int) -> RsaPkcs1v15:
return RsaPkcs1v15(
name=f"rsaPkcs1v15{name_suffix(hash)}", hash=hash, modulus_length=modulus_length
)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: Ed25519](algorithm, data) -> Effect[PrivateKey[A], CryptoError]An Ed25519 private key from its 32-byte seed (not libsodium's
64-byte seed‖public key form: pass its first 32 bytes).
| Parameter | Type | Description |
|---|---|---|
algorithm | A | |
data | Binary |
Returns Effect[PrivateKey[A], CryptoError]
def private_key[A: Ed25519](algorithm: A, data: Binary) -> Effect[PrivateKey[A], CryptoError]:
"""An Ed25519 private key from its 32-byte seed (not libsodium's
64-byte ``seed‖public key`` form: pass its first 32 bytes)."""
return _key.from_bytes(
lambda copied: PrivateKey(algorithm, copied),
"Signature",
"privateKey",
data,
_asymmetric.check_seed_length("Ed25519", "private"),
)public_key#
public_key[A: Ed25519 | Ecdsa[Any]](algorithm, data) -> Effect[PublicKey[A], CryptoError]An Ed25519 public key (32 bytes) or an uncompressed EC point. A point off the curve is rejected when the key is used.
| Parameter | Type | Description |
|---|---|---|
algorithm | A | |
data | Binary |
Returns Effect[PublicKey[A], CryptoError]
def public_key[A: Ed25519 | Ecdsa[Any]](
algorithm: A, data: Binary
) -> Effect[PublicKey[A], CryptoError]:
"""An Ed25519 public key (32 bytes) or an uncompressed EC point. A point
off the curve is rejected when the key is used."""
check = (
_asymmetric.check_point(algorithm.named_curve)
if isinstance(algorithm, Ecdsa)
else _asymmetric.check_seed_length("Ed25519", "public")
)
return _key.from_bytes(
lambda copied: PublicKey(algorithm, copied), "Signature", "publicKey", data, check
)private_key_from_crypto_key#
private_key_from_crypto_key[A: Algorithm](algorithm, crypto_key) -> Effect[PrivateKey[A], CryptoError]A private key backed by a CryptoKey of the algorithm (and its
curve or hash) with the sign usage.
| 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]:
"""A private key backed by a ``CryptoKey`` of the algorithm (and its
curve or hash) with the ``sign`` usage."""
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]A public key backed by a CryptoKey with the verify usage.
| 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]:
"""A public key backed by a ``CryptoKey`` with the ``verify`` usage."""
return _asymmetric.from_crypto_key(
family, "publicKeyFromCryptoKey", algorithm, "public", crypto_key
)to_bytes#
to_bytes(key) -> Effect[bytes, CryptoError]The key's stored form; NotExtractable for a non-extractable
CryptoKey.
| Parameter | Type | Description |
|---|---|---|
key | PrivateKey[Any] | PublicKey[Any] |
Returns Effect[bytes, CryptoError]
def to_bytes(key: PrivateKey[Any] | PublicKey[Any]) -> Effect[bytes, CryptoError]:
"""The key's stored form; ``NotExtractable`` for a non-extractable
``CryptoKey``."""
return _asymmetric.to_bytes(family, key)