GitHub

signature

API reference for signature.

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

AttributeTypeDescription
ed25519Ed25519
ecdsa_p256Ecdsa[Literal['P-256']]
ecdsa_p384Ecdsa[Literal['P-384']]
ecdsa_p521Ecdsa[Literal['P-521']]
rsa_pss_sha256RsaPss
rsa_pss_sha384RsaPss
rsa_pss_sha512RsaPss
rsa_pkcs1v15_sha256RsaPkcs1v15
rsa_pkcs1v15_sha384RsaPkcs1v15
rsa_pkcs1v15_sha512RsaPkcs1v15
familyFamily
layer_web_cryptoLayer[Signature, Never, SecureRandom]

Signatures on the WebCrypto reference; Ed25519 seeds from SecureRandom.

Types

TypeDefinitionDescription
Algorithmtype Algorithm = Ed25519 | Ecdsa[Any] | RsaPss | RsaPkcs1v15
Curvetype Curve = Literal['P-256', 'P-384', 'P-521']

Classes

Functions

rsa_pss

rsa_pss(*, hash, modulus_length, salt_length=None) -> RsaPss

RSA-PSS; the salt is the hash's length unless given.

ParameterTypeDescription
hashDigestAlgorithm
modulus_lengthint
salt_lengthint | 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
ParameterTypeDescription
hashDigestAlgorithm
modulus_lengthint

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
ParameterTypeDescription
uobject

Returns bool

def is_private_key(u: object) -> bool:
    return isinstance(u, PrivateKey)

is_public_key

is_public_key(u) -> bool
ParameterTypeDescription
uobject

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).

ParameterTypeDescription
algorithmA
dataBinary

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.

ParameterTypeDescription
algorithmA
dataBinary

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.

ParameterTypeDescription
algorithmA
crypto_keyCryptoKey

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.

ParameterTypeDescription
algorithmA
crypto_keyCryptoKey

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.

ParameterTypeDescription
keyPrivateKey[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)