GitHub

MacService

API reference for MacService.

The Mac service: layer_web_crypto builds it, and MacService(generate_key=..., sign=..., verify=...) makes one of three functions (a test double, say).

Functions

__init__

__init__(*, generate_key, sign, verify) -> None
ParameterTypeDescription
generate_keyCallable[[Algorithm], Effect[Key[Any], CryptoError]]
signCallable[[Key[Any], Binary], Effect[bytes, CryptoError]]
verifyCallable[[Key[Any], Binary, Binary], Effect[bool, CryptoError]]
def __init__(
    self,
    *,
    generate_key: Callable[[Algorithm], Effect[Key[Any], CryptoError]],
    sign: Callable[[Key[Any], Binary], Effect[bytes, CryptoError]],
    verify: Callable[[Key[Any], Binary, Binary], Effect[bool, CryptoError]],
) -> None:
    self._generate_key = generate_key
    self._sign = sign
    self._verify = verify

generate_key

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

A key of random bytes from SecureRandom, as long as the hash's block size (as Web Crypto generates them).

ParameterTypeDescription
algorithmA

Returns Effect[Key[A], CryptoError]

def generate_key[A: Algorithm](self, algorithm: A) -> Effect[Key[A], CryptoError]:
    """A key of random bytes from ``SecureRandom``, as long as the
    hash's block size (as Web Crypto generates them)."""
    return cast("Effect[Key[A], CryptoError]", self._generate_key(algorithm))

sign

sign(key, data) -> Effect[bytes, CryptoError]

The HMAC tag of data under key.

ParameterTypeDescription
keyKey[Any]
dataBinary

Returns Effect[bytes, CryptoError]

def sign(self, key: Key[Any], data: Binary) -> Effect[bytes, CryptoError]:
    """The HMAC tag of ``data`` under ``key``."""
    return self._sign(key, data)

verify

verify(key, tag, data) -> Effect[bool, CryptoError]

Whether tag is the HMAC tag of data under key, compared in constant time; False for a tag of another length.

ParameterTypeDescription
keyKey[Any]
tagBinary
dataBinary

Returns Effect[bool, CryptoError]

def verify(self, key: Key[Any], tag: Binary, data: Binary) -> Effect[bool, CryptoError]:
    """Whether ``tag`` is the HMAC tag of ``data`` under ``key``,
    compared in constant time; ``False`` for a tag of another length."""
    return self._verify(key, tag, data)