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

WebCrypto unstable

The WebCrypto Crypto object that the effect/crypto modules run on.

The layerWebCrypto layers of the family modules, such as Digest.layerWebCrypto, and key functions that touch crypto.subtle, such as Mac.toBytes, read the WebCrypto reference. It defaults to globalThis.crypto. Provide layer to run them on a polyfill, a worker's object, or a test double.

Gotchas

Browsers expose crypto.subtle only in secure contexts. Elsewhere, every operation that needs it dies with "crypto.subtle is unavailable". Layers still build, and SecureRandom and the pure TypeScript algorithms, such as MD5, keep working.

2 exports Since v4.0.0 Source

Layers

layer

unstable
Since v4.0.0

Layer that sets the WebCrypto reference.

When to use

Use to run the effect/crypto modules on a polyfill, a worker's Crypto object, or a test double.

Signature

declare function layer(crypto: Crypto): Layer<never>

Example

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

References

WebCrypto

unstable
Since v4.0.0

Reference to the WebCrypto Crypto object, defaulting to globalThis.crypto.

See

Signature

declare const WebCrypto: Context.Reference<Crypto>