/** * Abstract logic for hashes. * @module */ import { type Asyncify, type TArg, type TRet } from './utils.ts'; export type HashMod = { readonly segments: { readonly buffer: Uint8Array; readonly 'state.state_chunks': ReadonlyArray; readonly state: Uint8Array; readonly state_chunks: ReadonlyArray; }; reset(batchPos: number, batchCnt: number, maxWritten: number, blockLen: number, maxBlocks: number): void; padding(batchPos: number, take: number, maxBlocks: number, left: number, blockLen: number, suffix: number): number; processBlocks(batchPos: number, batchCnt: number, blocks: number, maxBlocks: number, blockLen: number, isLast: number, left: number, padBlocks: number): void; processOutBlocks(batchPos: number, batchCnt: number, blocks: number, maxBlocks: number, outBlockLen: number, isLast: number): void; }; /** Hash algorithm definition shared by all runtime/target wrappers. */ export type HashDef = { /** Optional domain-separation suffix byte applied before the shared padding path. */ suffix?: number; /** Preferred chunk count for batch helpers. */ chunks?: number; /** Compression/input block size in bytes. */ blockLen: number; /** Output block size in bytes when it differs from {@link blockLen}. */ outputBlockLen?: number; /** Default digest length in bytes. */ outputLen: number; /** Whether the hash supports variable-length XOF output. */ canXOF?: boolean; /** Optional ASN.1 object identifier for fixed-output hash variants. */ oid?: TRet; /** * Initialize state and optionally override block count or effective output length. * @param batchPos - batch slot being initialized. * @param maxBlocks - maximum number of blocks available in the shared buffers. * @param mod - backend hash module implementation for this batch slot. * @param hash - public hash helper being configured. * @param opts - merged hash and output options for this initialization; see {@link MergeOpts}. * @returns Optional overrides for preloaded block count or effective digest length. */ init?: (batchPos: number, maxBlocks: number, mod: Mod, hash: HashInstance, opts: MergeOpts) => void | { blocks?: number; outputLen?: number; }; }; /** Shared output-buffer options for one-shot and streaming hash APIs. */ export type OutputOpts = { /** Optional destination buffer to write the digest or XOF output into. */ out?: TArg; /** Starting offset inside {@link out}. */ outPos?: number; /** Requested digest length in bytes. */ dkLen?: number; }; /** Options accepted by multi-input hash APIs: `chunks` and `parallel`. */ export type HashBatchOpts = OutputOpts & { /** * Opaque state from `create().exportState()`. * The only supported use is passing it back to the same hash, platform, and library version. */ prefixState?: TArg; }; /** Options accepted by `parallel`, including caller-owned per-lane output views. */ export type HashParallelOpts = Omit & { /** Optional flat output buffer or one exact-size destination view per input lane. */ out?: TArg; /** * Opaque state from `create().exportState()`. * The only supported use is passing it back to the same hash, platform, and library version. */ prefixState?: TArg; }; type CreateOutputOpts = Pick; type CreateOpts = MergeOpts; declare const HASH_STATE: unique symbol; /** * Opaque exported hash state; not necessarily a byte array. * The value is an owned cleanup handle for one hash/platform/version only. */ export type HashState = { /** Internal brand that prevents treating arbitrary values as exported hash states. */ readonly [HASH_STATE]: true; }; /** Streaming hash instance returned by {@link HashInstance.create}. */ export type HashStream = { /** Input block size in bytes for the underlying hash. */ blockLen: number; /** Current digest length in bytes, including `create()` digest-length overrides. */ outputLen: number; /** Whether this stream supports variable-length XOF output via xof()/xofInto(). */ canXOF: boolean; /** Absorb more message bytes into the stream and return the same stream. */ update(msg: TArg): HashStream; /** * Finalize the stream and return the digest bytes. * @param opts - optional {@link OutputOpts} controlling digest length or destination buffer. * @returns Digest bytes. */ digest(opts?: OutputOpts): TRet; /** Wipe internal state and make the stream unusable. */ destroy(): void; /** * Finalize the stream and return variable-length XOF output. * @param bytes - number of XOF bytes to return. * @param opts - optional {@link OutputOpts} controlling output length or destination buffer. * @returns XOF output bytes. */ xof(bytes: number, opts?: OutputOpts): TRet; /** Copy the current stream state into another stream or a freshly created clone target. */ _cloneInto(to?: HashStream): HashStream; /** Clone the current stream state. */ clone(): HashStream; /** * Export an opaque state that can be reused as `prefixState` by this exact hash/platform/version. * Callers must eventually pass it to `hash.cleanState(state)`. */ exportState(): TRet; /** * Finalize the stream and write the fixed-size digest into `buf`. * @param buf - destination buffer for the digest bytes. */ digestInto(buf: TArg): void; /** * Finalize the stream and fill `buf` with XOF output. * @param buf - destination buffer for XOF output. * @returns The filled destination buffer. */ xofInto(buf: TArg): TRet; }; type MergeOpts = [Opts] extends [undefined] ? Out : Opts & Out; /** One-shot hash helper plus streaming constructor and metadata. */ export type HashInstance = Asyncify<(msg: TArg, opts?: MergeOpts) => TRet> & { chunks: Asyncify<(chunks: TArg, opts?: MergeOpts) => TRet>; parallel: Asyncify<(chunks: TArg, opts?: MergeOpts) => TRet>; create: (opts?: CreateOpts) => HashStream; /** Wipe and invalidate an opaque state returned by `create().exportState()`. */ cleanState: (state: TArg) => void; getPlatform: () => string | undefined; getDefinition: () => HashDef; isSupported?: () => boolean | Promise; blockLen: number; outputLen: number; canXOF: boolean; oid?: TRet; }; export declare function mkHash(modFn: () => Mod, def_: TArg>, platform?: string): TRet>; type NobleHashStream = { blockLen: number; outputLen: number; canXOF?: boolean; update(msg: TArg): NobleHashStream; digest(): TRet; digestInto(out: TArg): void; destroy(): void; xof?(bytes: number): TRet; xofInto?(out: TArg): TRet; _cloneInto(to?: NobleHashStream): NobleHashStream; }; type NobleHashImpl = { hash: (msg: TArg, opts?: TArg>) => TRet; create?: (opts?: TArg>) => NobleHashStream; }; export declare function mkHashNoble(def_: TArg>, impl_: TArg>, platform?: string): TRet>; type AsyncHashImpl = { hash: (msg: TArg, opts?: MergeOpts) => Promise>; chunks?: (parts: TArg, opts?: MergeOpts) => Promise>; parallel?: (parts: TArg, opts?: MergeOpts) => Promise>; }; export declare function mkHashAsync(def_: TArg>, impl_: TArg>, platform?: string, isSupported?: () => boolean | Promise, meta?: Record): TRet>; type InstallOpts = { onlyMissing?: boolean; }; /** Installable hash stub used by the `stub` platform. */ export type Stub = { /** * Install a branded hash implementation into this stub. * @param impl - Hash implementation created by an awasm hash constructor. * @param opts - {@link InstallOpts}; `onlyMissing` leaves an existing implementation unchanged. */ install: (impl: HashInstance, opts?: InstallOpts) => void; }; export declare function mkHashStub(def_: TArg>): TRet & Stub>; export {}; //# sourceMappingURL=hashes-abstract.d.ts.map