@decryption/hashes 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -0
  3. package/_blake.d.ts +20 -0
  4. package/_blake.js +56 -0
  5. package/_md.d.ts +98 -0
  6. package/_md.js +207 -0
  7. package/_u64.d.ts +62 -0
  8. package/_u64.js +106 -0
  9. package/argon2.d.ts +111 -0
  10. package/argon2.js +525 -0
  11. package/blake1.d.ts +155 -0
  12. package/blake1.js +599 -0
  13. package/blake2.d.ts +183 -0
  14. package/blake2.js +540 -0
  15. package/blake3.d.ts +67 -0
  16. package/blake3.js +287 -0
  17. package/eskdf.d.ts +91 -0
  18. package/eskdf.js +223 -0
  19. package/esm/_blake.js +51 -0
  20. package/esm/_md.js +201 -0
  21. package/esm/_u64.js +83 -0
  22. package/esm/argon2.js +516 -0
  23. package/esm/blake1.js +559 -0
  24. package/esm/blake2.js +500 -0
  25. package/esm/blake3.js +283 -0
  26. package/esm/eskdf.js +217 -0
  27. package/esm/hkdf.js +119 -0
  28. package/esm/hmac.js +92 -0
  29. package/esm/index.js +34 -0
  30. package/esm/legacy.js +326 -0
  31. package/esm/pbkdf2.js +134 -0
  32. package/esm/scrypt.js +256 -0
  33. package/esm/sha2.js +457 -0
  34. package/esm/sha3-addons.js +782 -0
  35. package/esm/sha3.js +430 -0
  36. package/esm/utils.js +577 -0
  37. package/esm/webcrypto.js +163 -0
  38. package/hkdf.d.ts +63 -0
  39. package/hkdf.js +125 -0
  40. package/hmac.d.ts +46 -0
  41. package/hmac.js +96 -0
  42. package/index.d.ts +0 -0
  43. package/index.js +34 -0
  44. package/legacy.d.ts +101 -0
  45. package/legacy.js +332 -0
  46. package/package.json +47 -0
  47. package/pbkdf2.d.ts +55 -0
  48. package/pbkdf2.js +138 -0
  49. package/scrypt.d.ts +64 -0
  50. package/scrypt.js +260 -0
  51. package/sha2.d.ts +248 -0
  52. package/sha2.js +499 -0
  53. package/sha3-addons.d.ts +460 -0
  54. package/sha3-addons.js +791 -0
  55. package/sha3.d.ts +198 -0
  56. package/sha3.js +435 -0
  57. package/utils.d.ts +518 -0
  58. package/utils.js +606 -0
  59. package/webcrypto.d.ts +119 -0
  60. package/webcrypto.js +168 -0
