@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/blake1.js ADDED
@@ -0,0 +1,599 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.blake512 = exports.blake384 = exports.blake256 = exports.blake224 = exports._BLAKE512 = exports._BLAKE384 = exports._BLAKE256 = exports._BLAKE224 = void 0;
37
+ /**
38
+ * Blake1 legacy hash function, one of SHA3 proposals.
39
+ * Rarely used. Check out blake2 or blake3 instead.
40
+ * {@link https://www.aumasson.jp/blake/blake.pdf}
41
+ *
42
+ * In the best case, there are 0 allocations.
43
+ *
44
+ * Differences from blake2:
45
+ *
46
+ * - BE instead of LE
47
+ * - Paddings, similar to MD5, RIPEMD, SHA1, SHA2, but:
48
+ * - length flag is located before actual length
49
+ * - padding block is compressed differently (no lengths)
50
+ * Instead of msg[sigma[k]], we have `msg[sigma[k]] ^ constants[sigma[k-1]]`
51
+ * (-1 for g1, g2 without -1)
52
+ * - Salt is XOR-ed into constants instead of state
53
+ * - Salt is XOR-ed with output in `compress`
54
+ * - Additional rows (+64 bytes) in SIGMA for new rounds
55
+ * - Different round count:
56
+ * - 14 / 10 rounds in blake256 / blake2s
57
+ * - 16 / 12 rounds in blake512 / blake2b
58
+ * - blake512: G1b: rotr 24 -> 25, G2b: rotr 63 -> 11
59
+ * @module
60
+ */
61
+ const _blake_1 = require("./_blake");
62
+ const _md_1 = require("./_md");
63
+ const u64 = __importStar(require("./_u64"));
64
+ // prettier-ignore
65
+ const utils_1 = require("./utils");
66
+ // Shared unsalted sentinel, sized for the 64-bit path and reused by the 32-bit path via prefix.
67
+ const EMPTY_SALT = /* @__PURE__ */ new Uint32Array(8);
68
+ // Base destroy logic only clears salt-derived state; the partial message buffer and length/position
69
+ // bookkeeping remain until the instance or backing buffer is reused.
70
+ class BLAKE1 {
71
+ canXOF = false;
72
+ finished = false;
73
+ length = 0;
74
+ pos = 0;
75
+ destroyed = false;
76
+ // For partial updates less than block size
77
+ buffer;
78
+ view;
79
+ salt;
80
+ blockLen;
81
+ outputLen;
82
+ lengthFlag;
83
+ counterLen;
84
+ constants;
85
+ constructor(blockLen, outputLen, lengthFlag, counterLen, saltLen, constants, opts = {}) {
86
+ const { salt } = opts;
87
+ this.blockLen = blockLen;
88
+ this.outputLen = outputLen;
89
+ this.lengthFlag = lengthFlag;
90
+ this.counterLen = counterLen;
91
+ this.buffer = new Uint8Array(blockLen);
92
+ this.view = (0, utils_1.createView)(this.buffer);
93
+ if (salt !== undefined) {
94
+ let slt = salt;
95
+ (0, utils_1.abytes)(slt, 4 * saltLen, 'salt');
96
+ // if (slt.length !== 4 * saltLen) throw new Error('wrong salt length');
97
+ const salt32 = (this.salt = new Uint32Array(saltLen));
98
+ const sv = (0, utils_1.createView)(slt);
99
+ this.constants = constants.slice();
100
+ for (let i = 0, offset = 0; i < salt32.length; i++, offset += 4) {
101
+ salt32[i] = sv.getUint32(offset, false);
102
+ this.constants[i] ^= salt32[i];
103
+ }
104
+ }
105
+ else {
106
+ this.salt = EMPTY_SALT;
107
+ this.constants = constants;
108
+ }
109
+ }
110
+ update(data) {
111
+ (0, utils_1.aexists)(this);
112
+ (0, utils_1.abytes)(data);
113
+ // From _md, but update length before each compress
114
+ const { view, buffer, blockLen } = this;
115
+ const len = data.length;
116
+ let dataView;
117
+ for (let pos = 0; pos < len;) {
118
+ const take = Math.min(blockLen - this.pos, len - pos);
119
+ // Fast path only when there is no buffered partial block: `take === blockLen` implies
120
+ // `this.pos === 0`, so we can process full blocks directly from the input view.
121
+ if (take === blockLen) {
122
+ if (!dataView)
123
+ dataView = (0, utils_1.createView)(data);
124
+ for (; blockLen <= len - pos; pos += blockLen) {
125
+ this.length += blockLen;
126
+ this.compress(dataView, pos);
127
+ }
128
+ continue;
129
+ }
130
+ buffer.set(data.subarray(pos, pos + take), this.pos);
131
+ this.pos += take;
132
+ pos += take;
133
+ if (this.pos === blockLen) {
134
+ this.length += blockLen;
135
+ this.compress(view, 0, true);
136
+ this.pos = 0;
137
+ }
138
+ }
139
+ return this;
140
+ }
141
+ destroy() {
142
+ this.destroyed = true;
143
+ if (this.salt !== EMPTY_SALT) {
144
+ (0, utils_1.clean)(this.salt, this.constants);
145
+ }
146
+ }
147
+ _cloneInto(to) {
148
+ to ||= new this.constructor();
149
+ to.set(...this.get());
150
+ const { buffer, length, finished, destroyed, constants, salt, pos } = this;
151
+ to.buffer.set(buffer);
152
+ // Clone salt-derived arrays by value so destroying the clone cannot wipe the source instance.
153
+ to.constants = constants.slice();
154
+ to.destroyed = destroyed;
155
+ to.finished = finished;
156
+ to.length = length;
157
+ to.pos = pos;
158
+ to.salt = salt.slice();
159
+ return to;
160
+ }
161
+ clone() {
162
+ return this._cloneInto();
163
+ }
164
+ digestInto(out) {
165
+ (0, utils_1.aexists)(this);
166
+ (0, utils_1.aoutput)(out, this);
167
+ this.finished = true;
168
+ // Padding
169
+ const { buffer, blockLen, counterLen, lengthFlag, view } = this;
170
+ (0, utils_1.clean)(buffer.subarray(this.pos)); // clean buf
171
+ const counter = BigInt((this.length + this.pos) * 8);
172
+ const counterPos = blockLen - counterLen - 1;
173
+ buffer[this.pos] |= 0b1000_0000; // End block flag
174
+ this.length += this.pos; // add unwritten length
175
+ // Not enough in buffer for length: write what we have.
176
+ if (this.pos > counterPos) {
177
+ this.compress(view, 0);
178
+ (0, utils_1.clean)(buffer);
179
+ this.pos = 0;
180
+ }
181
+ // Difference with md: here we have lengthFlag!
182
+ buffer[counterPos] |= lengthFlag; // Length flag
183
+ // We always set 8 byte length flag. Because length will overflow significantly sooner.
184
+ view.setBigUint64(blockLen - 8, counter, false);
185
+ // Blake1 omits the counter from the extra all-padding block; only the block that still carries
186
+ // message bytes mixes in the final bit length.
187
+ this.compress(view, 0, this.pos !== 0);
188
+ // Write output
189
+ (0, utils_1.clean)(buffer);
190
+ const v = (0, utils_1.createView)(out);
191
+ const state = this.get();
192
+ for (let i = 0; i < this.outputLen / 4; ++i)
193
+ v.setUint32(i * 4, state[i]);
194
+ }
195
+ digest() {
196
+ const { buffer, outputLen } = this;
197
+ this.digestInto(buffer);
198
+ // Return a copy so callers do not alias the instance scratch buffer used during finalization.
199
+ const res = buffer.slice(0, outputLen);
200
+ this.destroy();
201
+ return res;
202
+ }
203
+ }
204
+ // Blake1-512 / Blake1-384 constant table `C512`.
205
+ // Stored as sixteen 64-bit constants split into `[high32, low32]` halves so
206
+ // the Blake1-64 path can reuse one layout for both `v8..v15` initialization
207
+ // and the permuted constant lookups.
208
+ const B64C = /* @__PURE__ */ Uint32Array.from([
209
+ 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
210
+ 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
211
+ 0x9216d5d9, 0x8979fb1b, 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
212
+ 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69,
213
+ ]);
214
+ // Blake1-256 / Blake1-224 constant table `C256`, derived as the first half of `C512`.
215
+ const B32C = /* @__PURE__ */ B64C.slice(0, 16);
216
+ // Blake1-256 IV cloned from SHA-256.
217
+ const B256_IV = /* @__PURE__ */ _md_1.SHA256_IV.slice();
218
+ // Blake1-224 IV cloned from SHA-224.
219
+ const B224_IV = /* @__PURE__ */ _md_1.SHA224_IV.slice();
220
+ // Blake1-384 IV cloned from the SHA-384 high-then-low 32-bit halves.
221
+ const B384_IV = /* @__PURE__ */ _md_1.SHA384_IV.slice();
222
+ // Blake1-512 IV cloned from the SHA-512 high-then-low 32-bit halves.
223
+ const B512_IV = /* @__PURE__ */ _md_1.SHA512_IV.slice();
224
+ // Precompute the odd/even companion constants used by all 14 Blake1-32 rounds.
225
+ // Each pair stores `u[sigma[2i + 1]]` then `u[sigma[2i]]`, matching the `G1s` / `G2s` xor order.
226
+ function generateTBL256() {
227
+ const TBL = [];
228
+ for (let i = 0, j = 0; i < 14; i++, j += 16) {
229
+ for (let offset = 1; offset < 16; offset += 2) {
230
+ TBL.push(B32C[_blake_1.BSIGMA[j + offset]]);
231
+ TBL.push(B32C[_blake_1.BSIGMA[j + offset - 1]]);
232
+ }
233
+ }
234
+ return new Uint32Array(TBL);
235
+ }
236
+ // Full 14-round companion-constant table for Blake1-32.
237
+ const TBL256 = /* @__PURE__ */ generateTBL256();
238
+ // Shared synchronous message-word scratch for the 32-bit Blake1 path.
239
+ const BLAKE256_W = /* @__PURE__ */ new Uint32Array(16);
240
+ class BLAKE1_32B extends BLAKE1 {
241
+ v0;
242
+ v1;
243
+ v2;
244
+ v3;
245
+ v4;
246
+ v5;
247
+ v6;
248
+ v7;
249
+ constructor(outputLen, IV, lengthFlag, opts = {}) {
250
+ super(64, outputLen, lengthFlag, 8, 4, B32C, opts);
251
+ this.v0 = IV[0] | 0;
252
+ this.v1 = IV[1] | 0;
253
+ this.v2 = IV[2] | 0;
254
+ this.v3 = IV[3] | 0;
255
+ this.v4 = IV[4] | 0;
256
+ this.v5 = IV[5] | 0;
257
+ this.v6 = IV[6] | 0;
258
+ this.v7 = IV[7] | 0;
259
+ }
260
+ get() {
261
+ const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
262
+ return [v0, v1, v2, v3, v4, v5, v6, v7];
263
+ }
264
+ // prettier-ignore
265
+ set(v0, v1, v2, v3, v4, v5, v6, v7) {
266
+ this.v0 = v0 | 0;
267
+ this.v1 = v1 | 0;
268
+ this.v2 = v2 | 0;
269
+ this.v3 = v3 | 0;
270
+ this.v4 = v4 | 0;
271
+ this.v5 = v5 | 0;
272
+ this.v6 = v6 | 0;
273
+ this.v7 = v7 | 0;
274
+ }
275
+ destroy() {
276
+ super.destroy();
277
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
278
+ }
279
+ compress(view, offset, withLength = true) {
280
+ for (let i = 0; i < 16; i++, offset += 4)
281
+ BLAKE256_W[i] = view.getUint32(offset, false);
282
+ // Cannot reuse blake2s compress: Blake1 mixes each message word with the companion constants
283
+ // precomputed in `TBL256`, rather than using the raw schedule words directly.
284
+ let v00 = this.v0 | 0;
285
+ let v01 = this.v1 | 0;
286
+ let v02 = this.v2 | 0;
287
+ let v03 = this.v3 | 0;
288
+ let v04 = this.v4 | 0;
289
+ let v05 = this.v5 | 0;
290
+ let v06 = this.v6 | 0;
291
+ let v07 = this.v7 | 0;
292
+ let v08 = this.constants[0] | 0;
293
+ let v09 = this.constants[1] | 0;
294
+ let v10 = this.constants[2] | 0;
295
+ let v11 = this.constants[3] | 0;
296
+ // Blake1-32 injects the 64-bit bit counter as `[t0, t0, t1, t1]` across `v12..v15`; the
297
+ // final all-padding block passes `withLength = false`, leaving these lanes as raw constants.
298
+ const { h, l } = u64.fromBig(BigInt(withLength ? this.length * 8 : 0));
299
+ let v12 = (this.constants[4] ^ l) >>> 0;
300
+ let v13 = (this.constants[5] ^ l) >>> 0;
301
+ let v14 = (this.constants[6] ^ h) >>> 0;
302
+ let v15 = (this.constants[7] ^ h) >>> 0;
303
+ // prettier-ignore
304
+ for (let i = 0, k = 0, j = 0; i < 14; i++) {
305
+ ({ a: v00, b: v04, c: v08, d: v12 } = (0, _blake_1.G1s)(v00, v04, v08, v12, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
306
+ ({ a: v00, b: v04, c: v08, d: v12 } = (0, _blake_1.G2s)(v00, v04, v08, v12, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
307
+ ({ a: v01, b: v05, c: v09, d: v13 } = (0, _blake_1.G1s)(v01, v05, v09, v13, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
308
+ ({ a: v01, b: v05, c: v09, d: v13 } = (0, _blake_1.G2s)(v01, v05, v09, v13, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
309
+ ({ a: v02, b: v06, c: v10, d: v14 } = (0, _blake_1.G1s)(v02, v06, v10, v14, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
310
+ ({ a: v02, b: v06, c: v10, d: v14 } = (0, _blake_1.G2s)(v02, v06, v10, v14, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
311
+ ({ a: v03, b: v07, c: v11, d: v15 } = (0, _blake_1.G1s)(v03, v07, v11, v15, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
312
+ ({ a: v03, b: v07, c: v11, d: v15 } = (0, _blake_1.G2s)(v03, v07, v11, v15, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
313
+ ({ a: v00, b: v05, c: v10, d: v15 } = (0, _blake_1.G1s)(v00, v05, v10, v15, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
314
+ ({ a: v00, b: v05, c: v10, d: v15 } = (0, _blake_1.G2s)(v00, v05, v10, v15, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
315
+ ({ a: v01, b: v06, c: v11, d: v12 } = (0, _blake_1.G1s)(v01, v06, v11, v12, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
316
+ ({ a: v01, b: v06, c: v11, d: v12 } = (0, _blake_1.G2s)(v01, v06, v11, v12, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
317
+ ({ a: v02, b: v07, c: v08, d: v13 } = (0, _blake_1.G1s)(v02, v07, v08, v13, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
318
+ ({ a: v02, b: v07, c: v08, d: v13 } = (0, _blake_1.G2s)(v02, v07, v08, v13, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
319
+ ({ a: v03, b: v04, c: v09, d: v14 } = (0, _blake_1.G1s)(v03, v04, v09, v14, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
320
+ ({ a: v03, b: v04, c: v09, d: v14 } = (0, _blake_1.G2s)(v03, v04, v09, v14, BLAKE256_W[_blake_1.BSIGMA[k++]] ^ TBL256[j++]));
321
+ }
322
+ this.v0 = (this.v0 ^ v00 ^ v08 ^ this.salt[0]) >>> 0;
323
+ this.v1 = (this.v1 ^ v01 ^ v09 ^ this.salt[1]) >>> 0;
324
+ this.v2 = (this.v2 ^ v02 ^ v10 ^ this.salt[2]) >>> 0;
325
+ this.v3 = (this.v3 ^ v03 ^ v11 ^ this.salt[3]) >>> 0;
326
+ this.v4 = (this.v4 ^ v04 ^ v12 ^ this.salt[0]) >>> 0;
327
+ this.v5 = (this.v5 ^ v05 ^ v13 ^ this.salt[1]) >>> 0;
328
+ this.v6 = (this.v6 ^ v06 ^ v14 ^ this.salt[2]) >>> 0;
329
+ this.v7 = (this.v7 ^ v07 ^ v15 ^ this.salt[3]) >>> 0;
330
+ (0, utils_1.clean)(BLAKE256_W);
331
+ }
332
+ }
333
+ // Shared Blake1-64 work vector storing 16 working words as adjacent high/low 32-bit halves.
334
+ const BBUF = /* @__PURE__ */ new Uint32Array(32);
335
+ // Shared synchronous message-word scratch for the 64-bit Blake1 path.
336
+ const BLAKE512_W = /* @__PURE__ */ new Uint32Array(32);
337
+ // Precompute the high/low companion constants used by all 16 Blake1-64 rounds.
338
+ // Each quartet stores `u[sigma[2i + 1]]` high/low halves, then `u[sigma[2i]]` high/low halves.
339
+ function generateTBL512() {
340
+ const TBL = [];
341
+ for (let r = 0, k = 0; r < 16; r++, k += 16) {
342
+ for (let offset = 1; offset < 16; offset += 2) {
343
+ TBL.push(B64C[_blake_1.BSIGMA[k + offset] * 2 + 0]);
344
+ TBL.push(B64C[_blake_1.BSIGMA[k + offset] * 2 + 1]);
345
+ TBL.push(B64C[_blake_1.BSIGMA[k + offset - 1] * 2 + 0]);
346
+ TBL.push(B64C[_blake_1.BSIGMA[k + offset - 1] * 2 + 1]);
347
+ }
348
+ }
349
+ return new Uint32Array(TBL);
350
+ }
351
+ // Full 16-round companion-constant table as high/low halves.
352
+ const TBL512 = /* @__PURE__ */ generateTBL512();
353
+ // Blake1-64 first half-round with rotations `32` and `25`; `k` is the half-call schedule index.
354
+ function G1b(a, b, c, d, msg, k) {
355
+ const Xpos = 2 * _blake_1.BSIGMA[k];
356
+ const Xl = msg[Xpos + 1] ^ TBL512[k * 2 + 1], Xh = msg[Xpos] ^ TBL512[k * 2]; // prettier-ignore
357
+ let Al = BBUF[2 * a + 1], Ah = BBUF[2 * a]; // prettier-ignore
358
+ let Bl = BBUF[2 * b + 1], Bh = BBUF[2 * b]; // prettier-ignore
359
+ let Cl = BBUF[2 * c + 1], Ch = BBUF[2 * c]; // prettier-ignore
360
+ let Dl = BBUF[2 * d + 1], Dh = BBUF[2 * d]; // prettier-ignore
361
+ // v[a] = (v[a] + v[b] + x) | 0;
362
+ let ll = u64.add3L(Al, Bl, Xl);
363
+ Ah = u64.add3H(ll, Ah, Bh, Xh) >>> 0;
364
+ Al = (ll | 0) >>> 0;
365
+ // v[d] = rotr(v[d] ^ v[a], 32)
366
+ ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
367
+ ({ Dh, Dl } = { Dh: u64.rotr32H(Dh, Dl), Dl: u64.rotr32L(Dh, Dl) });
368
+ // v[c] = (v[c] + v[d]) | 0;
369
+ ({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
370
+ // v[b] = rotr(v[b] ^ v[c], 25)
371
+ ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
372
+ ({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 25), Bl: u64.rotrSL(Bh, Bl, 25) });
373
+ ((BBUF[2 * a + 1] = Al), (BBUF[2 * a] = Ah));
374
+ ((BBUF[2 * b + 1] = Bl), (BBUF[2 * b] = Bh));
375
+ ((BBUF[2 * c + 1] = Cl), (BBUF[2 * c] = Ch));
376
+ ((BBUF[2 * d + 1] = Dl), (BBUF[2 * d] = Dh));
377
+ }
378
+ // Blake1-64 second half-round with rotations `16` and `11`; `k` is the half-call schedule index.
379
+ function G2b(a, b, c, d, msg, k) {
380
+ const Xpos = 2 * _blake_1.BSIGMA[k];
381
+ const Xl = msg[Xpos + 1] ^ TBL512[k * 2 + 1], Xh = msg[Xpos] ^ TBL512[k * 2]; // prettier-ignore
382
+ let Al = BBUF[2 * a + 1], Ah = BBUF[2 * a]; // prettier-ignore
383
+ let Bl = BBUF[2 * b + 1], Bh = BBUF[2 * b]; // prettier-ignore
384
+ let Cl = BBUF[2 * c + 1], Ch = BBUF[2 * c]; // prettier-ignore
385
+ let Dl = BBUF[2 * d + 1], Dh = BBUF[2 * d]; // prettier-ignore
386
+ // v[a] = (v[a] + v[b] + x) | 0;
387
+ let ll = u64.add3L(Al, Bl, Xl);
388
+ Ah = u64.add3H(ll, Ah, Bh, Xh);
389
+ Al = ll | 0;
390
+ // v[d] = rotr(v[d] ^ v[a], 16)
391
+ ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
392
+ ({ Dh, Dl } = { Dh: u64.rotrSH(Dh, Dl, 16), Dl: u64.rotrSL(Dh, Dl, 16) });
393
+ // v[c] = (v[c] + v[d]) | 0;
394
+ ({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
395
+ // v[b] = rotr(v[b] ^ v[c], 11)
396
+ ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
397
+ ({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 11), Bl: u64.rotrSL(Bh, Bl, 11) });
398
+ ((BBUF[2 * a + 1] = Al), (BBUF[2 * a] = Ah));
399
+ ((BBUF[2 * b + 1] = Bl), (BBUF[2 * b] = Bh));
400
+ ((BBUF[2 * c + 1] = Cl), (BBUF[2 * c] = Ch));
401
+ ((BBUF[2 * d + 1] = Dl), (BBUF[2 * d] = Dh));
402
+ }
403
+ // Legacy field names keep the local `l/h` spelling, but array/state order stays `[high, low]` to
404
+ // match the IV tables and `BBUF` layout.
405
+ class BLAKE1_64B extends BLAKE1 {
406
+ v0l;
407
+ v0h;
408
+ v1l;
409
+ v1h;
410
+ v2l;
411
+ v2h;
412
+ v3l;
413
+ v3h;
414
+ v4l;
415
+ v4h;
416
+ v5l;
417
+ v5h;
418
+ v6l;
419
+ v6h;
420
+ v7l;
421
+ v7h;
422
+ constructor(outputLen, IV, lengthFlag, opts = {}) {
423
+ super(128, outputLen, lengthFlag, 16, 8, B64C, opts);
424
+ this.v0l = IV[0] | 0;
425
+ this.v0h = IV[1] | 0;
426
+ this.v1l = IV[2] | 0;
427
+ this.v1h = IV[3] | 0;
428
+ this.v2l = IV[4] | 0;
429
+ this.v2h = IV[5] | 0;
430
+ this.v3l = IV[6] | 0;
431
+ this.v3h = IV[7] | 0;
432
+ this.v4l = IV[8] | 0;
433
+ this.v4h = IV[9] | 0;
434
+ this.v5l = IV[10] | 0;
435
+ this.v5h = IV[11] | 0;
436
+ this.v6l = IV[12] | 0;
437
+ this.v6h = IV[13] | 0;
438
+ this.v7l = IV[14] | 0;
439
+ this.v7h = IV[15] | 0;
440
+ }
441
+ // prettier-ignore
442
+ get() {
443
+ let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
444
+ return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
445
+ }
446
+ // prettier-ignore
447
+ set(v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h) {
448
+ this.v0l = v0l | 0;
449
+ this.v0h = v0h | 0;
450
+ this.v1l = v1l | 0;
451
+ this.v1h = v1h | 0;
452
+ this.v2l = v2l | 0;
453
+ this.v2h = v2h | 0;
454
+ this.v3l = v3l | 0;
455
+ this.v3h = v3h | 0;
456
+ this.v4l = v4l | 0;
457
+ this.v4h = v4h | 0;
458
+ this.v5l = v5l | 0;
459
+ this.v5h = v5h | 0;
460
+ this.v6l = v6l | 0;
461
+ this.v6h = v6h | 0;
462
+ this.v7l = v7l | 0;
463
+ this.v7h = v7h | 0;
464
+ }
465
+ destroy() {
466
+ super.destroy();
467
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
468
+ }
469
+ compress(view, offset, withLength = true) {
470
+ for (let i = 0; i < 32; i++, offset += 4)
471
+ BLAKE512_W[i] = view.getUint32(offset, false);
472
+ this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
473
+ BBUF.set(this.constants.subarray(0, 16), 16);
474
+ if (withLength) {
475
+ // Blake1-64 injects the 64-bit bit counter into `v12` and `v13`; the final all-padding
476
+ // block passes `withLength = false`, leaving the trailing constant lanes untouched.
477
+ const { h, l } = u64.fromBig(BigInt(this.length * 8));
478
+ BBUF[24] = (BBUF[24] ^ h) >>> 0;
479
+ BBUF[25] = (BBUF[25] ^ l) >>> 0;
480
+ BBUF[26] = (BBUF[26] ^ h) >>> 0;
481
+ BBUF[27] = (BBUF[27] ^ l) >>> 0;
482
+ }
483
+ for (let i = 0, k = 0; i < 16; i++) {
484
+ G1b(0, 4, 8, 12, BLAKE512_W, k++);
485
+ G2b(0, 4, 8, 12, BLAKE512_W, k++);
486
+ G1b(1, 5, 9, 13, BLAKE512_W, k++);
487
+ G2b(1, 5, 9, 13, BLAKE512_W, k++);
488
+ G1b(2, 6, 10, 14, BLAKE512_W, k++);
489
+ G2b(2, 6, 10, 14, BLAKE512_W, k++);
490
+ G1b(3, 7, 11, 15, BLAKE512_W, k++);
491
+ G2b(3, 7, 11, 15, BLAKE512_W, k++);
492
+ G1b(0, 5, 10, 15, BLAKE512_W, k++);
493
+ G2b(0, 5, 10, 15, BLAKE512_W, k++);
494
+ G1b(1, 6, 11, 12, BLAKE512_W, k++);
495
+ G2b(1, 6, 11, 12, BLAKE512_W, k++);
496
+ G1b(2, 7, 8, 13, BLAKE512_W, k++);
497
+ G2b(2, 7, 8, 13, BLAKE512_W, k++);
498
+ G1b(3, 4, 9, 14, BLAKE512_W, k++);
499
+ G2b(3, 4, 9, 14, BLAKE512_W, k++);
500
+ }
501
+ this.v0l ^= BBUF[0] ^ BBUF[16] ^ this.salt[0];
502
+ this.v0h ^= BBUF[1] ^ BBUF[17] ^ this.salt[1];
503
+ this.v1l ^= BBUF[2] ^ BBUF[18] ^ this.salt[2];
504
+ this.v1h ^= BBUF[3] ^ BBUF[19] ^ this.salt[3];
505
+ this.v2l ^= BBUF[4] ^ BBUF[20] ^ this.salt[4];
506
+ this.v2h ^= BBUF[5] ^ BBUF[21] ^ this.salt[5];
507
+ this.v3l ^= BBUF[6] ^ BBUF[22] ^ this.salt[6];
508
+ this.v3h ^= BBUF[7] ^ BBUF[23] ^ this.salt[7];
509
+ this.v4l ^= BBUF[8] ^ BBUF[24] ^ this.salt[0];
510
+ this.v4h ^= BBUF[9] ^ BBUF[25] ^ this.salt[1];
511
+ this.v5l ^= BBUF[10] ^ BBUF[26] ^ this.salt[2];
512
+ this.v5h ^= BBUF[11] ^ BBUF[27] ^ this.salt[3];
513
+ this.v6l ^= BBUF[12] ^ BBUF[28] ^ this.salt[4];
514
+ this.v6h ^= BBUF[13] ^ BBUF[29] ^ this.salt[5];
515
+ this.v7l ^= BBUF[14] ^ BBUF[30] ^ this.salt[6];
516
+ this.v7h ^= BBUF[15] ^ BBUF[31] ^ this.salt[7];
517
+ (0, utils_1.clean)(BBUF, BLAKE512_W);
518
+ }
519
+ }
520
+ /** Internal blake1-224 hash class. */
521
+ class _BLAKE224 extends BLAKE1_32B {
522
+ constructor(opts = {}) {
523
+ super(28, B224_IV, 0b0000_0000, opts);
524
+ }
525
+ }
526
+ exports._BLAKE224 = _BLAKE224;
527
+ /** Internal blake1-256 hash class. */
528
+ class _BLAKE256 extends BLAKE1_32B {
529
+ constructor(opts = {}) {
530
+ super(32, B256_IV, 0b0000_0001, opts);
531
+ }
532
+ }
533
+ exports._BLAKE256 = _BLAKE256;
534
+ /** Internal blake1-384 hash class. */
535
+ class _BLAKE384 extends BLAKE1_64B {
536
+ constructor(opts = {}) {
537
+ super(48, B384_IV, 0b0000_0000, opts);
538
+ }
539
+ }
540
+ exports._BLAKE384 = _BLAKE384;
541
+ /** Internal blake1-512 hash class. */
542
+ class _BLAKE512 extends BLAKE1_64B {
543
+ constructor(opts = {}) {
544
+ super(64, B512_IV, 0b0000_0001, opts);
545
+ }
546
+ }
547
+ exports._BLAKE512 = _BLAKE512;
548
+ /**
549
+ * Blake1-224 hash function.
550
+ * @param msg - message bytes to hash
551
+ * @param opts - Optional Blake1 settings. See {@link BlakeOpts}. If set,
552
+ * `opts.salt` must be exactly 16 bytes.
553
+ * @returns Digest bytes.
554
+ * @example
555
+ * Hash a message with Blake1-224.
556
+ * ```ts
557
+ * blake224(new Uint8Array([97, 98, 99]));
558
+ * ```
559
+ */
560
+ exports.blake224 = (0, utils_1.createHasher)((opts) => new _BLAKE224(opts));
561
+ /**
562
+ * Blake1-256 hash function.
563
+ * @param msg - message bytes to hash
564
+ * @param opts - Optional Blake1 settings. See {@link BlakeOpts}. If set,
565
+ * `opts.salt` must be exactly 16 bytes.
566
+ * @returns Digest bytes.
567
+ * @example
568
+ * Hash a message with Blake1-256.
569
+ * ```ts
570
+ * blake256(new Uint8Array([97, 98, 99]));
571
+ * ```
572
+ */
573
+ exports.blake256 = (0, utils_1.createHasher)((opts) => new _BLAKE256(opts));
574
+ /**
575
+ * Blake1-384 hash function.
576
+ * @param msg - message bytes to hash
577
+ * @param opts - Optional Blake1 settings. See {@link BlakeOpts}. If set,
578
+ * `opts.salt` must be exactly 32 bytes.
579
+ * @returns Digest bytes.
580
+ * @example
581
+ * Hash a message with Blake1-384.
582
+ * ```ts
583
+ * blake384(new Uint8Array([97, 98, 99]));
584
+ * ```
585
+ */
586
+ exports.blake384 = (0, utils_1.createHasher)((opts) => new _BLAKE384(opts));
587
+ /**
588
+ * Blake1-512 hash function.
589
+ * @param msg - message bytes to hash
590
+ * @param opts - Optional Blake1 settings. See {@link BlakeOpts}. If set,
591
+ * `opts.salt` must be exactly 32 bytes.
592
+ * @returns Digest bytes.
593
+ * @example
594
+ * Hash a message with Blake1-512.
595
+ * ```ts
596
+ * blake512(new Uint8Array([97, 98, 99]));
597
+ * ```
598
+ */
599
+ exports.blake512 = (0, utils_1.createHasher)((opts) => new _BLAKE512(opts));