@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/sha2.js ADDED
@@ -0,0 +1,499 @@
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.sha512_224 = exports.sha512_256 = exports.sha384 = exports.sha512 = exports.sha224 = exports.sha256 = exports._SHA512_256 = exports._SHA512_224 = exports._SHA384 = exports._SHA512 = exports._SHA224 = exports._SHA256 = void 0;
37
+ /**
38
+ * SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
39
+ * SHA256 is the fastest hash implementable in JS, even faster than Blake3.
40
+ * Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
41
+ * {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
42
+ * @module
43
+ */
44
+ const _md_1 = require("./_md");
45
+ const u64 = __importStar(require("./_u64"));
46
+ const utils_1 = require("./utils");
47
+ /**
48
+ * SHA-224 / SHA-256 round constants from RFC 6234 §5.1: the first 32 bits
49
+ * of the cube roots of the first 64 primes (2..311).
50
+ */
51
+ // prettier-ignore
52
+ const SHA256_K = /* @__PURE__ */ Uint32Array.from([
53
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
54
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
55
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
56
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
57
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
58
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
59
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
60
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
61
+ ]);
62
+ /** Reusable SHA-224 / SHA-256 message schedule buffer `W_t` from RFC 6234 §6.2 step 1. */
63
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
64
+ /** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
65
+ class SHA2_32B extends _md_1.HashMD {
66
+ constructor(outputLen) {
67
+ super(64, outputLen, 8, false);
68
+ }
69
+ get() {
70
+ const { A, B, C, D, E, F, G, H } = this;
71
+ return [A, B, C, D, E, F, G, H];
72
+ }
73
+ // prettier-ignore
74
+ set(A, B, C, D, E, F, G, H) {
75
+ this.A = A | 0;
76
+ this.B = B | 0;
77
+ this.C = C | 0;
78
+ this.D = D | 0;
79
+ this.E = E | 0;
80
+ this.F = F | 0;
81
+ this.G = G | 0;
82
+ this.H = H | 0;
83
+ }
84
+ process(view, offset) {
85
+ // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
86
+ for (let i = 0; i < 16; i++, offset += 4)
87
+ SHA256_W[i] = view.getUint32(offset, false);
88
+ for (let i = 16; i < 64; i++) {
89
+ const W15 = SHA256_W[i - 15];
90
+ const W2 = SHA256_W[i - 2];
91
+ const s0 = (0, utils_1.rotr)(W15, 7) ^ (0, utils_1.rotr)(W15, 18) ^ (W15 >>> 3);
92
+ const s1 = (0, utils_1.rotr)(W2, 17) ^ (0, utils_1.rotr)(W2, 19) ^ (W2 >>> 10);
93
+ SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
94
+ }
95
+ // Compression function main loop, 64 rounds
96
+ let { A, B, C, D, E, F, G, H } = this;
97
+ for (let i = 0; i < 64; i++) {
98
+ const sigma1 = (0, utils_1.rotr)(E, 6) ^ (0, utils_1.rotr)(E, 11) ^ (0, utils_1.rotr)(E, 25);
99
+ const T1 = (H + sigma1 + (0, _md_1.Chi)(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
100
+ const sigma0 = (0, utils_1.rotr)(A, 2) ^ (0, utils_1.rotr)(A, 13) ^ (0, utils_1.rotr)(A, 22);
101
+ const T2 = (sigma0 + (0, _md_1.Maj)(A, B, C)) | 0;
102
+ H = G;
103
+ G = F;
104
+ F = E;
105
+ E = (D + T1) | 0;
106
+ D = C;
107
+ C = B;
108
+ B = A;
109
+ A = (T1 + T2) | 0;
110
+ }
111
+ // Add the compressed chunk to the current hash value
112
+ A = (A + this.A) | 0;
113
+ B = (B + this.B) | 0;
114
+ C = (C + this.C) | 0;
115
+ D = (D + this.D) | 0;
116
+ E = (E + this.E) | 0;
117
+ F = (F + this.F) | 0;
118
+ G = (G + this.G) | 0;
119
+ H = (H + this.H) | 0;
120
+ this.set(A, B, C, D, E, F, G, H);
121
+ }
122
+ roundClean() {
123
+ (0, utils_1.clean)(SHA256_W);
124
+ }
125
+ destroy() {
126
+ // HashMD callers route post-destroy usability through `destroyed`; zeroizing alone still leaves
127
+ // update()/digest() callable on reused instances.
128
+ this.destroyed = true;
129
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
130
+ (0, utils_1.clean)(this.buffer);
131
+ }
132
+ }
133
+ /** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
134
+ class _SHA256 extends SHA2_32B {
135
+ // We cannot use array here since array allows indexing by variable
136
+ // which means optimizer/compiler cannot use registers.
137
+ A = _md_1.SHA256_IV[0] | 0;
138
+ B = _md_1.SHA256_IV[1] | 0;
139
+ C = _md_1.SHA256_IV[2] | 0;
140
+ D = _md_1.SHA256_IV[3] | 0;
141
+ E = _md_1.SHA256_IV[4] | 0;
142
+ F = _md_1.SHA256_IV[5] | 0;
143
+ G = _md_1.SHA256_IV[6] | 0;
144
+ H = _md_1.SHA256_IV[7] | 0;
145
+ constructor() {
146
+ super(32);
147
+ }
148
+ }
149
+ exports._SHA256 = _SHA256;
150
+ /** Internal SHA-224 hash class grounded in RFC 6234 §6.2 and §8.5. */
151
+ class _SHA224 extends SHA2_32B {
152
+ A = _md_1.SHA224_IV[0] | 0;
153
+ B = _md_1.SHA224_IV[1] | 0;
154
+ C = _md_1.SHA224_IV[2] | 0;
155
+ D = _md_1.SHA224_IV[3] | 0;
156
+ E = _md_1.SHA224_IV[4] | 0;
157
+ F = _md_1.SHA224_IV[5] | 0;
158
+ G = _md_1.SHA224_IV[6] | 0;
159
+ H = _md_1.SHA224_IV[7] | 0;
160
+ constructor() {
161
+ super(28);
162
+ }
163
+ }
164
+ exports._SHA224 = _SHA224;
165
+ // SHA2-512 is slower than sha256 in js because u64 operations are slow.
166
+ // SHA-384 / SHA-512 round constants from RFC 6234 §5.2:
167
+ // 80 full 64-bit words split into high/low halves.
168
+ // prettier-ignore
169
+ const K512 = /* @__PURE__ */ (() => u64.split([
170
+ '0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
171
+ '0x3956c25bf348b538', '0x59f111f1b605d019', '0x923f82a4af194f9b', '0xab1c5ed5da6d8118',
172
+ '0xd807aa98a3030242', '0x12835b0145706fbe', '0x243185be4ee4b28c', '0x550c7dc3d5ffb4e2',
173
+ '0x72be5d74f27b896f', '0x80deb1fe3b1696b1', '0x9bdc06a725c71235', '0xc19bf174cf692694',
174
+ '0xe49b69c19ef14ad2', '0xefbe4786384f25e3', '0x0fc19dc68b8cd5b5', '0x240ca1cc77ac9c65',
175
+ '0x2de92c6f592b0275', '0x4a7484aa6ea6e483', '0x5cb0a9dcbd41fbd4', '0x76f988da831153b5',
176
+ '0x983e5152ee66dfab', '0xa831c66d2db43210', '0xb00327c898fb213f', '0xbf597fc7beef0ee4',
177
+ '0xc6e00bf33da88fc2', '0xd5a79147930aa725', '0x06ca6351e003826f', '0x142929670a0e6e70',
178
+ '0x27b70a8546d22ffc', '0x2e1b21385c26c926', '0x4d2c6dfc5ac42aed', '0x53380d139d95b3df',
179
+ '0x650a73548baf63de', '0x766a0abb3c77b2a8', '0x81c2c92e47edaee6', '0x92722c851482353b',
180
+ '0xa2bfe8a14cf10364', '0xa81a664bbc423001', '0xc24b8b70d0f89791', '0xc76c51a30654be30',
181
+ '0xd192e819d6ef5218', '0xd69906245565a910', '0xf40e35855771202a', '0x106aa07032bbd1b8',
182
+ '0x19a4c116b8d2d0c8', '0x1e376c085141ab53', '0x2748774cdf8eeb99', '0x34b0bcb5e19b48a8',
183
+ '0x391c0cb3c5c95a63', '0x4ed8aa4ae3418acb', '0x5b9cca4f7763e373', '0x682e6ff3d6b2b8a3',
184
+ '0x748f82ee5defb2fc', '0x78a5636f43172f60', '0x84c87814a1f0ab72', '0x8cc702081a6439ec',
185
+ '0x90befffa23631e28', '0xa4506cebde82bde9', '0xbef9a3f7b2c67915', '0xc67178f2e372532b',
186
+ '0xca273eceea26619c', '0xd186b8c721c0c207', '0xeada7dd6cde0eb1e', '0xf57d4f7fee6ed178',
187
+ '0x06f067aa72176fba', '0x0a637dc5a2c898a6', '0x113f9804bef90dae', '0x1b710b35131c471b',
188
+ '0x28db77f523047d84', '0x32caab7b40c72493', '0x3c9ebe0a15c9bebc', '0x431d67c49c100d4c',
189
+ '0x4cc5d4becb3e42b6', '0x597f299cfc657e2a', '0x5fcb6fab3ad6faec', '0x6c44198c4a475817'
190
+ ].map(n => BigInt(n))))();
191
+ const SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
192
+ const SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
193
+ // Reusable high-half schedule buffer for the RFC 6234 §6.4 64-bit `W_t` words.
194
+ const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
195
+ // Reusable low-half schedule buffer for the RFC 6234 §6.4 64-bit `W_t` words.
196
+ const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
197
+ /** Internal SHA-384 / SHA-512 compression engine from RFC 6234 §6.4. */
198
+ class SHA2_64B extends _md_1.HashMD {
199
+ constructor(outputLen) {
200
+ super(128, outputLen, 16, false);
201
+ }
202
+ // prettier-ignore
203
+ get() {
204
+ const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
205
+ return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
206
+ }
207
+ // prettier-ignore
208
+ set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
209
+ this.Ah = Ah | 0;
210
+ this.Al = Al | 0;
211
+ this.Bh = Bh | 0;
212
+ this.Bl = Bl | 0;
213
+ this.Ch = Ch | 0;
214
+ this.Cl = Cl | 0;
215
+ this.Dh = Dh | 0;
216
+ this.Dl = Dl | 0;
217
+ this.Eh = Eh | 0;
218
+ this.El = El | 0;
219
+ this.Fh = Fh | 0;
220
+ this.Fl = Fl | 0;
221
+ this.Gh = Gh | 0;
222
+ this.Gl = Gl | 0;
223
+ this.Hh = Hh | 0;
224
+ this.Hl = Hl | 0;
225
+ }
226
+ process(view, offset) {
227
+ // Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
228
+ for (let i = 0; i < 16; i++, offset += 4) {
229
+ SHA512_W_H[i] = view.getUint32(offset);
230
+ SHA512_W_L[i] = view.getUint32((offset += 4));
231
+ }
232
+ for (let i = 16; i < 80; i++) {
233
+ // s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)
234
+ const W15h = SHA512_W_H[i - 15] | 0;
235
+ const W15l = SHA512_W_L[i - 15] | 0;
236
+ const s0h = u64.rotrSH(W15h, W15l, 1) ^ u64.rotrSH(W15h, W15l, 8) ^ u64.shrSH(W15h, W15l, 7);
237
+ const s0l = u64.rotrSL(W15h, W15l, 1) ^ u64.rotrSL(W15h, W15l, 8) ^ u64.shrSL(W15h, W15l, 7);
238
+ // s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)
239
+ const W2h = SHA512_W_H[i - 2] | 0;
240
+ const W2l = SHA512_W_L[i - 2] | 0;
241
+ const s1h = u64.rotrSH(W2h, W2l, 19) ^ u64.rotrBH(W2h, W2l, 61) ^ u64.shrSH(W2h, W2l, 6);
242
+ const s1l = u64.rotrSL(W2h, W2l, 19) ^ u64.rotrBL(W2h, W2l, 61) ^ u64.shrSL(W2h, W2l, 6);
243
+ // SHA512_W[i] = s0 + s1 + SHA512_W[i - 7] + SHA512_W[i - 16];
244
+ const SUMl = u64.add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
245
+ const SUMh = u64.add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
246
+ SHA512_W_H[i] = SUMh | 0;
247
+ SHA512_W_L[i] = SUMl | 0;
248
+ }
249
+ let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
250
+ // Compression function main loop, 80 rounds
251
+ for (let i = 0; i < 80; i++) {
252
+ // S1 := (e rightrotate 14) xor (e rightrotate 18) xor (e rightrotate 41)
253
+ const sigma1h = u64.rotrSH(Eh, El, 14) ^ u64.rotrSH(Eh, El, 18) ^ u64.rotrBH(Eh, El, 41);
254
+ const sigma1l = u64.rotrSL(Eh, El, 14) ^ u64.rotrSL(Eh, El, 18) ^ u64.rotrBL(Eh, El, 41);
255
+ //const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
256
+ const CHIh = (Eh & Fh) ^ (~Eh & Gh);
257
+ const CHIl = (El & Fl) ^ (~El & Gl);
258
+ // T1 = H + sigma1 + Chi(E, F, G) + SHA512_K[i] + SHA512_W[i]
259
+ // prettier-ignore
260
+ const T1ll = u64.add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
261
+ const T1h = u64.add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
262
+ const T1l = T1ll | 0;
263
+ // S0 := (a rightrotate 28) xor (a rightrotate 34) xor (a rightrotate 39)
264
+ const sigma0h = u64.rotrSH(Ah, Al, 28) ^ u64.rotrBH(Ah, Al, 34) ^ u64.rotrBH(Ah, Al, 39);
265
+ const sigma0l = u64.rotrSL(Ah, Al, 28) ^ u64.rotrBL(Ah, Al, 34) ^ u64.rotrBL(Ah, Al, 39);
266
+ const MAJh = (Ah & Bh) ^ (Ah & Ch) ^ (Bh & Ch);
267
+ const MAJl = (Al & Bl) ^ (Al & Cl) ^ (Bl & Cl);
268
+ Hh = Gh | 0;
269
+ Hl = Gl | 0;
270
+ Gh = Fh | 0;
271
+ Gl = Fl | 0;
272
+ Fh = Eh | 0;
273
+ Fl = El | 0;
274
+ ({ h: Eh, l: El } = u64.add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
275
+ Dh = Ch | 0;
276
+ Dl = Cl | 0;
277
+ Ch = Bh | 0;
278
+ Cl = Bl | 0;
279
+ Bh = Ah | 0;
280
+ Bl = Al | 0;
281
+ const All = u64.add3L(T1l, sigma0l, MAJl);
282
+ Ah = u64.add3H(All, T1h, sigma0h, MAJh);
283
+ Al = All | 0;
284
+ }
285
+ // Add the compressed chunk to the current hash value
286
+ ({ h: Ah, l: Al } = u64.add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
287
+ ({ h: Bh, l: Bl } = u64.add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
288
+ ({ h: Ch, l: Cl } = u64.add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
289
+ ({ h: Dh, l: Dl } = u64.add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
290
+ ({ h: Eh, l: El } = u64.add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
291
+ ({ h: Fh, l: Fl } = u64.add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
292
+ ({ h: Gh, l: Gl } = u64.add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
293
+ ({ h: Hh, l: Hl } = u64.add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
294
+ this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
295
+ }
296
+ roundClean() {
297
+ (0, utils_1.clean)(SHA512_W_H, SHA512_W_L);
298
+ }
299
+ destroy() {
300
+ // HashMD callers route post-destroy usability through `destroyed`; zeroizing alone still leaves
301
+ // update()/digest() callable on reused instances.
302
+ this.destroyed = true;
303
+ (0, utils_1.clean)(this.buffer);
304
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
305
+ }
306
+ }
307
+ /** Internal SHA-512 hash class grounded in RFC 6234 §6.3 and §6.4. */
308
+ class _SHA512 extends SHA2_64B {
309
+ Ah = _md_1.SHA512_IV[0] | 0;
310
+ Al = _md_1.SHA512_IV[1] | 0;
311
+ Bh = _md_1.SHA512_IV[2] | 0;
312
+ Bl = _md_1.SHA512_IV[3] | 0;
313
+ Ch = _md_1.SHA512_IV[4] | 0;
314
+ Cl = _md_1.SHA512_IV[5] | 0;
315
+ Dh = _md_1.SHA512_IV[6] | 0;
316
+ Dl = _md_1.SHA512_IV[7] | 0;
317
+ Eh = _md_1.SHA512_IV[8] | 0;
318
+ El = _md_1.SHA512_IV[9] | 0;
319
+ Fh = _md_1.SHA512_IV[10] | 0;
320
+ Fl = _md_1.SHA512_IV[11] | 0;
321
+ Gh = _md_1.SHA512_IV[12] | 0;
322
+ Gl = _md_1.SHA512_IV[13] | 0;
323
+ Hh = _md_1.SHA512_IV[14] | 0;
324
+ Hl = _md_1.SHA512_IV[15] | 0;
325
+ constructor() {
326
+ super(64);
327
+ }
328
+ }
329
+ exports._SHA512 = _SHA512;
330
+ /** Internal SHA-384 hash class grounded in RFC 6234 §6.3 and §6.4. */
331
+ class _SHA384 extends SHA2_64B {
332
+ Ah = _md_1.SHA384_IV[0] | 0;
333
+ Al = _md_1.SHA384_IV[1] | 0;
334
+ Bh = _md_1.SHA384_IV[2] | 0;
335
+ Bl = _md_1.SHA384_IV[3] | 0;
336
+ Ch = _md_1.SHA384_IV[4] | 0;
337
+ Cl = _md_1.SHA384_IV[5] | 0;
338
+ Dh = _md_1.SHA384_IV[6] | 0;
339
+ Dl = _md_1.SHA384_IV[7] | 0;
340
+ Eh = _md_1.SHA384_IV[8] | 0;
341
+ El = _md_1.SHA384_IV[9] | 0;
342
+ Fh = _md_1.SHA384_IV[10] | 0;
343
+ Fl = _md_1.SHA384_IV[11] | 0;
344
+ Gh = _md_1.SHA384_IV[12] | 0;
345
+ Gl = _md_1.SHA384_IV[13] | 0;
346
+ Hh = _md_1.SHA384_IV[14] | 0;
347
+ Hl = _md_1.SHA384_IV[15] | 0;
348
+ constructor() {
349
+ super(48);
350
+ }
351
+ }
352
+ exports._SHA384 = _SHA384;
353
+ /**
354
+ * Truncated SHA512/256 and SHA512/224.
355
+ * SHA512_IV is XORed with 0xa5a5a5a5a5a5a5a5, then used as "intermediary" IV of SHA512/t.
356
+ * Then t hashes string to produce result IV.
357
+ * See the repo-side derivation recipe in `test/misc/sha2-gen-iv.js`.
358
+ * These IV literals are checked against that script rather than a dedicated
359
+ * local RFC section.
360
+ */
361
+ /** SHA-512/224 IV derived by the SHA-512/t recipe in `test/misc/sha2-gen-iv.js` and
362
+ * stored as sixteen big-endian 32-bit halves. */
363
+ const T224_IV = /* @__PURE__ */ Uint32Array.from([
364
+ 0x8c3d37c8, 0x19544da2, 0x73e19966, 0x89dcd4d6, 0x1dfab7ae, 0x32ff9c82, 0x679dd514, 0x582f9fcf,
365
+ 0x0f6d2b69, 0x7bd44da8, 0x77e36f73, 0x04c48942, 0x3f9d85a8, 0x6a1d36c8, 0x1112e6ad, 0x91d692a1,
366
+ ]);
367
+ /** SHA-512/256 IV derived by the SHA-512/t recipe in `test/misc/sha2-gen-iv.js` and
368
+ * stored as sixteen big-endian 32-bit halves. */
369
+ const T256_IV = /* @__PURE__ */ Uint32Array.from([
370
+ 0x22312194, 0xfc2bf72c, 0x9f555fa3, 0xc84c64c2, 0x2393b86b, 0x6f53b151, 0x96387719, 0x5940eabd,
371
+ 0x96283ee2, 0xa88effe3, 0xbe5e1e25, 0x53863992, 0x2b0199fc, 0x2c85b8aa, 0x0eb72ddc, 0x81c52ca2,
372
+ ]);
373
+ /** Internal SHA-512/224 hash class using the derived `T224_IV` and the shared
374
+ * RFC 6234 §6.4 compression engine. */
375
+ class _SHA512_224 extends SHA2_64B {
376
+ Ah = T224_IV[0] | 0;
377
+ Al = T224_IV[1] | 0;
378
+ Bh = T224_IV[2] | 0;
379
+ Bl = T224_IV[3] | 0;
380
+ Ch = T224_IV[4] | 0;
381
+ Cl = T224_IV[5] | 0;
382
+ Dh = T224_IV[6] | 0;
383
+ Dl = T224_IV[7] | 0;
384
+ Eh = T224_IV[8] | 0;
385
+ El = T224_IV[9] | 0;
386
+ Fh = T224_IV[10] | 0;
387
+ Fl = T224_IV[11] | 0;
388
+ Gh = T224_IV[12] | 0;
389
+ Gl = T224_IV[13] | 0;
390
+ Hh = T224_IV[14] | 0;
391
+ Hl = T224_IV[15] | 0;
392
+ constructor() {
393
+ super(28);
394
+ }
395
+ }
396
+ exports._SHA512_224 = _SHA512_224;
397
+ /** Internal SHA-512/256 hash class using the derived `T256_IV` and the shared
398
+ * RFC 6234 §6.4 compression engine. */
399
+ class _SHA512_256 extends SHA2_64B {
400
+ Ah = T256_IV[0] | 0;
401
+ Al = T256_IV[1] | 0;
402
+ Bh = T256_IV[2] | 0;
403
+ Bl = T256_IV[3] | 0;
404
+ Ch = T256_IV[4] | 0;
405
+ Cl = T256_IV[5] | 0;
406
+ Dh = T256_IV[6] | 0;
407
+ Dl = T256_IV[7] | 0;
408
+ Eh = T256_IV[8] | 0;
409
+ El = T256_IV[9] | 0;
410
+ Fh = T256_IV[10] | 0;
411
+ Fl = T256_IV[11] | 0;
412
+ Gh = T256_IV[12] | 0;
413
+ Gl = T256_IV[13] | 0;
414
+ Hh = T256_IV[14] | 0;
415
+ Hl = T256_IV[15] | 0;
416
+ constructor() {
417
+ super(32);
418
+ }
419
+ }
420
+ exports._SHA512_256 = _SHA512_256;
421
+ /**
422
+ * SHA2-256 hash function from RFC 4634. In JS it's the fastest: even faster than Blake3. Some info:
423
+ *
424
+ * - Trying 2^128 hashes would get 50% chance of collision, using birthday attack.
425
+ * - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
426
+ * - Each sha256 hash is executing 2^18 bit operations.
427
+ * - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
428
+ * @param msg - message bytes to hash
429
+ * @returns Digest bytes.
430
+ * @example
431
+ * Hash a message with SHA2-256.
432
+ * ```ts
433
+ * sha256(new Uint8Array([97, 98, 99]));
434
+ * ```
435
+ */
436
+ exports.sha256 = (0, utils_1.createHasher)(() => new _SHA256(),
437
+ /* @__PURE__ */ (0, utils_1.oidNist)(0x01));
438
+ /**
439
+ * SHA2-224 hash function from RFC 4634.
440
+ * @param msg - message bytes to hash
441
+ * @returns Digest bytes.
442
+ * @example
443
+ * Hash a message with SHA2-224.
444
+ * ```ts
445
+ * sha224(new Uint8Array([97, 98, 99]));
446
+ * ```
447
+ */
448
+ exports.sha224 = (0, utils_1.createHasher)(() => new _SHA224(),
449
+ /* @__PURE__ */ (0, utils_1.oidNist)(0x04));
450
+ /**
451
+ * SHA2-512 hash function from RFC 4634.
452
+ * @param msg - message bytes to hash
453
+ * @returns Digest bytes.
454
+ * @example
455
+ * Hash a message with SHA2-512.
456
+ * ```ts
457
+ * sha512(new Uint8Array([97, 98, 99]));
458
+ * ```
459
+ */
460
+ exports.sha512 = (0, utils_1.createHasher)(() => new _SHA512(),
461
+ /* @__PURE__ */ (0, utils_1.oidNist)(0x03));
462
+ /**
463
+ * SHA2-384 hash function from RFC 4634.
464
+ * @param msg - message bytes to hash
465
+ * @returns Digest bytes.
466
+ * @example
467
+ * Hash a message with SHA2-384.
468
+ * ```ts
469
+ * sha384(new Uint8Array([97, 98, 99]));
470
+ * ```
471
+ */
472
+ exports.sha384 = (0, utils_1.createHasher)(() => new _SHA384(),
473
+ /* @__PURE__ */ (0, utils_1.oidNist)(0x02));
474
+ /**
475
+ * SHA2-512/256 "truncated" hash function, with improved resistance to length extension attacks.
476
+ * See the paper on {@link https://eprint.iacr.org/2010/548.pdf | truncated SHA512}.
477
+ * @param msg - message bytes to hash
478
+ * @returns Digest bytes.
479
+ * @example
480
+ * Hash a message with SHA2-512/256.
481
+ * ```ts
482
+ * sha512_256(new Uint8Array([97, 98, 99]));
483
+ * ```
484
+ */
485
+ exports.sha512_256 = (0, utils_1.createHasher)(() => new _SHA512_256(),
486
+ /* @__PURE__ */ (0, utils_1.oidNist)(0x06));
487
+ /**
488
+ * SHA2-512/224 "truncated" hash function, with improved resistance to length extension attacks.
489
+ * See the paper on {@link https://eprint.iacr.org/2010/548.pdf | truncated SHA512}.
490
+ * @param msg - message bytes to hash
491
+ * @returns Digest bytes.
492
+ * @example
493
+ * Hash a message with SHA2-512/224.
494
+ * ```ts
495
+ * sha512_224(new Uint8Array([97, 98, 99]));
496
+ * ```
497
+ */
498
+ exports.sha512_224 = (0, utils_1.createHasher)(() => new _SHA512_224(),
499
+ /* @__PURE__ */ (0, utils_1.oidNist)(0x05));