Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

Digest unstable

Message digests: MD5, SHA-1, SHA-256, SHA-384, and SHA-512.

The Digest service computes digests and lowercase hex strings. Provide layerWebCrypto to run it on the WebCrypto reference, or mock the service with Layer.mock.

MD5 runs a pure TypeScript implementation vendored from @noble/hashes 2.4.0, because WebCrypto does not provide it. Whether the @noble/hashes audit covers its MD5 code has not been confirmed. The SHA algorithms run on crypto.subtle.

Output is stable byte for byte, so digests can be persisted.

Gotchas

MD5 and SHA-1 are broken for collision resistance. Use them only for interoperability with existing protocols and checksums.

8 exports Since v4.0.0 Source

Algorithms

md5

unstable
Since v4.0.0

MD5 (RFC 1321), with a 16-byte output.

Gotchas

MD5 is broken for collision resistance. Use it only for interoperability with existing protocols and checksums.

Signature

declare const md5: Algorithm

sha1

unstable
Since v4.0.0

SHA-1, with a 20-byte output.

Gotchas

SHA-1 is broken for collision resistance. Use it only for interoperability with existing protocols.

Signature

declare const sha1: Algorithm

sha256

unstable
Since v4.0.0

SHA-256, with a 32-byte output.

Signature

declare const sha256: Algorithm

sha384

unstable
Since v4.0.0

SHA-384, with a 48-byte output.

Signature

declare const sha384: Algorithm

sha512

unstable
Since v4.0.0

SHA-512, with a 64-byte output.

Signature

declare const sha512: Algorithm

Layers

Since v4.0.0

Layer that provides Digest on the WebCrypto reference.

Details

The SHA algorithms run on crypto.subtle, and MD5 on the pure TypeScript implementation. Without crypto.subtle, the layer still builds and MD5 keeps working, but SHA digests die.

Signature

declare const layerWebCrypto: Layer.Layer<Digest>

Models

Algorithm

interface unstable
Since v4.0.0

A digest algorithm, such as sha256.

Details

name is the WebCrypto algorithm name. implementation is the pure TypeScript code for algorithms that WebCrypto does not provide, which a layer calls when it has no native mapping for name.

Signature

interface Algorithm {
readonly implementation: (data: Uint8Array) => Uint8Array | undefined;
readonly name: "MD5" | "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";
readonly outputLength: number;
}

Services

Digest

unstable
Since v4.0.0

Service that computes message digests.

Signature

declare class Digest extends Shape<"effect/crypto/Digest", {
readonly digest: (algorithm: Algorithm, data: Uint8Array) => Effect<Uint8Array<ArrayBufferLike>>;
readonly digestHex: (algorithm: Algorithm, data: Uint8Array) => Effect<string>;
}, this> {
constructor(_: never);
}

Example Hashing with SHA-256

import { Effect } from "effect"
import { Digest } from "effect/crypto"
const program = Effect.gen(function*() {
const digest = yield* Digest.Digest
return yield* digest.digestHex(Digest.sha256, new TextEncoder().encode("abc"))
})
const hex = await Effect.runPromise(program.pipe(Effect.provide(Digest.layerWebCrypto)))
hex.length // => 64