@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/blake2.js ADDED
@@ -0,0 +1,540 @@
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.blake2s = exports._BLAKE2s = exports.blake2b = exports._BLAKE2b = exports._BLAKE2 = void 0;
37
+ exports.compress = compress;
38
+ /**
39
+ * blake2b (64-bit) & blake2s (8 to 32-bit) hash functions.
40
+ * b could have been faster, but there is no fast u64 in js, so s is 1.5x faster.
41
+ * @module
42
+ */
43
+ const _blake_1 = require("./_blake");
44
+ const _md_1 = require("./_md");
45
+ const u64 = __importStar(require("./_u64"));
46
+ // prettier-ignore
47
+ const utils_1 = require("./utils");
48
+ // Same IV words as `SHA512_IV`, but endian-swapped into LE u32 low/high halves
49
+ // for the BLAKE2b u64 helpers below.
50
+ const B2B_IV = /* @__PURE__ */ Uint32Array.from([
51
+ 0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a,
52
+ 0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19,
53
+ ]);
54
+ // Shared synchronous BLAKE2b work vector as LE u32 low/high halves.
55
+ const BBUF = /* @__PURE__ */ new Uint32Array(32);
56
+ // BLAKE2b G mix split into two half-rounds over LE u32 low/high limbs.
57
+ function G1b(a, b, c, d, msg, x) {
58
+ // NOTE: V is LE here
59
+ const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
60
+ let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
61
+ let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
62
+ let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
63
+ let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
64
+ // v[a] = (v[a] + v[b] + x) | 0;
65
+ let ll = u64.add3L(Al, Bl, Xl);
66
+ Ah = u64.add3H(ll, Ah, Bh, Xh);
67
+ Al = ll | 0;
68
+ // v[d] = rotr(v[d] ^ v[a], 32)
69
+ ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
70
+ ({ Dh, Dl } = { Dh: u64.rotr32H(Dh, Dl), Dl: u64.rotr32L(Dh, Dl) });
71
+ // v[c] = (v[c] + v[d]) | 0;
72
+ ({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
73
+ // v[b] = rotr(v[b] ^ v[c], 24)
74
+ ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
75
+ ({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 24), Bl: u64.rotrSL(Bh, Bl, 24) });
76
+ ((BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah));
77
+ ((BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh));
78
+ ((BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch));
79
+ ((BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh));
80
+ }
81
+ // Second half-round of the same LE-limb BLAKE2b G mix; `x` is the message word offset.
82
+ function G2b(a, b, c, d, msg, x) {
83
+ // NOTE: V is LE here
84
+ const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
85
+ let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
86
+ let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
87
+ let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
88
+ let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
89
+ // v[a] = (v[a] + v[b] + x) | 0;
90
+ let ll = u64.add3L(Al, Bl, Xl);
91
+ Ah = u64.add3H(ll, Ah, Bh, Xh);
92
+ Al = ll | 0;
93
+ // v[d] = rotr(v[d] ^ v[a], 16)
94
+ ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
95
+ ({ Dh, Dl } = { Dh: u64.rotrSH(Dh, Dl, 16), Dl: u64.rotrSL(Dh, Dl, 16) });
96
+ // v[c] = (v[c] + v[d]) | 0;
97
+ ({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
98
+ // v[b] = rotr(v[b] ^ v[c], 63)
99
+ ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
100
+ ({ Bh, Bl } = { Bh: u64.rotrBH(Bh, Bl, 63), Bl: u64.rotrBL(Bh, Bl, 63) });
101
+ ((BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah));
102
+ ((BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh));
103
+ ((BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch));
104
+ ((BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh));
105
+ }
106
+ function checkBlake2Opts(outputLen, opts = {}, keyLen, saltLen, persLen) {
107
+ (0, utils_1.anumber)(keyLen);
108
+ // RFC 7693 §2.1 requires digest length nn in 1..keyLen.
109
+ if (outputLen <= 0 || outputLen > keyLen)
110
+ throw new Error('outputLen bigger than keyLen');
111
+ const { key, salt, personalization } = opts;
112
+ // This API uses `undefined` for the RFC 7693 `kk = 0` case, so a provided key must be non-empty.
113
+ if (key !== undefined && (key.length < 1 || key.length > keyLen))
114
+ throw new Error('"key" expected to be undefined or of length=1..' + keyLen);
115
+ if (salt !== undefined)
116
+ (0, utils_1.abytes)(salt, saltLen, 'salt');
117
+ if (personalization !== undefined)
118
+ (0, utils_1.abytes)(personalization, persLen, 'personalization');
119
+ }
120
+ /** Internal base class for BLAKE2. */
121
+ class _BLAKE2 {
122
+ buffer;
123
+ buffer32;
124
+ finished = false;
125
+ destroyed = false;
126
+ length = 0;
127
+ pos = 0;
128
+ blockLen;
129
+ outputLen;
130
+ canXOF = false;
131
+ constructor(blockLen, outputLen) {
132
+ (0, utils_1.anumber)(blockLen);
133
+ (0, utils_1.anumber)(outputLen);
134
+ this.blockLen = blockLen;
135
+ this.outputLen = outputLen;
136
+ this.buffer = new Uint8Array(blockLen);
137
+ this.buffer32 = (0, utils_1.u32)(this.buffer);
138
+ }
139
+ update(data) {
140
+ (0, utils_1.aexists)(this);
141
+ (0, utils_1.abytes)(data);
142
+ // Main difference with other hashes: there is flag for last block,
143
+ // so we cannot process current block before we know that there
144
+ // is the next one. This significantly complicates logic and reduces ability
145
+ // to do zero-copy processing
146
+ const { blockLen, buffer, buffer32 } = this;
147
+ const len = data.length;
148
+ const offset = data.byteOffset;
149
+ const buf = data.buffer;
150
+ for (let pos = 0; pos < len;) {
151
+ // If buffer is full and we still have input (don't process last block, same as blake2s)
152
+ if (this.pos === blockLen) {
153
+ (0, utils_1.swap32IfBE)(buffer32);
154
+ this.compress(buffer32, 0, false);
155
+ (0, utils_1.swap32IfBE)(buffer32);
156
+ this.pos = 0;
157
+ }
158
+ const take = Math.min(blockLen - this.pos, len - pos);
159
+ const dataOffset = offset + pos;
160
+ // Zero-copy only for full, 4-byte-aligned, non-final blocks.
161
+ if (take === blockLen && !(dataOffset % 4) && pos + take < len) {
162
+ const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4));
163
+ (0, utils_1.swap32IfBE)(data32);
164
+ for (let pos32 = 0; pos + blockLen < len; pos32 += buffer32.length, pos += blockLen) {
165
+ this.length += blockLen;
166
+ this.compress(data32, pos32, false);
167
+ }
168
+ (0, utils_1.swap32IfBE)(data32);
169
+ continue;
170
+ }
171
+ buffer.set(data.subarray(pos, pos + take), this.pos);
172
+ this.pos += take;
173
+ this.length += take;
174
+ pos += take;
175
+ }
176
+ return this;
177
+ }
178
+ digestInto(out) {
179
+ (0, utils_1.aexists)(this);
180
+ (0, utils_1.aoutput)(out, this);
181
+ const { pos, buffer32 } = this;
182
+ this.finished = true;
183
+ // Padding
184
+ (0, utils_1.clean)(this.buffer.subarray(pos));
185
+ (0, utils_1.swap32IfBE)(buffer32);
186
+ this.compress(buffer32, 0, true);
187
+ (0, utils_1.swap32IfBE)(buffer32);
188
+ // Reject unaligned views explicitly instead of hiding them behind a full scratch copy.
189
+ if (out.byteOffset & 3)
190
+ throw new RangeError('"digestInto() output" expected 4-byte aligned byteOffset, got ' + out.byteOffset);
191
+ const state = this.get();
192
+ const out32 = (0, utils_1.u32)(out);
193
+ const full = Math.floor(this.outputLen / 4);
194
+ for (let i = 0; i < full; i++)
195
+ out32[i] = (0, utils_1.swap8IfBE)(state[i]);
196
+ const tail = this.outputLen % 4;
197
+ if (!tail)
198
+ return;
199
+ const off = full * 4;
200
+ const word = state[full];
201
+ for (let i = 0; i < tail; i++)
202
+ out[off + i] = word >>> (8 * i);
203
+ }
204
+ digest() {
205
+ const { buffer, outputLen } = this;
206
+ this.digestInto(buffer);
207
+ // Return a copy so callers do not alias the instance scratch buffer used during finalization.
208
+ const res = buffer.slice(0, outputLen);
209
+ this.destroy();
210
+ return res;
211
+ }
212
+ _cloneInto(to) {
213
+ const { buffer, length, finished, destroyed, outputLen, pos } = this;
214
+ // Recreate only `dkLen`; key/salt/personalization are already absorbed into the copied state.
215
+ to ||= new this.constructor({ dkLen: outputLen });
216
+ to.set(...this.get());
217
+ to.buffer.set(buffer);
218
+ to.destroyed = destroyed;
219
+ to.finished = finished;
220
+ to.length = length;
221
+ to.pos = pos;
222
+ // @ts-ignore
223
+ to.outputLen = outputLen;
224
+ return to;
225
+ }
226
+ clone() {
227
+ return this._cloneInto();
228
+ }
229
+ }
230
+ exports._BLAKE2 = _BLAKE2;
231
+ /** Internal blake2b hash class with state stored as LE u32 low/high halves. */
232
+ class _BLAKE2b extends _BLAKE2 {
233
+ // Same IV words as SHA-512 / BLAKE2b, encoded as LE u32 low/high halves.
234
+ v0l = B2B_IV[0] | 0;
235
+ v0h = B2B_IV[1] | 0;
236
+ v1l = B2B_IV[2] | 0;
237
+ v1h = B2B_IV[3] | 0;
238
+ v2l = B2B_IV[4] | 0;
239
+ v2h = B2B_IV[5] | 0;
240
+ v3l = B2B_IV[6] | 0;
241
+ v3h = B2B_IV[7] | 0;
242
+ v4l = B2B_IV[8] | 0;
243
+ v4h = B2B_IV[9] | 0;
244
+ v5l = B2B_IV[10] | 0;
245
+ v5h = B2B_IV[11] | 0;
246
+ v6l = B2B_IV[12] | 0;
247
+ v6h = B2B_IV[13] | 0;
248
+ v7l = B2B_IV[14] | 0;
249
+ v7h = B2B_IV[15] | 0;
250
+ constructor(opts = {}) {
251
+ const olen = opts.dkLen === undefined ? 64 : opts.dkLen;
252
+ super(128, olen);
253
+ checkBlake2Opts(olen, opts, 64, 16, 16);
254
+ let { key, personalization, salt } = opts;
255
+ let keyLength = 0;
256
+ if (key !== undefined) {
257
+ (0, utils_1.abytes)(key, undefined, 'key');
258
+ keyLength = key.length;
259
+ }
260
+ // RFC 7693 §2.5: xor `p[0] = 0x0101kknn` into the low 32 bits of `h[0]`;
261
+ // the high 32 bits stay at `IV[0]`.
262
+ this.v0l ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
263
+ if (salt !== undefined) {
264
+ (0, utils_1.abytes)(salt, undefined, 'salt');
265
+ const slt = (0, utils_1.u32)(salt);
266
+ this.v4l ^= (0, utils_1.swap8IfBE)(slt[0]);
267
+ this.v4h ^= (0, utils_1.swap8IfBE)(slt[1]);
268
+ this.v5l ^= (0, utils_1.swap8IfBE)(slt[2]);
269
+ this.v5h ^= (0, utils_1.swap8IfBE)(slt[3]);
270
+ }
271
+ if (personalization !== undefined) {
272
+ (0, utils_1.abytes)(personalization, undefined, 'personalization');
273
+ const pers = (0, utils_1.u32)(personalization);
274
+ this.v6l ^= (0, utils_1.swap8IfBE)(pers[0]);
275
+ this.v6h ^= (0, utils_1.swap8IfBE)(pers[1]);
276
+ this.v7l ^= (0, utils_1.swap8IfBE)(pers[2]);
277
+ this.v7h ^= (0, utils_1.swap8IfBE)(pers[3]);
278
+ }
279
+ if (key !== undefined) {
280
+ // Pad to blockLen and update
281
+ const tmp = new Uint8Array(this.blockLen);
282
+ tmp.set(key);
283
+ this.update(tmp);
284
+ }
285
+ }
286
+ // prettier-ignore
287
+ get() {
288
+ let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
289
+ return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
290
+ }
291
+ // prettier-ignore
292
+ set(v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h) {
293
+ this.v0l = v0l | 0;
294
+ this.v0h = v0h | 0;
295
+ this.v1l = v1l | 0;
296
+ this.v1h = v1h | 0;
297
+ this.v2l = v2l | 0;
298
+ this.v2h = v2h | 0;
299
+ this.v3l = v3l | 0;
300
+ this.v3h = v3h | 0;
301
+ this.v4l = v4l | 0;
302
+ this.v4h = v4h | 0;
303
+ this.v5l = v5l | 0;
304
+ this.v5h = v5h | 0;
305
+ this.v6l = v6l | 0;
306
+ this.v6h = v6h | 0;
307
+ this.v7l = v7l | 0;
308
+ this.v7h = v7h | 0;
309
+ }
310
+ compress(msg, offset, isLast) {
311
+ this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
312
+ BBUF.set(B2B_IV, 16); // Second half from IV.
313
+ let { h, l } = u64.fromBig(BigInt(this.length));
314
+ BBUF[24] = B2B_IV[8] ^ l; // Low word of the offset.
315
+ BBUF[25] = B2B_IV[9] ^ h; // High word.
316
+ // Invert all bits for last block
317
+ if (isLast) {
318
+ BBUF[28] = ~BBUF[28];
319
+ BBUF[29] = ~BBUF[29];
320
+ }
321
+ let j = 0;
322
+ const s = _blake_1.BSIGMA;
323
+ // SIGMA selects 64-bit message words; multiply by 2 because `msg` stores
324
+ // each word as [low32, high32].
325
+ for (let i = 0; i < 12; i++) {
326
+ G1b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
327
+ G2b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
328
+ G1b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
329
+ G2b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
330
+ G1b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
331
+ G2b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
332
+ G1b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
333
+ G2b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
334
+ G1b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
335
+ G2b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
336
+ G1b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
337
+ G2b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
338
+ G1b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
339
+ G2b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
340
+ G1b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
341
+ G2b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
342
+ }
343
+ this.v0l ^= BBUF[0] ^ BBUF[16];
344
+ this.v0h ^= BBUF[1] ^ BBUF[17];
345
+ this.v1l ^= BBUF[2] ^ BBUF[18];
346
+ this.v1h ^= BBUF[3] ^ BBUF[19];
347
+ this.v2l ^= BBUF[4] ^ BBUF[20];
348
+ this.v2h ^= BBUF[5] ^ BBUF[21];
349
+ this.v3l ^= BBUF[6] ^ BBUF[22];
350
+ this.v3h ^= BBUF[7] ^ BBUF[23];
351
+ this.v4l ^= BBUF[8] ^ BBUF[24];
352
+ this.v4h ^= BBUF[9] ^ BBUF[25];
353
+ this.v5l ^= BBUF[10] ^ BBUF[26];
354
+ this.v5h ^= BBUF[11] ^ BBUF[27];
355
+ this.v6l ^= BBUF[12] ^ BBUF[28];
356
+ this.v6h ^= BBUF[13] ^ BBUF[29];
357
+ this.v7l ^= BBUF[14] ^ BBUF[30];
358
+ this.v7h ^= BBUF[15] ^ BBUF[31];
359
+ (0, utils_1.clean)(BBUF);
360
+ }
361
+ destroy() {
362
+ this.destroyed = true;
363
+ (0, utils_1.clean)(this.buffer32);
364
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
365
+ }
366
+ }
367
+ exports._BLAKE2b = _BLAKE2b;
368
+ /**
369
+ * Blake2b hash function. 64-bit. 1.5x slower than blake2s in JS.
370
+ * @param msg - message that would be hashed
371
+ * @param opts - Optional output, MAC, salt, and personalization settings.
372
+ * `dkLen` must be 1..64 bytes; `salt` and `personalization`, if present,
373
+ * must be 16 bytes each. See {@link Blake2Opts}.
374
+ * @returns Digest bytes.
375
+ * @example
376
+ * Hash a message with Blake2b.
377
+ * ```ts
378
+ * blake2b(new Uint8Array([97, 98, 99]));
379
+ * ```
380
+ */
381
+ exports.blake2b = (0, utils_1.createHasher)((opts) => new _BLAKE2b(opts));
382
+ /**
383
+ * BLAKE2-compress core method.
384
+ * Runs only the round function over a caller-supplied local vector; callers initialize `v0..v15`
385
+ * and apply the final `h[i] ^= v[i] ^ v[i + 8]` fold themselves.
386
+ * @param s - flattened sigma schedule bytes
387
+ * @param offset - starting word offset inside `msg`, not a byte offset
388
+ * @param msg - message words
389
+ * @param rounds - round count to execute
390
+ * @param v0 - state word 0
391
+ * @param v1 - state word 1
392
+ * @param v2 - state word 2
393
+ * @param v3 - state word 3
394
+ * @param v4 - state word 4
395
+ * @param v5 - state word 5
396
+ * @param v6 - state word 6
397
+ * @param v7 - state word 7
398
+ * @param v8 - state word 8
399
+ * @param v9 - state word 9
400
+ * @param v10 - state word 10
401
+ * @param v11 - state word 11
402
+ * @param v12 - state word 12
403
+ * @param v13 - state word 13
404
+ * @param v14 - state word 14
405
+ * @param v15 - state word 15
406
+ * @returns Updated compression state words.
407
+ * @example
408
+ * Run the BLAKE2 compression core on zeroed state and message words.
409
+ * ```ts
410
+ * import { compress } from '@decryption/hashes/blake2.js';
411
+ * const state = compress(
412
+ * new Uint8Array(16),
413
+ * 0,
414
+ * new Uint32Array(16),
415
+ * 1,
416
+ * 0, 0, 0, 0, 0, 0, 0, 0,
417
+ * 0, 0, 0, 0, 0, 0, 0, 0
418
+ * );
419
+ * state.v0;
420
+ * ```
421
+ */
422
+ // prettier-ignore
423
+ function compress(s, offset, msg, rounds, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) {
424
+ let j = 0;
425
+ for (let i = 0; i < rounds; i++) {
426
+ ({ a: v0, b: v4, c: v8, d: v12 } = (0, _blake_1.G1s)(v0, v4, v8, v12, msg[offset + s[j++]]));
427
+ ({ a: v0, b: v4, c: v8, d: v12 } = (0, _blake_1.G2s)(v0, v4, v8, v12, msg[offset + s[j++]]));
428
+ ({ a: v1, b: v5, c: v9, d: v13 } = (0, _blake_1.G1s)(v1, v5, v9, v13, msg[offset + s[j++]]));
429
+ ({ a: v1, b: v5, c: v9, d: v13 } = (0, _blake_1.G2s)(v1, v5, v9, v13, msg[offset + s[j++]]));
430
+ ({ a: v2, b: v6, c: v10, d: v14 } = (0, _blake_1.G1s)(v2, v6, v10, v14, msg[offset + s[j++]]));
431
+ ({ a: v2, b: v6, c: v10, d: v14 } = (0, _blake_1.G2s)(v2, v6, v10, v14, msg[offset + s[j++]]));
432
+ ({ a: v3, b: v7, c: v11, d: v15 } = (0, _blake_1.G1s)(v3, v7, v11, v15, msg[offset + s[j++]]));
433
+ ({ a: v3, b: v7, c: v11, d: v15 } = (0, _blake_1.G2s)(v3, v7, v11, v15, msg[offset + s[j++]]));
434
+ ({ a: v0, b: v5, c: v10, d: v15 } = (0, _blake_1.G1s)(v0, v5, v10, v15, msg[offset + s[j++]]));
435
+ ({ a: v0, b: v5, c: v10, d: v15 } = (0, _blake_1.G2s)(v0, v5, v10, v15, msg[offset + s[j++]]));
436
+ ({ a: v1, b: v6, c: v11, d: v12 } = (0, _blake_1.G1s)(v1, v6, v11, v12, msg[offset + s[j++]]));
437
+ ({ a: v1, b: v6, c: v11, d: v12 } = (0, _blake_1.G2s)(v1, v6, v11, v12, msg[offset + s[j++]]));
438
+ ({ a: v2, b: v7, c: v8, d: v13 } = (0, _blake_1.G1s)(v2, v7, v8, v13, msg[offset + s[j++]]));
439
+ ({ a: v2, b: v7, c: v8, d: v13 } = (0, _blake_1.G2s)(v2, v7, v8, v13, msg[offset + s[j++]]));
440
+ ({ a: v3, b: v4, c: v9, d: v14 } = (0, _blake_1.G1s)(v3, v4, v9, v14, msg[offset + s[j++]]));
441
+ ({ a: v3, b: v4, c: v9, d: v14 } = (0, _blake_1.G2s)(v3, v4, v9, v14, msg[offset + s[j++]]));
442
+ }
443
+ return { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 };
444
+ }
445
+ // Blake2s reuses the SHA-256 IV words as-is.
446
+ const B2S_IV = /* @__PURE__ */ _md_1.SHA256_IV.slice();
447
+ /** Internal blake2s hash class. */
448
+ class _BLAKE2s extends _BLAKE2 {
449
+ // Internal state, same as SHA-256
450
+ v0 = B2S_IV[0] | 0;
451
+ v1 = B2S_IV[1] | 0;
452
+ v2 = B2S_IV[2] | 0;
453
+ v3 = B2S_IV[3] | 0;
454
+ v4 = B2S_IV[4] | 0;
455
+ v5 = B2S_IV[5] | 0;
456
+ v6 = B2S_IV[6] | 0;
457
+ v7 = B2S_IV[7] | 0;
458
+ constructor(opts = {}) {
459
+ const olen = opts.dkLen === undefined ? 32 : opts.dkLen;
460
+ super(64, olen);
461
+ checkBlake2Opts(olen, opts, 32, 8, 8);
462
+ let { key, personalization, salt } = opts;
463
+ let keyLength = 0;
464
+ if (key !== undefined) {
465
+ (0, utils_1.abytes)(key, undefined, 'key');
466
+ keyLength = key.length;
467
+ }
468
+ // RFC 7693 §2.5: xor `p[0] = 0x0101kknn` directly into `h[0]`, since
469
+ // BLAKE2s stores each state word as one `u32`.
470
+ this.v0 ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
471
+ if (salt !== undefined) {
472
+ (0, utils_1.abytes)(salt, undefined, 'salt');
473
+ const slt = (0, utils_1.u32)(salt);
474
+ this.v4 ^= (0, utils_1.swap8IfBE)(slt[0]);
475
+ this.v5 ^= (0, utils_1.swap8IfBE)(slt[1]);
476
+ }
477
+ if (personalization !== undefined) {
478
+ (0, utils_1.abytes)(personalization, undefined, 'personalization');
479
+ const pers = (0, utils_1.u32)(personalization);
480
+ this.v6 ^= (0, utils_1.swap8IfBE)(pers[0]);
481
+ this.v7 ^= (0, utils_1.swap8IfBE)(pers[1]);
482
+ }
483
+ if (key !== undefined) {
484
+ // Pad to blockLen and update
485
+ const tmp = new Uint8Array(this.blockLen);
486
+ tmp.set(key);
487
+ this.update(tmp);
488
+ }
489
+ }
490
+ get() {
491
+ const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
492
+ return [v0, v1, v2, v3, v4, v5, v6, v7];
493
+ }
494
+ // prettier-ignore
495
+ set(v0, v1, v2, v3, v4, v5, v6, v7) {
496
+ this.v0 = v0 | 0;
497
+ this.v1 = v1 | 0;
498
+ this.v2 = v2 | 0;
499
+ this.v3 = v3 | 0;
500
+ this.v4 = v4 | 0;
501
+ this.v5 = v5 | 0;
502
+ this.v6 = v6 | 0;
503
+ this.v7 = v7 | 0;
504
+ }
505
+ compress(msg, offset, isLast) {
506
+ const { h, l } = u64.fromBig(BigInt(this.length));
507
+ // Seed v8..v15 from the IV, xor the low/high 32-bit byte counter into
508
+ // v12/v13, and invert v14 on the final block.
509
+ // prettier-ignore
510
+ const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = compress(_blake_1.BSIGMA, offset, msg, 10, this.v0, this.v1, this.v2, this.v3, this.v4, this.v5, this.v6, this.v7, B2S_IV[0], B2S_IV[1], B2S_IV[2], B2S_IV[3], l ^ B2S_IV[4], h ^ B2S_IV[5], isLast ? ~B2S_IV[6] : B2S_IV[6], B2S_IV[7]);
511
+ this.v0 ^= v0 ^ v8;
512
+ this.v1 ^= v1 ^ v9;
513
+ this.v2 ^= v2 ^ v10;
514
+ this.v3 ^= v3 ^ v11;
515
+ this.v4 ^= v4 ^ v12;
516
+ this.v5 ^= v5 ^ v13;
517
+ this.v6 ^= v6 ^ v14;
518
+ this.v7 ^= v7 ^ v15;
519
+ }
520
+ destroy() {
521
+ this.destroyed = true;
522
+ (0, utils_1.clean)(this.buffer32);
523
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
524
+ }
525
+ }
526
+ exports._BLAKE2s = _BLAKE2s;
527
+ /**
528
+ * Blake2s hash function. Focuses on 8-bit to 32-bit platforms. 1.5x faster than blake2b in JS.
529
+ * @param msg - message that would be hashed
530
+ * @param opts - Optional output, MAC, salt, and personalization settings.
531
+ * `dkLen` must be 1..32 bytes; `salt` and `personalization`, if present,
532
+ * must be 8 bytes each. See {@link Blake2Opts}.
533
+ * @returns Digest bytes.
534
+ * @example
535
+ * Hash a message with Blake2s.
536
+ * ```ts
537
+ * blake2s(new Uint8Array([97, 98, 99]));
538
+ * ```
539
+ */
540
+ exports.blake2s = (0, utils_1.createHasher)((opts) => new _BLAKE2s(opts));
package/blake3.d.ts ADDED
@@ -0,0 +1,67 @@
1
+ import { _BLAKE2 } from './blake2';
2
+ import { type CHashXOF, type HashXOF, type TArg, type TRet } from './utils';
3
+ /**
4
+ * Ensure to use EITHER `key` OR `context`, not both.
5
+ *
6
+ * * `key`: 32-byte MAC key.
7
+ * * `context`: caller-encoded KDF context bytes. If your protocol defines a
8
+ * string context, encode it before passing it here.
9
+ * A good default format for the original context string is
10
+ * "[application] [commit timestamp] [purpose]".
11
+ */
12
+ export type Blake3Opts = {
13
+ /** Desired digest length in bytes. The BLAKE3 spec allows 0..2^64-1 bytes of output. */
14
+ dkLen?: number;
15
+ /** Optional 32-byte MAC key. */
16
+ key?: Uint8Array;
17
+ /** Optional KDF context bytes. */
18
+ context?: Uint8Array;
19
+ };
20
+ /** Blake3 hash. Can be used as MAC and KDF with caller-encoded context bytes. */
21
+ export declare class _BLAKE3 extends _BLAKE2<_BLAKE3> implements HashXOF<_BLAKE3> {
22
+ readonly canXOF = true;
23
+ private chunkPos;
24
+ private chunksDone;
25
+ private flags;
26
+ private IV;
27
+ private state;
28
+ private stack;
29
+ private posOut;
30
+ private bufferOut32;
31
+ private bufferOut;
32
+ private chunkOut;
33
+ private enableXOF;
34
+ constructor(opts?: Blake3Opts, flags?: number);
35
+ protected get(): [];
36
+ protected set(): void;
37
+ private b2Compress;
38
+ protected compress(buf: Uint32Array, bufPos?: number, isLast?: boolean): void;
39
+ _cloneInto(to?: _BLAKE3): _BLAKE3;
40
+ destroy(): void;
41
+ private b2CompressOut;
42
+ protected finish(): void;
43
+ private writeInto;
44
+ xofInto(out: TArg<Uint8Array>): TRet<Uint8Array>;
45
+ xof(bytes: number): TRet<Uint8Array>;
46
+ digestInto(out: TArg<Uint8Array>): void;
47
+ digest(): TRet<Uint8Array>;
48
+ }
49
+ /**
50
+ * BLAKE3 hash function. Can be used as MAC and KDF.
51
+ * @param msg - message that would be hashed
52
+ * @param opts - Optional output, MAC, or KDF configuration. `key` must be
53
+ * exactly 32 bytes, `context` is caller-encoded bytes, and `dkLen` can be
54
+ * 0..2^64-1 via the XOF-backed output path. See {@link Blake3Opts}.
55
+ * @returns Digest bytes.
56
+ * @example
57
+ * Hash, MAC, or derive key material with BLAKE3.
58
+ * ```ts
59
+ * import { blake3 } from '@decryption/hashes/blake3.js';
60
+ * import { utf8ToBytes } from '@decryption/hashes/utils.js';
61
+ * const data = new Uint8Array(32);
62
+ * const hash = blake3(data);
63
+ * const mac = blake3(data, { key: new Uint8Array(32) });
64
+ * const kdf = blake3(data, { context: utf8ToBytes('application name') });
65
+ * ```
66
+ */
67
+ export declare const blake3: TRet<CHashXOF<_BLAKE3, Blake3Opts>>;