/** * Message authentication codes: HMAC with SHA-1, SHA-256, SHA-384, or * SHA-512. * * The {@link Mac} service generates keys, signs data, and verifies tags. The * top-level {@link key}, {@link fromCryptoKey}, and {@link toBytes} functions * build and export keys without a service, and {@link timingSafeEqual} * compares byte arrays in constant time. * * Keys are opaque. They hold either key bytes or a `CryptoKey`, display only * their algorithm, and compare by reference. * * @stability unstable * @since 4.0.0 */ import * as Context from "../Context.ts"; import * as Effect from "../Effect.ts"; import type { Inspectable } from "../Inspectable.ts"; import * as Layer from "../Layer.ts"; import type * as CryptoError from "./CryptoError.ts"; import * as Digest from "./Digest.ts"; import * as SecureRandom from "./SecureRandom.ts"; import * as WebCrypto from "./WebCrypto.ts"; declare const TypeId: "~effect/crypto/Mac/Key"; /** * An HMAC algorithm, such as {@link hmacSha256}. * * **Details** * * `hash` is the digest the HMAC is built on. * * @stability unstable * @category models * @since 4.0.0 */ export interface Algorithm { readonly name: "hmacSha1" | "hmacSha256" | "hmacSha384" | "hmacSha512"; readonly hash: Digest.Algorithm; } /** * HMAC with SHA-1, with a 20-byte tag. * * **Gotchas** * * HMAC-SHA-1 is still secure as a MAC, but use it only for interoperability * with existing protocols. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const hmacSha1: Algorithm; /** * HMAC with SHA-256, with a 32-byte tag. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const hmacSha256: Algorithm; /** * HMAC with SHA-384, with a 48-byte tag. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const hmacSha384: Algorithm; /** * HMAC with SHA-512, with a 64-byte tag. * * @stability unstable * @category algorithms * @since 4.0.0 */ export declare const hmacSha512: Algorithm; /** * An opaque HMAC key for the algorithm `A`. * * **Details** * * The key holds either key bytes or a `CryptoKey` from {@link fromCryptoKey}. * `toString`, `toJSON`, and `inspect` show only the algorithm. Keys compare * by reference and do not implement `Equal`, because comparing key bytes is * not constant time. * * @stability unstable * @category models * @since 4.0.0 */ export interface Key extends Inspectable { readonly [TypeId]: typeof TypeId; readonly algorithm: A; } /** * Checks whether a value is a {@link Key}. * * @stability unstable * @category guards * @since 4.0.0 */ export declare const isKey: (u: unknown) => u is Key; declare const Mac_base: Context.ServiceClass(algorithm: A) => Effect.Effect, CryptoError.CryptoError>; /** * Computes the tag of `data`. */ readonly sign: (key: Key, data: Uint8Array) => Effect.Effect; /** * Checks `tag` against the tag of `data` in constant time. A wrong tag, or * a tag of the wrong length, returns `false`. */ readonly verify: (key: Key, tag: Uint8Array, data: Uint8Array) => Effect.Effect; }>; /** * Service that generates HMAC keys, signs data, and verifies tags. * * **Example** (Signing and verifying a message) * * ```ts import.meta.vitest * import { Effect, Layer } from "effect" * import { Mac, SecureRandom } from "effect/crypto" * * const program = Effect.gen(function*() { * const mac = yield* Mac.Mac * const key = yield* mac.generateKey(Mac.hmacSha256) * const data = new TextEncoder().encode("message") * const tag = yield* mac.sign(key, data) * return yield* mac.verify(key, tag, data) * }) * * const layer = Mac.layerWebCrypto.pipe(Layer.provide(SecureRandom.layerWebCrypto)) * const valid = await Effect.runPromise(program.pipe(Effect.provide(layer))) * valid // => true * ``` * * @stability unstable * @category services * @since 4.0.0 */ export declare class Mac extends Mac_base { } /** * Builds a `Mac` layer on the `WebCrypto` reference that can sign and verify * byte-backed keys with a synchronous HMAC primitive. * * **When to use** * * Use to run HMAC on a runtime's native code, as `NodeCrypto.layerMac` does * with `createHmac` from `node:crypto`. * * **Details** * * `hmac` receives the hash name, the key bytes, and the data, and returns the * tag. It runs only for byte-backed keys. `CryptoKey`-backed keys run on * `crypto.subtle`, and generated keys come from `SecureRandom`, as in * {@link layerWebCrypto}. `verify` computes the tag with `hmac` and compares * it with {@link timingSafeEqual}. Without `hmac`, the layer is * {@link layerWebCrypto}. * * **Gotchas** * * `hmac` receives the key's own bytes, not a copy, so it must not modify or * keep them. * * **Example** (Signing with a native HMAC) * * ```ts * import { Layer } from "effect" * import { Mac, SecureRandom } from "effect/crypto" * * declare const nativeHmac: (hash: string, key: Uint8Array, data: Uint8Array) => Uint8Array * * const layer = Mac.layerWebCryptoWith({ hmac: nativeHmac }).pipe( * Layer.provide(SecureRandom.layerWebCrypto) * ) * ``` * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerWebCryptoWith: (options: { readonly hmac?: ((hash: Digest.Algorithm["name"], key: Uint8Array, data: Uint8Array) => Uint8Array) | undefined; }) => Layer.Layer; /** * Layer that provides `Mac` on the `WebCrypto` reference. * * **Details** * * Signing and verification run on `crypto.subtle`. The layer imports a * byte-backed key on first use and caches the `CryptoKey`. Generated keys come * from `SecureRandom`, so a seeded `SecureRandom` gives reproducible keys. * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerWebCrypto: Layer.Layer; /** * Builds a key from raw key bytes. * * **Details** * * The bytes are copied into a new `Uint8Array`, so later changes to the input, * including a Node.js `Buffer`, do not reach the key. Any non-empty length is * accepted; RFC 2104 recommends at least the hash's output length. An empty key * fails with an `InvalidKey` reason. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const key: (algorithm: A, bytes: Uint8Array) => Effect.Effect, CryptoError.CryptoError>; /** * Builds a key from an existing `CryptoKey`, such as a non-extractable one. * * **Details** * * The `CryptoKey` must be a secret HMAC key with the algorithm's hash and both * the `sign` and `verify` usages, since a key reaches both operations. * Otherwise the constructor fails with an `InvalidKey` reason. It must come * from the `Crypto` object of the `WebCrypto` reference. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const fromCryptoKey: (algorithm: A, cryptoKey: WebCrypto.CryptoKey) => Effect.Effect, CryptoError.CryptoError>; /** * Exports a key's bytes. * * **Details** * * Returns a fresh `Uint8Array` that shares no memory with the key. A * `CryptoKey`-backed key is exported on the `WebCrypto` reference, and fails * with a `NotExtractable` reason when the `CryptoKey` is not extractable. * * @stability unstable * @category converting * @since 4.0.0 */ export declare const toBytes: (key: Key) => Effect.Effect; /** * Compares two byte arrays in constant time. * * **When to use** * * Use to compare secrets of a fixed, known length, such as tags computed * outside {@link Mac}. To check an HMAC tag, use `Mac.verify`. * * **Gotchas** * * Arrays of different lengths return `false` immediately, revealing their * lengths. Equal-length arrays are compared with an XOR-accumulate loop; * JavaScript runtimes do not guarantee constant-time execution. * * **Example** (Comparing fixed-length secrets) * * ```ts import.meta.vitest * import { Mac } from "effect/crypto" * * const expected = new Uint8Array([1, 2, 3]) * const received = new Uint8Array([1, 2, 3]) * Mac.timingSafeEqual(expected, received) // => true * ``` * * @stability unstable * @category utils * @since 4.0.0 */ export declare const timingSafeEqual: (a: Uint8Array, b: Uint8Array) => boolean; export {}; //# sourceMappingURL=Mac.d.ts.map