GitHub

API reference for schema.

Schemas of crypto keys (Effect's Schema.MacKey, AeadKey, SignaturePublicKey, ... and their FromBytes / FromFormat codecs). They live here, not in pyfect.schema, because pyfect.crypto is built on pyfect.schema::

from pyfect.crypto import mac
from pyfect.crypto import schema as CS

key_config = config.schema(CS.MacKeyFromBytes(mac.hmac_sha256), "HMAC_KEY")

A declaration (CS.MacKey(algorithm)) accepts live keys of an algorithm with the same operation parameters (name, hash, salt length; an RSA modulus length only matters when generating). Keys have no JSON form, no strategy, display redacted and compare by identity.

...FromBytes decodes raw key bytes synchronously; ...FromFormat decodes with key_format and is asynchronous only, so the synchronous decoders refuse it. A CryptoError becomes an InvalidValue issue whose cause is the error. Secret and private keys are written back only with allow_encode=True (and never to the read-only PKCS#1 and SEC1).

Functions

MacKey

MacKey[A: mac.Algorithm](algorithm) -> Bottom[mac.Key[A]]

Live mac.Key values of algorithm.

ParameterTypeDescription
algorithmA

Returns Bottom[mac.Key[A]]

def MacKey[A: mac.Algorithm](algorithm: A) -> Bottom[mac.Key[A]]:
    """Live ``mac.Key`` values of ``algorithm``."""
    return _declare("MacKey", "Mac", "Key", mac.Key, algorithm)

MacKeyFromBytes

MacKeyFromBytes[A: mac.Algorithm](algorithm, *, allow_encode=False) -> DecodeToSchema[mac.Key[A], bytes]

Raw key bytes (base64 under config.schema) as a mac.Key.

ParameterTypeDescription
algorithmA
allow_encodebool = False

Returns DecodeToSchema[mac.Key[A], bytes]

def MacKeyFromBytes[A: mac.Algorithm](
    algorithm: A, *, allow_encode: bool = False
) -> DecodeToSchema[mac.Key[A], bytes]:
    """Raw key bytes (base64 under ``config.schema``) as a ``mac.Key``."""
    return _from_bytes(
        "MacKeyFromBytes",
        MacKey(algorithm),
        lambda data: mac.key(algorithm, data),
        mac.to_bytes if allow_encode else None,
    )

MacKeyFromFormat

MacKeyFromFormat[A: mac.Algorithm, Data](format, algorithm, *, allow_encode=False) -> DecodeToSchema[mac.Key[A], Data]

A mac.Key read with key_format.decode_secret.

ParameterTypeDescription
formatkey_format.Format[Literal['secret'], _Decode, Data]
algorithmA
allow_encodebool = False

Returns DecodeToSchema[mac.Key[A], Data]

def MacKeyFromFormat[A: mac.Algorithm, Data](
    format: key_format.Format[Literal["secret"], _Decode, Data],
    algorithm: A,
    *,
    allow_encode: bool = False,
) -> DecodeToSchema[mac.Key[A], Data]:
    """A ``mac.Key`` read with ``key_format.decode_secret``."""
    return _from_format(
        "MacKeyFromFormat",
        MacKey(algorithm),
        format,
        lambda data: cast("Any", key_format.decode_secret)(format, algorithm, data),
        allow_encode,
    )

AeadKey

AeadKey[A: aead.Algorithm](algorithm) -> Bottom[aead.Key[A]]

Live aead.Key values of algorithm.

ParameterTypeDescription
algorithmA

Returns Bottom[aead.Key[A]]

def AeadKey[A: aead.Algorithm](algorithm: A) -> Bottom[aead.Key[A]]:
    """Live ``aead.Key`` values of ``algorithm``."""
    return _declare("AeadKey", "Aead", "Key", aead.Key, algorithm)

AeadKeyFromBytes

AeadKeyFromBytes[A: aead.Algorithm](algorithm, *, allow_encode=False) -> DecodeToSchema[aead.Key[A], bytes]

Raw key bytes (base64 under config.schema) as an aead.Key.

ParameterTypeDescription
algorithmA
allow_encodebool = False

Returns DecodeToSchema[aead.Key[A], bytes]

def AeadKeyFromBytes[A: aead.Algorithm](
    algorithm: A, *, allow_encode: bool = False
) -> DecodeToSchema[aead.Key[A], bytes]:
    """Raw key bytes (base64 under ``config.schema``) as an ``aead.Key``."""
    return _from_bytes(
        "AeadKeyFromBytes",
        AeadKey(algorithm),
        lambda data: aead.key(algorithm, data),
        aead.to_bytes if allow_encode else None,
    )

AeadKeyFromFormat

AeadKeyFromFormat[A: aead.Algorithm, Data](format, algorithm, *, allow_encode=False) -> DecodeToSchema[aead.Key[A], Data]

An aead.Key read with key_format.decode_secret.

ParameterTypeDescription
formatkey_format.Format[Literal['secret'], _Decode, Data]
algorithmA
allow_encodebool = False

Returns DecodeToSchema[aead.Key[A], Data]

def AeadKeyFromFormat[A: aead.Algorithm, Data](
    format: key_format.Format[Literal["secret"], _Decode, Data],
    algorithm: A,
    *,
    allow_encode: bool = False,
) -> DecodeToSchema[aead.Key[A], Data]:
    """An ``aead.Key`` read with ``key_format.decode_secret``."""
    return _from_format(
        "AeadKeyFromFormat",
        AeadKey(algorithm),
        format,
        lambda data: cast("Any", key_format.decode_secret)(format, algorithm, data),
        allow_encode,
    )

CipherKey

CipherKey[A: cipher.Algorithm](algorithm) -> Bottom[cipher.Key[A]]

Live cipher.Key values of algorithm.

ParameterTypeDescription
algorithmA

Returns Bottom[cipher.Key[A]]

def CipherKey[A: cipher.Algorithm](algorithm: A) -> Bottom[cipher.Key[A]]:
    """Live ``cipher.Key`` values of ``algorithm``."""
    return _declare("CipherKey", "Cipher", "Key", cipher.Key, algorithm)

CipherKeyFromBytes

CipherKeyFromBytes[A: cipher.Algorithm](algorithm, *, allow_encode=False) -> DecodeToSchema[cipher.Key[A], bytes]

Raw key bytes (base64 under config.schema) as a cipher.Key.

ParameterTypeDescription
algorithmA
allow_encodebool = False

Returns DecodeToSchema[cipher.Key[A], bytes]

def CipherKeyFromBytes[A: cipher.Algorithm](
    algorithm: A, *, allow_encode: bool = False
) -> DecodeToSchema[cipher.Key[A], bytes]:
    """Raw key bytes (base64 under ``config.schema``) as a ``cipher.Key``."""
    return _from_bytes(
        "CipherKeyFromBytes",
        CipherKey(algorithm),
        lambda data: cipher.key(algorithm, data),
        cipher.to_bytes if allow_encode else None,
    )

CipherKeyFromFormat

CipherKeyFromFormat[A: cipher.Algorithm, Data](format, algorithm, *, allow_encode=False) -> DecodeToSchema[cipher.Key[A], Data]

A cipher.Key read with key_format.decode_secret.

ParameterTypeDescription
formatkey_format.Format[Literal['secret'], _Decode, Data]
algorithmA
allow_encodebool = False

Returns DecodeToSchema[cipher.Key[A], Data]

def CipherKeyFromFormat[A: cipher.Algorithm, Data](
    format: key_format.Format[Literal["secret"], _Decode, Data],
    algorithm: A,
    *,
    allow_encode: bool = False,
) -> DecodeToSchema[cipher.Key[A], Data]:
    """A ``cipher.Key`` read with ``key_format.decode_secret``."""
    return _from_format(
        "CipherKeyFromFormat",
        CipherKey(algorithm),
        format,
        lambda data: cast("Any", key_format.decode_secret)(format, algorithm, data),
        allow_encode,
    )

SignaturePublicKey

SignaturePublicKey[A: signature.Algorithm](algorithm) -> Bottom[signature.PublicKey[A]]

Live signature.PublicKey values of algorithm.

ParameterTypeDescription
algorithmA

Returns Bottom[signature.PublicKey[A]]

def SignaturePublicKey[A: signature.Algorithm](algorithm: A) -> Bottom[signature.PublicKey[A]]:
    """Live ``signature.PublicKey`` values of ``algorithm``."""
    return _declare("SignaturePublicKey", "Signature", "PublicKey", signature.PublicKey, algorithm)

SignaturePublicKeyFromBytes

SignaturePublicKeyFromBytes[A: signature.Ed25519 | signature.Ecdsa[Any]](algorithm) -> DecodeToSchema[signature.PublicKey[A], bytes]

A raw Ed25519 key or uncompressed EC point as a signature.PublicKey.

ParameterTypeDescription
algorithmA

Returns DecodeToSchema[signature.PublicKey[A], bytes]

def SignaturePublicKeyFromBytes[A: signature.Ed25519 | signature.Ecdsa[Any]](
    algorithm: A,
) -> DecodeToSchema[signature.PublicKey[A], bytes]:
    """A raw Ed25519 key or uncompressed EC point as a ``signature.PublicKey``."""
    return _from_bytes(
        "SignaturePublicKeyFromBytes",
        SignaturePublicKey(algorithm),
        lambda data: signature.public_key(algorithm, data),
        signature.to_bytes,
    )

SignaturePublicKeyFromFormat

SignaturePublicKeyFromFormat[A: signature.Algorithm, Data](format, algorithm) -> DecodeToSchema[signature.PublicKey[A], Data]

A signature.PublicKey read with key_format.decode_public and written with key_format.encode.

ParameterTypeDescription
formatkey_format.Format[Literal['public'], _Decode, Data]
algorithmA

Returns DecodeToSchema[signature.PublicKey[A], Data]

def SignaturePublicKeyFromFormat[A: signature.Algorithm, Data](
    format: key_format.Format[Literal["public"], _Decode, Data], algorithm: A
) -> DecodeToSchema[signature.PublicKey[A], Data]:
    """A ``signature.PublicKey`` read with ``key_format.decode_public`` and
    written with ``key_format.encode``."""
    return _from_format(
        "SignaturePublicKeyFromFormat",
        SignaturePublicKey(algorithm),
        format,
        lambda data: cast("Any", key_format.decode_public)(format, algorithm, data),
        True,
    )

SignaturePrivateKey

SignaturePrivateKey[A: signature.Algorithm](algorithm) -> Bottom[signature.PrivateKey[A]]

Live signature.PrivateKey values of algorithm.

ParameterTypeDescription
algorithmA

Returns Bottom[signature.PrivateKey[A]]

def SignaturePrivateKey[A: signature.Algorithm](algorithm: A) -> Bottom[signature.PrivateKey[A]]:
    """Live ``signature.PrivateKey`` values of ``algorithm``."""
    return _declare(
        "SignaturePrivateKey", "Signature", "PrivateKey", signature.PrivateKey, algorithm
    )

SignaturePrivateKeyFromBytes

SignaturePrivateKeyFromBytes[A: signature.Ed25519](algorithm, *, allow_encode=False) -> DecodeToSchema[signature.PrivateKey[A], bytes]

An Ed25519 seed as a signature.PrivateKey.

ParameterTypeDescription
algorithmA
allow_encodebool = False

Returns DecodeToSchema[signature.PrivateKey[A], bytes]

def SignaturePrivateKeyFromBytes[A: signature.Ed25519](
    algorithm: A, *, allow_encode: bool = False
) -> DecodeToSchema[signature.PrivateKey[A], bytes]:
    """An Ed25519 seed as a ``signature.PrivateKey``."""
    return _from_bytes(
        "SignaturePrivateKeyFromBytes",
        SignaturePrivateKey(algorithm),
        lambda data: signature.private_key(algorithm, data),
        signature.to_bytes if allow_encode else None,
    )

SignaturePrivateKeyFromFormat

SignaturePrivateKeyFromFormat[A: signature.Algorithm, Data](format, algorithm, *, allow_encode=False) -> DecodeToSchema[signature.PrivateKey[A], Data]

A signature.PrivateKey read with key_format.decode_private.

ParameterTypeDescription
formatkey_format.Format[Literal['private'], _Decode, Data]
algorithmA
allow_encodebool = False

Returns DecodeToSchema[signature.PrivateKey[A], Data]

def SignaturePrivateKeyFromFormat[A: signature.Algorithm, Data](
    format: key_format.Format[Literal["private"], _Decode, Data],
    algorithm: A,
    *,
    allow_encode: bool = False,
) -> DecodeToSchema[signature.PrivateKey[A], Data]:
    """A ``signature.PrivateKey`` read with ``key_format.decode_private``."""
    return _from_format(
        "SignaturePrivateKeyFromFormat",
        SignaturePrivateKey(algorithm),
        format,
        lambda data: cast("Any", key_format.decode_private)(format, algorithm, data),
        allow_encode,
    )

KeyAgreementPublicKey

KeyAgreementPublicKey[A: key_agreement.Algorithm](algorithm) -> Bottom[key_agreement.PublicKey[A]]

Live key_agreement.PublicKey values of algorithm.

ParameterTypeDescription
algorithmA

Returns Bottom[key_agreement.PublicKey[A]]

def KeyAgreementPublicKey[A: key_agreement.Algorithm](
    algorithm: A,
) -> Bottom[key_agreement.PublicKey[A]]:
    """Live ``key_agreement.PublicKey`` values of ``algorithm``."""
    return _declare(
        "KeyAgreementPublicKey", "KeyAgreement", "PublicKey", key_agreement.PublicKey, algorithm
    )

KeyAgreementPublicKeyFromBytes

KeyAgreementPublicKeyFromBytes[A: key_agreement.Algorithm](algorithm) -> DecodeToSchema[key_agreement.PublicKey[A], bytes]

A raw X25519 key or uncompressed EC point as a key_agreement.PublicKey.

ParameterTypeDescription
algorithmA

Returns DecodeToSchema[key_agreement.PublicKey[A], bytes]

def KeyAgreementPublicKeyFromBytes[A: key_agreement.Algorithm](
    algorithm: A,
) -> DecodeToSchema[key_agreement.PublicKey[A], bytes]:
    """A raw X25519 key or uncompressed EC point as a ``key_agreement.PublicKey``."""
    return _from_bytes(
        "KeyAgreementPublicKeyFromBytes",
        KeyAgreementPublicKey(algorithm),
        lambda data: key_agreement.public_key(algorithm, data),
        key_agreement.to_bytes,
    )

KeyAgreementPublicKeyFromFormat

KeyAgreementPublicKeyFromFormat[A: key_agreement.Algorithm, Data](format, algorithm) -> DecodeToSchema[key_agreement.PublicKey[A], Data]

A key_agreement.PublicKey read with key_format.decode_public and written with key_format.encode.

ParameterTypeDescription
formatkey_format.Format[Literal['public'], _Decode, Data]
algorithmA

Returns DecodeToSchema[key_agreement.PublicKey[A], Data]

def KeyAgreementPublicKeyFromFormat[A: key_agreement.Algorithm, Data](
    format: key_format.Format[Literal["public"], _Decode, Data], algorithm: A
) -> DecodeToSchema[key_agreement.PublicKey[A], Data]:
    """A ``key_agreement.PublicKey`` read with ``key_format.decode_public``
    and written with ``key_format.encode``."""
    return _from_format(
        "KeyAgreementPublicKeyFromFormat",
        KeyAgreementPublicKey(algorithm),
        format,
        lambda data: cast("Any", key_format.decode_public)(format, algorithm, data),
        True,
    )

KeyAgreementPrivateKey

KeyAgreementPrivateKey[A: key_agreement.Algorithm](algorithm) -> Bottom[key_agreement.PrivateKey[A]]

Live key_agreement.PrivateKey values of algorithm.

ParameterTypeDescription
algorithmA

Returns Bottom[key_agreement.PrivateKey[A]]

def KeyAgreementPrivateKey[A: key_agreement.Algorithm](
    algorithm: A,
) -> Bottom[key_agreement.PrivateKey[A]]:
    """Live ``key_agreement.PrivateKey`` values of ``algorithm``."""
    return _declare(
        "KeyAgreementPrivateKey", "KeyAgreement", "PrivateKey", key_agreement.PrivateKey, algorithm
    )

KeyAgreementPrivateKeyFromBytes

KeyAgreementPrivateKeyFromBytes[A: key_agreement.X25519](algorithm, *, allow_encode=False) -> DecodeToSchema[key_agreement.PrivateKey[A], bytes]

An X25519 private key's 32 bytes as a key_agreement.PrivateKey.

ParameterTypeDescription
algorithmA
allow_encodebool = False

Returns DecodeToSchema[key_agreement.PrivateKey[A], bytes]

def KeyAgreementPrivateKeyFromBytes[A: key_agreement.X25519](
    algorithm: A, *, allow_encode: bool = False
) -> DecodeToSchema[key_agreement.PrivateKey[A], bytes]:
    """An X25519 private key's 32 bytes as a ``key_agreement.PrivateKey``."""
    return _from_bytes(
        "KeyAgreementPrivateKeyFromBytes",
        KeyAgreementPrivateKey(algorithm),
        lambda data: key_agreement.private_key(algorithm, data),
        key_agreement.to_bytes if allow_encode else None,
    )

KeyAgreementPrivateKeyFromFormat

KeyAgreementPrivateKeyFromFormat[A: key_agreement.Algorithm, Data](format, algorithm, *, allow_encode=False) -> DecodeToSchema[key_agreement.PrivateKey[A], Data]

A key_agreement.PrivateKey read with key_format.decode_private.

ParameterTypeDescription
formatkey_format.Format[Literal['private'], _Decode, Data]
algorithmA
allow_encodebool = False

Returns DecodeToSchema[key_agreement.PrivateKey[A], Data]

def KeyAgreementPrivateKeyFromFormat[A: key_agreement.Algorithm, Data](
    format: key_format.Format[Literal["private"], _Decode, Data],
    algorithm: A,
    *,
    allow_encode: bool = False,
) -> DecodeToSchema[key_agreement.PrivateKey[A], Data]:
    """A ``key_agreement.PrivateKey`` read with ``key_format.decode_private``."""
    return _from_format(
        "KeyAgreementPrivateKeyFromFormat",
        KeyAgreementPrivateKey(algorithm),
        format,
        lambda data: cast("Any", key_format.decode_private)(format, algorithm, data),
        allow_encode,
    )

KeyWrapKey

KeyWrapKey[A: key_wrap.AesKw](algorithm) -> Bottom[key_wrap.Key[A]]

Live AES-KW key_wrap.Key values of algorithm.

ParameterTypeDescription
algorithmA

Returns Bottom[key_wrap.Key[A]]

def KeyWrapKey[A: key_wrap.AesKw](algorithm: A) -> Bottom[key_wrap.Key[A]]:
    """Live AES-KW ``key_wrap.Key`` values of ``algorithm``."""
    return _declare("KeyWrapKey", "KeyWrap", "Key", key_wrap.Key, algorithm)

KeyWrapKeyFromBytes

KeyWrapKeyFromBytes[A: key_wrap.AesKw](algorithm, *, allow_encode=False) -> DecodeToSchema[key_wrap.Key[A], bytes]

Raw key bytes (base64 under config.schema) as a key_wrap.Key.

ParameterTypeDescription
algorithmA
allow_encodebool = False

Returns DecodeToSchema[key_wrap.Key[A], bytes]

def KeyWrapKeyFromBytes[A: key_wrap.AesKw](
    algorithm: A, *, allow_encode: bool = False
) -> DecodeToSchema[key_wrap.Key[A], bytes]:
    """Raw key bytes (base64 under ``config.schema``) as a ``key_wrap.Key``."""
    return _from_bytes(
        "KeyWrapKeyFromBytes",
        KeyWrapKey(algorithm),
        lambda data: key_wrap.key(algorithm, data),
        key_wrap.to_bytes if allow_encode else None,
    )

KeyWrapKeyFromFormat

KeyWrapKeyFromFormat[A: key_wrap.AesKw, Data](format, algorithm, *, allow_encode=False) -> DecodeToSchema[key_wrap.Key[A], Data]

A key_wrap.Key read with key_format.decode_secret.

ParameterTypeDescription
formatkey_format.Format[Literal['secret'], _Decode, Data]
algorithmA
allow_encodebool = False

Returns DecodeToSchema[key_wrap.Key[A], Data]

def KeyWrapKeyFromFormat[A: key_wrap.AesKw, Data](
    format: key_format.Format[Literal["secret"], _Decode, Data],
    algorithm: A,
    *,
    allow_encode: bool = False,
) -> DecodeToSchema[key_wrap.Key[A], Data]:
    """A ``key_wrap.Key`` read with ``key_format.decode_secret``."""
    return _from_format(
        "KeyWrapKeyFromFormat",
        KeyWrapKey(algorithm),
        format,
        lambda data: cast("Any", key_format.decode_secret)(format, algorithm, data),
        allow_encode,
    )

KeyWrapPublicKey

KeyWrapPublicKey[A: key_wrap.RsaOaep](algorithm) -> Bottom[key_wrap.PublicKey[A]]

Live RSA-OAEP key_wrap.PublicKey values of algorithm.

ParameterTypeDescription
algorithmA

Returns Bottom[key_wrap.PublicKey[A]]

def KeyWrapPublicKey[A: key_wrap.RsaOaep](algorithm: A) -> Bottom[key_wrap.PublicKey[A]]:
    """Live RSA-OAEP ``key_wrap.PublicKey`` values of ``algorithm``."""
    return _declare("KeyWrapPublicKey", "KeyWrap", "PublicKey", key_wrap.PublicKey, algorithm)

KeyWrapPublicKeyFromFormat

KeyWrapPublicKeyFromFormat[A: key_wrap.RsaOaep, Data](format, algorithm) -> DecodeToSchema[key_wrap.PublicKey[A], Data]

A key_wrap.PublicKey read with key_format.decode_public and written with key_format.encode.

ParameterTypeDescription
formatkey_format.Format[Literal['public'], _Decode, Data]
algorithmA

Returns DecodeToSchema[key_wrap.PublicKey[A], Data]

def KeyWrapPublicKeyFromFormat[A: key_wrap.RsaOaep, Data](
    format: key_format.Format[Literal["public"], _Decode, Data], algorithm: A
) -> DecodeToSchema[key_wrap.PublicKey[A], Data]:
    """A ``key_wrap.PublicKey`` read with ``key_format.decode_public`` and
    written with ``key_format.encode``."""
    return _from_format(
        "KeyWrapPublicKeyFromFormat",
        KeyWrapPublicKey(algorithm),
        format,
        lambda data: cast("Any", key_format.decode_public)(format, algorithm, data),
        True,
    )

KeyWrapPrivateKey

KeyWrapPrivateKey[A: key_wrap.RsaOaep](algorithm) -> Bottom[key_wrap.PrivateKey[A]]

Live RSA-OAEP key_wrap.PrivateKey values of algorithm.

ParameterTypeDescription
algorithmA

Returns Bottom[key_wrap.PrivateKey[A]]

def KeyWrapPrivateKey[A: key_wrap.RsaOaep](algorithm: A) -> Bottom[key_wrap.PrivateKey[A]]:
    """Live RSA-OAEP ``key_wrap.PrivateKey`` values of ``algorithm``."""
    return _declare("KeyWrapPrivateKey", "KeyWrap", "PrivateKey", key_wrap.PrivateKey, algorithm)

KeyWrapPrivateKeyFromFormat

KeyWrapPrivateKeyFromFormat[A: key_wrap.RsaOaep, Data](format, algorithm, *, allow_encode=False) -> DecodeToSchema[key_wrap.PrivateKey[A], Data]

A key_wrap.PrivateKey read with key_format.decode_private.

ParameterTypeDescription
formatkey_format.Format[Literal['private'], _Decode, Data]
algorithmA
allow_encodebool = False

Returns DecodeToSchema[key_wrap.PrivateKey[A], Data]

def KeyWrapPrivateKeyFromFormat[A: key_wrap.RsaOaep, Data](
    format: key_format.Format[Literal["private"], _Decode, Data],
    algorithm: A,
    *,
    allow_encode: bool = False,
) -> DecodeToSchema[key_wrap.PrivateKey[A], Data]:
    """A ``key_wrap.PrivateKey`` read with ``key_format.decode_private``."""
    return _from_format(
        "KeyWrapPrivateKeyFromFormat",
        KeyWrapPrivateKey(algorithm),
        format,
        lambda data: cast("Any", key_format.decode_private)(format, algorithm, data),
        allow_encode,
    )