/** * @file Crypto hash helpers that prefer Node builtins where available. Probes * `node:crypto.hash()` (added v21.7.0 / v20.12.0) and falls back to the * streaming `createHash().update().digest()` chain on older runtimes. Which * digest goes where (integrity = sha512 trust boundary; checksum = sha256 * interop shape; cache keys = truncated, never trust): see * `docs/hash-algorithms.md`. */ import type { hash as NodeHash } from 'node:crypto'; /** * Compute the Socket content-address of `bytes`: `Q` + * base64url(sha256(bytes)). * * Matches the blob store's hash scheme so a fetched blob can be verified * against the hash it was requested by. * * @example * ;```typescript * import { blobHashOf } from '@socketsecurity/lib/crypto/hash' * * blobHashOf(new TextEncoder().encode('hello')) * ``` */ export declare function blobHashOf(bytes: NodeJS.ArrayBufferView): string; /** * Compute a one-shot cryptographic hash. * * Prefers Node's `crypto.hash(algorithm, data, outputEncoding)` (added v21.7.0 * / v20.12.0), which is ~30% faster than the `createHash().update().digest()` * chain on small-to-medium inputs because it skips constructing the streaming * `Hash` object. Falls back to that chain on older runtimes. * * Use this only for the one-shot case where the entire input is available as a * single buffer or string; if you need to feed chunks, stay on `createHash()`. * * @example * ;```typescript * import { hash } from '@socketsecurity/lib/crypto/hash' * * hash('sha256', 'hello', 'hex') * // '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824' * * hash('sha512', someBuffer, 'base64') * // 'z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==' * ``` */ export declare function hash(algorithm: string, data: string | NodeJS.ArrayBufferView, outputEncoding: 'hex' | 'base64' | 'base64url' | 'binary'): string; /** * Resolve `crypto.hash` (or `undefined` if the runtime predates it). * * Exported for unit tests; not part of the public API. * * @internal */ export declare function nativeHash(): typeof NodeHash | undefined; /** * Throw if `bytes` does not content-address to `blobHash`. Both `Q`-prefixed * single-blob and `S`-prefixed file-stream hashes share the sha256 digest * body, so both verify against the same digest; the leading discriminator char * is dropped before comparison. * * @throws {Error} When the recomputed digest does not match `blobHash`. */ export declare function verifyBlobHash(blobHash: string, bytes: NodeJS.ArrayBufferView): void;