package/sha3.d.ts ADDED
@@ -0,0 +1,198 @@
1
+ import { type CHash, type CHashXOF, type Hash, type HashXOF, type TArg, type TRet } from './utils';
2
+ /**
3
+ * `keccakf1600` internal permutation, additionally allows adjusting the round count.
4
+ * @param s - 5x5 Keccak state encoded as 25 lanes split into 50 uint32 words
5
+ * in this file's local little-endian lane-word order
6
+ * @param rounds - number of rounds to execute
7
+ * @throws If `rounds` is outside the supported `1..24` range. {@link Error}
8
+ * @example
9
+ * Permute a Keccak state with the default 24 rounds.
10
+ * ```ts
11
+ * keccakP(new Uint32Array(50));
12
+ * ```
13
+ */
14
+ export declare function keccakP(s: TArg<Uint32Array>, rounds?: number): void;
15
+ /**
16
+ * Keccak sponge function.
17
+ * @param blockLen - absorb/squeeze rate in bytes
18
+ * @param suffix - domain separation suffix byte
19
+ * @param outputLen - default digest length in bytes. This base sponge only
20
+ * requires a non-negative integer; wrappers that need positive output
21
+ * lengths must enforce that themselves.
22
+ * @param enableXOF - whether XOF output is allowed
23
+ * @param rounds - number of Keccak-f rounds
24
+ * @example
25
+ * Build a sponge state, absorb bytes, then finalize a digest.
26
+ * ```ts
27
+ * const hash = new Keccak(136, 0x06, 32);
28
+ * hash.update(new Uint8Array([1, 2, 3]));
29
+ * hash.digest();
30
+ * ```
31
+ */
32
+ export declare class Keccak implements Hash<Keccak>, HashXOF<Keccak> {
33
+ protected state: Uint8Array;
34
+ protected pos: number;
35
+ protected posOut: number;
36
+ protected finished: boolean;
37
+ protected state32: Uint32Array;
38
+ protected destroyed: boolean;
39
+ blockLen: number;
40
+ suffix: number;
41
+ outputLen: number;
42
+ canXOF: boolean;
43
+ protected enableXOF: boolean;
44
+ protected rounds: number;
45
+ constructor(blockLen: number, suffix: number, outputLen: number, enableXOF?: boolean, rounds?: number);
46
+ clone(): Keccak;
47
+ protected keccak(): void;
48
+ update(data: TArg<Uint8Array>): this;
49
+ protected finish(): void;
50
+ protected writeInto(out: TArg<Uint8Array>): TRet<Uint8Array>;
51
+ xofInto(out: TArg<Uint8Array>): TRet<Uint8Array>;
52
+ xof(bytes: number): TRet<Uint8Array>;
53
+ digestInto(out: TArg<Uint8Array>): void;
54
+ digest(): TRet<Uint8Array>;
55
+ destroy(): void;
56
+ _cloneInto(to?: Keccak): Keccak;
57
+ }
58
+ /**
59
+ * SHA3-224 hash function.
60
+ * @param msg - message bytes to hash
61
+ * @returns Digest bytes.
62
+ * @example
63
+ * Hash a message with SHA3-224.
64
+ * ```ts
65
+ * sha3_224(new Uint8Array([97, 98, 99]));
66
+ * ```
67
+ */
68
+ export declare const sha3_224: TRet<CHash>;
69
+ /**
70
+ * SHA3-256 hash function. Different from keccak-256.
71
+ * @param msg - message bytes to hash
72
+ * @returns Digest bytes.
73
+ * @example
74
+ * Hash a message with SHA3-256.
75
+ * ```ts
76
+ * sha3_256(new Uint8Array([97, 98, 99]));
77
+ * ```
78
+ */
79
+ export declare const sha3_256: TRet<CHash>;
80
+ /**
81
+ * SHA3-384 hash function.
82
+ * @param msg - message bytes to hash
83
+ * @returns Digest bytes.
84
+ * @example
85
+ * Hash a message with SHA3-384.
86
+ * ```ts
87
+ * sha3_384(new Uint8Array([97, 98, 99]));
88
+ * ```
89
+ */
90
+ export declare const sha3_384: TRet<CHash>;
91
+ /**
92
+ * SHA3-512 hash function.
93
+ * @param msg - message bytes to hash
94
+ * @returns Digest bytes.
95
+ * @example
96
+ * Hash a message with SHA3-512.
97
+ * ```ts
98
+ * sha3_512(new Uint8Array([97, 98, 99]));
99
+ * ```
100
+ */
101
+ export declare const sha3_512: TRet<CHash>;
102
+ /**
103
+ * Keccak-224 hash function.
104
+ * @param msg - message bytes to hash
105
+ * @returns Digest bytes.
106
+ * @example
107
+ * Hash a message with Keccak-224.
108
+ * ```ts
109
+ * keccak_224(new Uint8Array([97, 98, 99]));
110
+ * ```
111
+ */
112
+ export declare const keccak_224: TRet<CHash>;
113
+ /**
114
+ * Keccak-256 hash function. Different from SHA3-256.
115
+ * @param msg - message bytes to hash
116
+ * @returns Digest bytes.
117
+ * @example
118
+ * Hash a message with Keccak-256.
119
+ * ```ts
120
+ * keccak_256(new Uint8Array([97, 98, 99]));
121
+ * ```
122
+ */
123
+ export declare const keccak_256: TRet<CHash>;
124
+ /**
125
+ * Keccak-384 hash function.
126
+ * @param msg - message bytes to hash
127
+ * @returns Digest bytes.
128
+ * @example
129
+ * Hash a message with Keccak-384.
130
+ * ```ts
131
+ * keccak_384(new Uint8Array([97, 98, 99]));
132
+ * ```
133
+ */
134
+ export declare const keccak_384: TRet<CHash>;
135
+ /**
136
+ * Keccak-512 hash function.
137
+ * @param msg - message bytes to hash
138
+ * @returns Digest bytes.
139
+ * @example
140
+ * Hash a message with Keccak-512.
141
+ * ```ts
142
+ * keccak_512(new Uint8Array([97, 98, 99]));
143
+ * ```
144
+ */
145
+ export declare const keccak_512: TRet<CHash>;
146
+ /** Options for SHAKE XOF. */
147
+ export type ShakeOpts = {
148
+ /** Desired number of output bytes. */
149
+ dkLen?: number;
150
+ };
151
+ /**
152
+ * SHAKE128 XOF with 128-bit security and a 16-byte default output.
153
+ * @param msg - message bytes to hash
154
+ * @param opts - Optional output-length override. See {@link ShakeOpts}.
155
+ * @returns Digest bytes.
156
+ * @example
157
+ * Hash a message with SHAKE128.
158
+ * ```ts
159
+ * shake128(new Uint8Array([97, 98, 99]), { dkLen: 32 });
160
+ * ```
161
+ */
162
+ export declare const shake128: TRet<CHashXOF<Keccak, ShakeOpts>>;
163
+ /**
164
+ * SHAKE256 XOF with 256-bit security and a 32-byte default output.
165
+ * @param msg - message bytes to hash
166
+ * @param opts - Optional output-length override. See {@link ShakeOpts}.
167
+ * @returns Digest bytes.
168
+ * @example
169
+ * Hash a message with SHAKE256.
170
+ * ```ts
171
+ * shake256(new Uint8Array([97, 98, 99]), { dkLen: 64 });
172
+ * ```
173
+ */
174
+ export declare const shake256: TRet<CHashXOF<Keccak, ShakeOpts>>;
175
+ /**
176
+ * SHAKE128 XOF with 256-bit output (NIST version).
177
+ * @param msg - message bytes to hash
178
+ * @param opts - Optional output-length override. See {@link ShakeOpts}.
179
+ * @returns Digest bytes.
180
+ * @example
181
+ * Hash a message with SHAKE128 using a 32-byte default output.
182
+ * ```ts
183
+ * shake128_32(new Uint8Array([97, 98, 99]), { dkLen: 32 });
184
+ * ```
185
+ */
186
+ export declare const shake128_32: TRet<CHashXOF<Keccak, ShakeOpts>>;
187
+ /**
188
+ * SHAKE256 XOF with 512-bit output (NIST version).
189
+ * @param msg - message bytes to hash
190
+ * @param opts - Optional output-length override. See {@link ShakeOpts}.
191
+ * @returns Digest bytes.
192
+ * @example
193
+ * Hash a message with SHAKE256 using a 64-byte default output.
194
+ * ```ts
195
+ * shake256_64(new Uint8Array([97, 98, 99]), { dkLen: 64 });
196
+ * ```
197
+ */
198
+ export declare const shake256_64: TRet<CHashXOF<Keccak, ShakeOpts>>;
package/sha3.js ADDED
@@ -0,0 +1,435 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shake256_64 = exports.shake128_32 = exports.shake256 = exports.shake128 = exports.keccak_512 = exports.keccak_384 = exports.keccak_256 = exports.keccak_224 = exports.sha3_512 = exports.sha3_384 = exports.sha3_256 = exports.sha3_224 = exports.Keccak = void 0;
4
+ exports.keccakP = keccakP;
5
+ /**
6
+ * SHA3 (keccak) hash function, based on a new "Sponge function" design.
7
+ * Different from older hashes, the internal state is bigger than output size.
8
+ *
9
+ * Check out
10
+ * {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf | FIPS-202},
11
+ * {@link https://keccak.team/keccak.html | Website}, and
12
+ * {@link https://crypto.stackexchange.com/q/15727 | the differences between
13
+ * SHA-3 and Keccak}.
14
+ *
15
+ * Check out `sha3-addons` module for cSHAKE, k12, and others.
16
+ * @module
17
+ */
18
+ const _u64_1 = require("./_u64");
19
+ // prettier-ignore
20
+ const utils_1 = require("./utils");
21
+ // No __PURE__ annotations in sha3 header:
22
+ // EVERYTHING is in fact used on every export.
23
+ // Various per round constants calculations
24
+ const _0n = BigInt(0);
25
+ const _1n = BigInt(1);
26
+ const _2n = BigInt(2);
27
+ const _7n = BigInt(7);
28
+ const _256n = BigInt(256);
29
+ // FIPS 202 Algorithm 5 rc(): when the outgoing bit is 1, the 8-bit LFSR xors
30
+ // taps 0, 4, 5, and 6, which compresses to the feedback mask `0x71`.
31
+ const _0x71n = BigInt(0x71);
32
+ const SHA3_PI = [];
33
+ const SHA3_ROTL = [];
34
+ const _SHA3_IOTA = []; // no pure annotation: var is always used
35
+ for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
36
+ // Pi
37
+ [x, y] = [y, (2 * x + 3 * y) % 5];
38
+ SHA3_PI.push(2 * (5 * y + x));
39
+ // Rotational
40
+ SHA3_ROTL.push((((round + 1) * (round + 2)) / 2) % 64);
41
+ // Iota
42
+ let t = _0n;
43
+ for (let j = 0; j < 7; j++) {
44
+ R = ((R << _1n) ^ ((R >> _7n) * _0x71n)) % _256n;
45
+ if (R & _2n)
46
+ t ^= _1n << ((_1n << BigInt(j)) - _1n);
47
+ }
48
+ _SHA3_IOTA.push(t);
49
+ }
50
+ const IOTAS = (0, _u64_1.split)(_SHA3_IOTA, true);
51
+ // `split(..., true)` keeps the local little-endian lane-word layout used by
52
+ // `state32`, so these `H` / `L` tables follow the file's first-word /
53
+ // second-word lane slots rather than `_u64.ts`'s usual high/low naming.
54
+ const SHA3_IOTA_H = IOTAS[0];
55
+ const SHA3_IOTA_L = IOTAS[1];
56
+ // Left rotation (without 0, 32, 64)
57
+ const rotlH = (h, l, s) => (s > 32 ? (0, _u64_1.rotlBH)(h, l, s) : (0, _u64_1.rotlSH)(h, l, s));
58
+ const rotlL = (h, l, s) => (s > 32 ? (0, _u64_1.rotlBL)(h, l, s) : (0, _u64_1.rotlSL)(h, l, s));
59
+ /**
60
+ * `keccakf1600` internal permutation, additionally allows adjusting the round count.
61
+ * @param s - 5x5 Keccak state encoded as 25 lanes split into 50 uint32 words
62
+ * in this file's local little-endian lane-word order
63
+ * @param rounds - number of rounds to execute
64
+ * @throws If `rounds` is outside the supported `1..24` range. {@link Error}
65
+ * @example
66
+ * Permute a Keccak state with the default 24 rounds.
67
+ * ```ts
68
+ * keccakP(new Uint32Array(50));
69
+ * ```
70
+ */
71
+ function keccakP(s, rounds = 24) {
72
+ (0, utils_1.anumber)(rounds, 'rounds');
73
+ // This implementation precomputes only the standard Keccak-f[1600] 24-round Iota table.
74
+ if (rounds < 1 || rounds > 24)
75
+ throw new Error('"rounds" expected integer 1..24');
76
+ const B = new Uint32Array(5 * 2);
77
+ // NOTE: all indices are x2 since we store state as u32 instead of u64 (bigints to slow in js)
78
+ for (let round = 24 - rounds; round < 24; round++) {
79
+ // Theta θ
80
+ for (let x = 0; x < 10; x++)
81
+ B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
82
+ for (let x = 0; x < 10; x += 2) {
83
+ const idx1 = (x + 8) % 10;
84
+ const idx0 = (x + 2) % 10;
85
+ const B0 = B[idx0];
86
+ const B1 = B[idx0 + 1];
87
+ const Th = rotlH(B0, B1, 1) ^ B[idx1];
88
+ const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
89
+ for (let y = 0; y < 50; y += 10) {
90
+ s[x + y] ^= Th;
91
+ s[x + y + 1] ^= Tl;
92
+ }
93
+ }
94
+ // Rho (ρ) and Pi (π)
95
+ let curH = s[2];
96
+ let curL = s[3];
97
+ for (let t = 0; t < 24; t++) {
98
+ const shift = SHA3_ROTL[t];
99
+ const Th = rotlH(curH, curL, shift);
100
+ const Tl = rotlL(curH, curL, shift);
101
+ const PI = SHA3_PI[t];
102
+ curH = s[PI];
103
+ curL = s[PI + 1];
104
+ s[PI] = Th;
105
+ s[PI + 1] = Tl;
106
+ }
107
+ // Chi (χ)
108
+ // Same as:
109
+ // for (let x = 0; x < 10; x++) B[x] = s[y + x];
110
+ // for (let x = 0; x < 10; x++) s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
111
+ for (let y = 0; y < 50; y += 10) {
112
+ const b0 = s[y], b1 = s[y + 1], b2 = s[y + 2], b3 = s[y + 3];
113
+ s[y] ^= ~s[y + 2] & s[y + 4];
114
+ s[y + 1] ^= ~s[y + 3] & s[y + 5];
115
+ s[y + 2] ^= ~s[y + 4] & s[y + 6];
116
+ s[y + 3] ^= ~s[y + 5] & s[y + 7];
117
+ s[y + 4] ^= ~s[y + 6] & s[y + 8];
118
+ s[y + 5] ^= ~s[y + 7] & s[y + 9];
119
+ s[y + 6] ^= ~s[y + 8] & b0;
120
+ s[y + 7] ^= ~s[y + 9] & b1;
121
+ s[y + 8] ^= ~b0 & b2;
122
+ s[y + 9] ^= ~b1 & b3;
123
+ }
124
+ // Iota (ι)
125
+ s[0] ^= SHA3_IOTA_H[round];
126
+ s[1] ^= SHA3_IOTA_L[round];
127
+ }
128
+ (0, utils_1.clean)(B);
129
+ }
130
+ /**
131
+ * Keccak sponge function.
132
+ * @param blockLen - absorb/squeeze rate in bytes
133
+ * @param suffix - domain separation suffix byte
134
+ * @param outputLen - default digest length in bytes. This base sponge only
135
+ * requires a non-negative integer; wrappers that need positive output
136
+ * lengths must enforce that themselves.
137
+ * @param enableXOF - whether XOF output is allowed
138
+ * @param rounds - number of Keccak-f rounds
139
+ * @example
140
+ * Build a sponge state, absorb bytes, then finalize a digest.
141
+ * ```ts
142
+ * const hash = new Keccak(136, 0x06, 32);
143
+ * hash.update(new Uint8Array([1, 2, 3]));
144
+ * hash.digest();
145
+ * ```
146
+ */
147
+ class Keccak {
148
+ state;
149
+ pos = 0;
150
+ posOut = 0;
151
+ finished = false;
152
+ state32;
153
+ destroyed = false;
154
+ blockLen;
155
+ suffix;
156
+ outputLen;
157
+ canXOF;
158
+ enableXOF = false;
159
+ rounds;
160
+ // NOTE: we accept arguments in bytes instead of bits here.
161
+ constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
162
+ this.blockLen = blockLen;
163
+ this.suffix = suffix;
164
+ this.outputLen = outputLen;
165
+ this.enableXOF = enableXOF;
166
+ this.canXOF = enableXOF;
167
+ this.rounds = rounds;
168
+ // Can be passed from user as dkLen
169
+ (0, utils_1.anumber)(outputLen, 'outputLen');
170
+ // 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
171
+ // 0 < blockLen < 200
172
+ if (!(0 < blockLen && blockLen < 200))
173
+ throw new Error('only keccak-f1600 function is supported');
174
+ this.state = new Uint8Array(200);
175
+ this.state32 = (0, utils_1.u32)(this.state);
176
+ }
177
+ clone() {
178
+ return this._cloneInto();
179
+ }
180
+ keccak() {
181
+ (0, utils_1.swap32IfBE)(this.state32);
182
+ keccakP(this.state32, this.rounds);
183
+ (0, utils_1.swap32IfBE)(this.state32);
184
+ this.posOut = 0;
185
+ this.pos = 0;
186
+ }
187
+ update(data) {
188
+ (0, utils_1.aexists)(this);
189
+ (0, utils_1.abytes)(data);
190
+ const { blockLen, state } = this;
191
+ const len = data.length;
192
+ for (let pos = 0; pos < len;) {
193
+ const take = Math.min(blockLen - this.pos, len - pos);
194
+ for (let i = 0; i < take; i++)
195
+ state[this.pos++] ^= data[pos++];
196
+ if (this.pos === blockLen)
197
+ this.keccak();
198
+ }
199
+ return this;
200
+ }
201
+ finish() {
202
+ if (this.finished)
203
+ return;
204
+ this.finished = true;
205
+ const { state, suffix, pos, blockLen } = this;
206
+ // FIPS 202 appends the SHA3/SHAKE domain-separation suffix before pad10*1.
207
+ // These byte values already include the first padding bit, while the
208
+ // final `0x80` below supplies the closing `1` bit in the last rate byte.
209
+ state[pos] ^= suffix;
210
+ // If that combined suffix lands in the last rate byte and already sets
211
+ // bit 7, absorb it first so the final pad10*1 bit can be xored into a
212
+ // fresh block.
213
+ if ((suffix & 0x80) !== 0 && pos === blockLen - 1)
214
+ this.keccak();
215
+ state[blockLen - 1] ^= 0x80;
216
+ this.keccak();
217
+ }
218
+ writeInto(out) {
219
+ (0, utils_1.aexists)(this, false);
220
+ (0, utils_1.abytes)(out);
221
+ this.finish();
222
+ const bufferOut = this.state;
223
+ const { blockLen } = this;
224
+ for (let pos = 0, len = out.length; pos < len;) {
225
+ if (this.posOut >= blockLen)
226
+ this.keccak();
227
+ const take = Math.min(blockLen - this.posOut, len - pos);
228
+ out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
229
+ this.posOut += take;
230
+ pos += take;
231
+ }
232
+ return out;
233
+ }
234
+ xofInto(out) {
235
+ // Plain SHA3/Keccak usage with XOF is probably a mistake, but this base
236
+ // class is also reused by SHAKE/cSHAKE/KMAC/TupleHash/ParallelHash/
237
+ // TurboSHAKE/KangarooTwelve wrappers that intentionally enable XOF.
238
+ if (!this.enableXOF)
239
+ throw new Error('XOF is not possible for this instance');
240
+ return this.writeInto(out);
241
+ }
242
+ xof(bytes) {
243
+ (0, utils_1.anumber)(bytes);
244
+ return this.xofInto(new Uint8Array(bytes));
245
+ }
246
+ digestInto(out) {
247
+ (0, utils_1.aoutput)(out, this);
248
+ if (this.finished)
249
+ throw new Error('digest() was already called');
250
+ // `aoutput(...)` allows oversized buffers; digestInto() must fill only the advertised digest.
251
+ this.writeInto(out.subarray(0, this.outputLen));
252
+ this.destroy();
253
+ }
254
+ digest() {
255
+ const out = new Uint8Array(this.outputLen);
256
+ this.digestInto(out);
257
+ return out;
258
+ }
259
+ destroy() {
260
+ this.destroyed = true;
261
+ (0, utils_1.clean)(this.state);
262
+ }
263
+ _cloneInto(to) {
264
+ const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
265
+ to ||= new Keccak(blockLen, suffix, outputLen, enableXOF, rounds);
266
+ // Reused destinations can come from a different rate/capacity variant, so clone must rewrite
267
+ // the sponge geometry as well as the state words.
268
+ to.blockLen = blockLen;
269
+ to.state32.set(this.state32);
270
+ to.pos = this.pos;
271
+ to.posOut = this.posOut;
272
+ to.finished = this.finished;
273
+ to.rounds = rounds;
274
+ // Suffix can change in cSHAKE
275
+ to.suffix = suffix;
276
+ to.outputLen = outputLen;
277
+ to.enableXOF = enableXOF;
278
+ // Clones must preserve the public capability bit too; `_KMAC` reuses this path and deep clone
279
+ // tests compare instance fields directly, so leaving `canXOF` behind makes the clone lie.
280
+ to.canXOF = this.canXOF;
281
+ to.destroyed = this.destroyed;
282
+ return to;
283
+ }
284
+ }
285
+ exports.Keccak = Keccak;
286
+ const genKeccak = (suffix, blockLen, outputLen, info = {}) => (0, utils_1.createHasher)(() => new Keccak(blockLen, suffix, outputLen), info);
287
+ /**
288
+ * SHA3-224 hash function.
289
+ * @param msg - message bytes to hash
290
+ * @returns Digest bytes.
291
+ * @example
292
+ * Hash a message with SHA3-224.
293
+ * ```ts
294
+ * sha3_224(new Uint8Array([97, 98, 99]));
295
+ * ```
296
+ */
297
+ exports.sha3_224 = genKeccak(0x06, 144, 28,
298
+ /* @__PURE__ */ (0, utils_1.oidNist)(0x07));
299
+ /**
300
+ * SHA3-256 hash function. Different from keccak-256.
301
+ * @param msg - message bytes to hash
302
+ * @returns Digest bytes.
303
+ * @example
304
+ * Hash a message with SHA3-256.
305
+ * ```ts
306
+ * sha3_256(new Uint8Array([97, 98, 99]));
307
+ * ```
308
+ */
309
+ exports.sha3_256 = genKeccak(0x06, 136, 32,
310
+ /* @__PURE__ */ (0, utils_1.oidNist)(0x08));
311
+ /**
312
+ * SHA3-384 hash function.
313
+ * @param msg - message bytes to hash
314
+ * @returns Digest bytes.
315
+ * @example
316
+ * Hash a message with SHA3-384.
317
+ * ```ts
318
+ * sha3_384(new Uint8Array([97, 98, 99]));
319
+ * ```
320
+ */
321
+ exports.sha3_384 = genKeccak(0x06, 104, 48,
322
+ /* @__PURE__ */ (0, utils_1.oidNist)(0x09));
323
+ /**
324
+ * SHA3-512 hash function.
325
+ * @param msg - message bytes to hash
326
+ * @returns Digest bytes.
327
+ * @example
328
+ * Hash a message with SHA3-512.
329
+ * ```ts
330
+ * sha3_512(new Uint8Array([97, 98, 99]));
331
+ * ```
332
+ */
333
+ exports.sha3_512 = genKeccak(0x06, 72, 64,
334
+ /* @__PURE__ */ (0, utils_1.oidNist)(0x0a));
335
+ /**
336
+ * Keccak-224 hash function.
337
+ * @param msg - message bytes to hash
338
+ * @returns Digest bytes.
339
+ * @example
340
+ * Hash a message with Keccak-224.
341
+ * ```ts
342
+ * keccak_224(new Uint8Array([97, 98, 99]));
343
+ * ```
344
+ */
345
+ exports.keccak_224 = genKeccak(0x01, 144, 28);
346
+ /**
347
+ * Keccak-256 hash function. Different from SHA3-256.
348
+ * @param msg - message bytes to hash
349
+ * @returns Digest bytes.
350
+ * @example
351
+ * Hash a message with Keccak-256.
352
+ * ```ts
353
+ * keccak_256(new Uint8Array([97, 98, 99]));
354
+ * ```
355
+ */
356
+ exports.keccak_256 = genKeccak(0x01, 136, 32);
357
+ /**
358
+ * Keccak-384 hash function.
359
+ * @param msg - message bytes to hash
360
+ * @returns Digest bytes.
361
+ * @example
362
+ * Hash a message with Keccak-384.
363
+ * ```ts
364
+ * keccak_384(new Uint8Array([97, 98, 99]));
365
+ * ```
366
+ */
367
+ exports.keccak_384 = genKeccak(0x01, 104, 48);
368
+ /**
369
+ * Keccak-512 hash function.
370
+ * @param msg - message bytes to hash
371
+ * @returns Digest bytes.
372
+ * @example
373
+ * Hash a message with Keccak-512.
374
+ * ```ts
375
+ * keccak_512(new Uint8Array([97, 98, 99]));
376
+ * ```
377
+ */
378
+ exports.keccak_512 = genKeccak(0x01, 72, 64);
379
+ const genShake = (suffix, blockLen, outputLen, info = {}) => (0, utils_1.createHasher)((opts = {}) => new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true), info);
380
+ /**
381
+ * SHAKE128 XOF with 128-bit security and a 16-byte default output.
382
+ * @param msg - message bytes to hash
383
+ * @param opts - Optional output-length override. See {@link ShakeOpts}.
384
+ * @returns Digest bytes.
385
+ * @example
386
+ * Hash a message with SHAKE128.
387
+ * ```ts
388
+ * shake128(new Uint8Array([97, 98, 99]), { dkLen: 32 });
389
+ * ```
390
+ */
391
+ exports.shake128 =
392
+ /* @__PURE__ */
393
+ genShake(0x1f, 168, 16, /* @__PURE__ */ (0, utils_1.oidNist)(0x0b));
394
+ /**
395
+ * SHAKE256 XOF with 256-bit security and a 32-byte default output.
396
+ * @param msg - message bytes to hash
397
+ * @param opts - Optional output-length override. See {@link ShakeOpts}.
398
+ * @returns Digest bytes.
399
+ * @example
400
+ * Hash a message with SHAKE256.
401
+ * ```ts
402
+ * shake256(new Uint8Array([97, 98, 99]), { dkLen: 64 });
403
+ * ```
404
+ */
405
+ exports.shake256 =
406
+ /* @__PURE__ */
407
+ genShake(0x1f, 136, 32, /* @__PURE__ */ (0, utils_1.oidNist)(0x0c));
408
+ /**
409
+ * SHAKE128 XOF with 256-bit output (NIST version).
410
+ * @param msg - message bytes to hash
411
+ * @param opts - Optional output-length override. See {@link ShakeOpts}.
412
+ * @returns Digest bytes.
413
+ * @example
414
+ * Hash a message with SHAKE128 using a 32-byte default output.
415
+ * ```ts
416
+ * shake128_32(new Uint8Array([97, 98, 99]), { dkLen: 32 });
417
+ * ```
418
+ */
419
+ exports.shake128_32 =
420
+ /* @__PURE__ */
421
+ genShake(0x1f, 168, 32, /* @__PURE__ */ (0, utils_1.oidNist)(0x0b));
422
+ /**
423
+ * SHAKE256 XOF with 512-bit output (NIST version).
424
+ * @param msg - message bytes to hash
425
+ * @param opts - Optional output-length override. See {@link ShakeOpts}.
426
+ * @returns Digest bytes.
427
+ * @example
428
+ * Hash a message with SHAKE256 using a 64-byte default output.
429
+ * ```ts
430
+ * shake256_64(new Uint8Array([97, 98, 99]), { dkLen: 64 });
431
+ * ```
432
+ */
433
+ exports.shake256_64 =
434
+ /* @__PURE__ */
435
+ genShake(0x1f, 136, 64, /* @__PURE__ */ (0, utils_1.oidNist)(0x0c));