GitHub

API reference for digest.

Message digests as a service (Effect's effect/crypto/Digest)::

digest = yield from Digest()
hex_text = yield from digest.digest_hex(sha256, data)

The algorithms are values: md5, sha1, sha256, sha384, sha512. layer_web_crypto runs the SHA family on the WebCrypto reference's subtle and MD5 in the process (hashlib), so MD5 works without subtle. MD5 and SHA-1 are for existing protocols only.

Values

AttributeTypeDescription
md5Algorithm
sha1Algorithm
sha256Algorithm
sha384Algorithm
sha512Algorithm
layer_web_cryptoLayer[Digest]

Digests on the WebCrypto reference, MD5 in the process.

Types

TypeDefinitionDescription
Nametype Name = Literal['MD5', 'SHA-1', 'SHA-256', 'SHA-384', 'SHA-512']

Classes

Functions

name_suffix

name_suffix(hash) -> str

"Sha256" for SHA-256, as the algorithm names use it.

ParameterTypeDescription
hashAlgorithm

Returns str

def name_suffix(hash: Algorithm) -> str:
    """``"Sha256"`` for SHA-256, as the algorithm names use it."""
    return hash.name[0] + hash.name[1:].lower().replace("-", "")

web_crypto_digest

web_crypto_digest(crypto, algorithm, data) -> Effect[bytes]

algorithm's digest of data: its implementation, or crypto.subtle (dying when there is none).

ParameterTypeDescription
cryptoCrypto | None
algorithmAlgorithm
dataBinary

Returns Effect[bytes]

def web_crypto_digest(crypto: Crypto | None, algorithm: Algorithm, data: Binary) -> Effect[bytes]:
    """``algorithm``'s digest of ``data``: its ``implementation``, or
    ``crypto.subtle`` (dying when there is none)."""
    implementation = algorithm.implementation
    if implementation is not None:
        return sync(lambda: implementation(bytes(data)))

    def go() -> Effect[bytes]:
        subtle = None if crypto is None else crypto.subtle
        if subtle is None:
            return die(_web.subtle_unavailable())
        return sync(lambda: bytes(subtle.digest(algorithm.name, bytes(data))))

    return suspend(go)