GitHub

API reference for cipher.

Unauthenticated AES encryption as a service (Effect's effect/crypto/Cipher)::

cipher = yield from Cipher()
encrypted = yield from cipher.encrypt(key, data, iv=iv)

AES-CTR counts with the whole 128-bit block and never fails to decrypt; AES-CBC pads with PKCS#7 and fails every bad padding or length with the same DecryptionFailed (distinguishable failures would be a padding oracle). Prefer aead: nothing here detects tampering.

Re-exports

Values

AttributeTypeDescription
aes128_ctrAlgorithm
aes256_ctrAlgorithm
aes128_cbcAlgorithm
aes256_cbcAlgorithm
layer_web_cryptoLayer[Cipher, Never, SecureRandom]

AES-CTR and AES-CBC on the WebCrypto reference.

Classes

Functions

is_key

is_key(u) -> bool
ParameterTypeDescription
uobject

Returns bool

def is_key(u: object) -> bool:
    return isinstance(u, Key)

key

key[A: Algorithm](algorithm, data) -> Effect[Key[A], CryptoError]

A key holding a copy of data, which must be the algorithm's key length.

ParameterTypeDescription
algorithmA
dataBinary

Returns Effect[Key[A], CryptoError]

def key[A: Algorithm](algorithm: A, data: Binary) -> Effect[Key[A], CryptoError]:
    """A key holding a copy of ``data``, which must be the algorithm's key
    length."""
    return _key.from_bytes(
        lambda copied: Key(algorithm, copied),
        "Cipher",
        "key",
        data,
        _key.check_key_length(algorithm.name, algorithm.key_length),
    )

from_crypto_key

from_crypto_key[A: Algorithm](algorithm, crypto_key) -> Effect[Key[A], CryptoError]

A key backed by a CryptoKey of the algorithm's mode and size, with both the encrypt and decrypt usages.

ParameterTypeDescription
algorithmA
crypto_keyCryptoKey

Returns Effect[Key[A], CryptoError]

def from_crypto_key[A: Algorithm](
    algorithm: A, crypto_key: CryptoKey
) -> Effect[Key[A], CryptoError]:
    """A key backed by a ``CryptoKey`` of the algorithm's mode and size,
    with both the ``encrypt`` and ``decrypt`` usages."""
    return _key.from_crypto_key(
        Key,
        "Cipher",
        "fromCryptoKey",
        algorithm,
        crypto_key,
        _key.ExpectedCryptoKey(
            type="secret",
            name=algorithm.mode,
            usages=("encrypt", "decrypt"),
            usage_policy="all",
            check=_key.check_aes_length(algorithm.mode, algorithm.key_length),
        ),
    )

to_bytes

to_bytes(key) -> Effect[bytes, CryptoError]

A copy of the key's bytes; NotExtractable for a non-extractable CryptoKey.

ParameterTypeDescription
keyKey[Any]

Returns Effect[bytes, CryptoError]

def to_bytes(key: Key[Any]) -> Effect[bytes, CryptoError]:
    """A copy of the key's bytes; ``NotExtractable`` for a non-extractable
    ``CryptoKey``."""
    return _key.to_bytes("Cipher", key, _key.export_raw)