UNPKG

functionalscript

Version:

FunctionalScript is a purely functional subset of JavaScript

87 lines (86 loc) 2.45 kB
import type { Array16, Array8 } from '../../types/array/module.f.ts'; import { type Vec } from '../../types/bit_vec/module.f.ts'; import { type List } from '../../types/list/module.f.ts'; export type V8 = Array8<bigint>; export type V16 = Array16<bigint>; /** * Type definition for the state of the SHA-2 algorithm. */ export type State = { /** * The current hash value. */ readonly hash: V8; /** * The length of the data processed so far. */ readonly len: bigint; /** * The remaining data that has not yet been processed. */ readonly remainder: Vec; }; export type Base = { readonly bitLength: bigint; readonly chunkLength: bigint; readonly compress: (i: V8) => (u: bigint) => V8; readonly fromV8: (a: V8) => bigint; readonly append: (state: State) => (v: Vec) => State; readonly end: (hashLength: bigint) => (state: State) => bigint; }; /** * SHA2. See https://en.wikipedia.org/wiki/SHA-2 * * @example * * ```js * const s = msbUtf8("The quick brown fox jumps over the lazy dog.") * let state = sha224.init * state = sha224.append(state)(s) * const h = sha224.end(state) // 0x619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4cn * ``` */ export type Sha2 = { /** * A hash length. */ readonly hashLength: bigint; /** * An internal block length. */ readonly blockLength: bigint; /** * Initial state of the SHA-2 algorithm. */ readonly init: State; /** * Appends data to the state and returns the new state. * * @param state The current state. * @param v The data to append. * @returns The new state after appending data. */ readonly append: (state: State) => (v: Vec) => State; /** * Finalizes the hash and returns the result as a bigint. * * @param state The final state. * @returns The resulting hash. */ readonly end: (state: State) => bigint; }; export declare const computeSync: ({ append, init, end }: Sha2) => (list: List<Vec>) => Vec; export declare const base32: Base; export declare const base64: Base; /** SHA-256 */ export declare const sha256: Sha2; /** SHA-224 */ export declare const sha224: Sha2; /** SHA-512 */ export declare const sha512: Sha2; /** SHA-384 */ export declare const sha384: Sha2; /** SHA-512/256 */ export declare const sha512x256: Sha2; /** SHA-512/224 */ export declare const sha512x224: Sha2;