@hive-p2p/browser 1.0.38 → 1.0.40

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.
@@ -0,0 +1,397 @@
1
+ /**
2
+ * SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
3
+ * SHA256 is the fastest hash implementable in JS, even faster than Blake3.
4
+ * Check out [RFC 4634](https://www.rfc-editor.org/rfc/rfc4634) and
5
+ * [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
6
+ * @module
7
+ */
8
+ import { Chi, HashMD, Maj, SHA224_IV, SHA256_IV, SHA384_IV, SHA512_IV } from "./_md.js";
9
+ import * as u64 from "./_u64.js";
10
+ import { clean, createHasher, oidNist, rotr } from "./utils.js";
11
+ /**
12
+ * Round constants:
13
+ * First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
14
+ */
15
+ // prettier-ignore
16
+ const SHA256_K = /* @__PURE__ */ Uint32Array.from([
17
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
18
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
19
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
20
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
21
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
22
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
23
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
24
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
25
+ ]);
26
+ /** Reusable temporary buffer. "W" comes straight from spec. */
27
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
28
+ /** Internal 32-byte base SHA2 hash class. */
29
+ class SHA2_32B extends HashMD {
30
+ constructor(outputLen) {
31
+ super(64, outputLen, 8, false);
32
+ }
33
+ get() {
34
+ const { A, B, C, D, E, F, G, H } = this;
35
+ return [A, B, C, D, E, F, G, H];
36
+ }
37
+ // prettier-ignore
38
+ set(A, B, C, D, E, F, G, H) {
39
+ this.A = A | 0;
40
+ this.B = B | 0;
41
+ this.C = C | 0;
42
+ this.D = D | 0;
43
+ this.E = E | 0;
44
+ this.F = F | 0;
45
+ this.G = G | 0;
46
+ this.H = H | 0;
47
+ }
48
+ process(view, offset) {
49
+ // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
50
+ for (let i = 0; i < 16; i++, offset += 4)
51
+ SHA256_W[i] = view.getUint32(offset, false);
52
+ for (let i = 16; i < 64; i++) {
53
+ const W15 = SHA256_W[i - 15];
54
+ const W2 = SHA256_W[i - 2];
55
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
56
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
57
+ SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
58
+ }
59
+ // Compression function main loop, 64 rounds
60
+ let { A, B, C, D, E, F, G, H } = this;
61
+ for (let i = 0; i < 64; i++) {
62
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
63
+ const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
64
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
65
+ const T2 = (sigma0 + Maj(A, B, C)) | 0;
66
+ H = G;
67
+ G = F;
68
+ F = E;
69
+ E = (D + T1) | 0;
70
+ D = C;
71
+ C = B;
72
+ B = A;
73
+ A = (T1 + T2) | 0;
74
+ }
75
+ // Add the compressed chunk to the current hash value
76
+ A = (A + this.A) | 0;
77
+ B = (B + this.B) | 0;
78
+ C = (C + this.C) | 0;
79
+ D = (D + this.D) | 0;
80
+ E = (E + this.E) | 0;
81
+ F = (F + this.F) | 0;
82
+ G = (G + this.G) | 0;
83
+ H = (H + this.H) | 0;
84
+ this.set(A, B, C, D, E, F, G, H);
85
+ }
86
+ roundClean() {
87
+ clean(SHA256_W);
88
+ }
89
+ destroy() {
90
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
91
+ clean(this.buffer);
92
+ }
93
+ }
94
+ /** Internal SHA2-256 hash class. */
95
+ export class _SHA256 extends SHA2_32B {
96
+ // We cannot use array here since array allows indexing by variable
97
+ // which means optimizer/compiler cannot use registers.
98
+ A = SHA256_IV[0] | 0;
99
+ B = SHA256_IV[1] | 0;
100
+ C = SHA256_IV[2] | 0;
101
+ D = SHA256_IV[3] | 0;
102
+ E = SHA256_IV[4] | 0;
103
+ F = SHA256_IV[5] | 0;
104
+ G = SHA256_IV[6] | 0;
105
+ H = SHA256_IV[7] | 0;
106
+ constructor() {
107
+ super(32);
108
+ }
109
+ }
110
+ /** Internal SHA2-224 hash class. */
111
+ export class _SHA224 extends SHA2_32B {
112
+ A = SHA224_IV[0] | 0;
113
+ B = SHA224_IV[1] | 0;
114
+ C = SHA224_IV[2] | 0;
115
+ D = SHA224_IV[3] | 0;
116
+ E = SHA224_IV[4] | 0;
117
+ F = SHA224_IV[5] | 0;
118
+ G = SHA224_IV[6] | 0;
119
+ H = SHA224_IV[7] | 0;
120
+ constructor() {
121
+ super(28);
122
+ }
123
+ }
124
+ // SHA2-512 is slower than sha256 in js because u64 operations are slow.
125
+ // Round contants
126
+ // First 32 bits of the fractional parts of the cube roots of the first 80 primes 2..409
127
+ // prettier-ignore
128
+ const K512 = /* @__PURE__ */ (() => u64.split([
129
+ '0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
130
+ '0x3956c25bf348b538', '0x59f111f1b605d019', '0x923f82a4af194f9b', '0xab1c5ed5da6d8118',
131
+ '0xd807aa98a3030242', '0x12835b0145706fbe', '0x243185be4ee4b28c', '0x550c7dc3d5ffb4e2',
132
+ '0x72be5d74f27b896f', '0x80deb1fe3b1696b1', '0x9bdc06a725c71235', '0xc19bf174cf692694',
133
+ '0xe49b69c19ef14ad2', '0xefbe4786384f25e3', '0x0fc19dc68b8cd5b5', '0x240ca1cc77ac9c65',
134
+ '0x2de92c6f592b0275', '0x4a7484aa6ea6e483', '0x5cb0a9dcbd41fbd4', '0x76f988da831153b5',
135
+ '0x983e5152ee66dfab', '0xa831c66d2db43210', '0xb00327c898fb213f', '0xbf597fc7beef0ee4',
136
+ '0xc6e00bf33da88fc2', '0xd5a79147930aa725', '0x06ca6351e003826f', '0x142929670a0e6e70',
137
+ '0x27b70a8546d22ffc', '0x2e1b21385c26c926', '0x4d2c6dfc5ac42aed', '0x53380d139d95b3df',
138
+ '0x650a73548baf63de', '0x766a0abb3c77b2a8', '0x81c2c92e47edaee6', '0x92722c851482353b',
139
+ '0xa2bfe8a14cf10364', '0xa81a664bbc423001', '0xc24b8b70d0f89791', '0xc76c51a30654be30',
140
+ '0xd192e819d6ef5218', '0xd69906245565a910', '0xf40e35855771202a', '0x106aa07032bbd1b8',
141
+ '0x19a4c116b8d2d0c8', '0x1e376c085141ab53', '0x2748774cdf8eeb99', '0x34b0bcb5e19b48a8',
142
+ '0x391c0cb3c5c95a63', '0x4ed8aa4ae3418acb', '0x5b9cca4f7763e373', '0x682e6ff3d6b2b8a3',
143
+ '0x748f82ee5defb2fc', '0x78a5636f43172f60', '0x84c87814a1f0ab72', '0x8cc702081a6439ec',
144
+ '0x90befffa23631e28', '0xa4506cebde82bde9', '0xbef9a3f7b2c67915', '0xc67178f2e372532b',
145
+ '0xca273eceea26619c', '0xd186b8c721c0c207', '0xeada7dd6cde0eb1e', '0xf57d4f7fee6ed178',
146
+ '0x06f067aa72176fba', '0x0a637dc5a2c898a6', '0x113f9804bef90dae', '0x1b710b35131c471b',
147
+ '0x28db77f523047d84', '0x32caab7b40c72493', '0x3c9ebe0a15c9bebc', '0x431d67c49c100d4c',
148
+ '0x4cc5d4becb3e42b6', '0x597f299cfc657e2a', '0x5fcb6fab3ad6faec', '0x6c44198c4a475817'
149
+ ].map(n => BigInt(n))))();
150
+ const SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
151
+ const SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
152
+ // Reusable temporary buffers
153
+ const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
154
+ const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
155
+ /** Internal 64-byte base SHA2 hash class. */
156
+ class SHA2_64B extends HashMD {
157
+ constructor(outputLen) {
158
+ super(128, outputLen, 16, false);
159
+ }
160
+ // prettier-ignore
161
+ get() {
162
+ const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
163
+ return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
164
+ }
165
+ // prettier-ignore
166
+ set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
167
+ this.Ah = Ah | 0;
168
+ this.Al = Al | 0;
169
+ this.Bh = Bh | 0;
170
+ this.Bl = Bl | 0;
171
+ this.Ch = Ch | 0;
172
+ this.Cl = Cl | 0;
173
+ this.Dh = Dh | 0;
174
+ this.Dl = Dl | 0;
175
+ this.Eh = Eh | 0;
176
+ this.El = El | 0;
177
+ this.Fh = Fh | 0;
178
+ this.Fl = Fl | 0;
179
+ this.Gh = Gh | 0;
180
+ this.Gl = Gl | 0;
181
+ this.Hh = Hh | 0;
182
+ this.Hl = Hl | 0;
183
+ }
184
+ process(view, offset) {
185
+ // Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
186
+ for (let i = 0; i < 16; i++, offset += 4) {
187
+ SHA512_W_H[i] = view.getUint32(offset);
188
+ SHA512_W_L[i] = view.getUint32((offset += 4));
189
+ }
190
+ for (let i = 16; i < 80; i++) {
191
+ // s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)
192
+ const W15h = SHA512_W_H[i - 15] | 0;
193
+ const W15l = SHA512_W_L[i - 15] | 0;
194
+ const s0h = u64.rotrSH(W15h, W15l, 1) ^ u64.rotrSH(W15h, W15l, 8) ^ u64.shrSH(W15h, W15l, 7);
195
+ const s0l = u64.rotrSL(W15h, W15l, 1) ^ u64.rotrSL(W15h, W15l, 8) ^ u64.shrSL(W15h, W15l, 7);
196
+ // s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)
197
+ const W2h = SHA512_W_H[i - 2] | 0;
198
+ const W2l = SHA512_W_L[i - 2] | 0;
199
+ const s1h = u64.rotrSH(W2h, W2l, 19) ^ u64.rotrBH(W2h, W2l, 61) ^ u64.shrSH(W2h, W2l, 6);
200
+ const s1l = u64.rotrSL(W2h, W2l, 19) ^ u64.rotrBL(W2h, W2l, 61) ^ u64.shrSL(W2h, W2l, 6);
201
+ // SHA256_W[i] = s0 + s1 + SHA256_W[i - 7] + SHA256_W[i - 16];
202
+ const SUMl = u64.add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
203
+ const SUMh = u64.add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
204
+ SHA512_W_H[i] = SUMh | 0;
205
+ SHA512_W_L[i] = SUMl | 0;
206
+ }
207
+ let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
208
+ // Compression function main loop, 80 rounds
209
+ for (let i = 0; i < 80; i++) {
210
+ // S1 := (e rightrotate 14) xor (e rightrotate 18) xor (e rightrotate 41)
211
+ const sigma1h = u64.rotrSH(Eh, El, 14) ^ u64.rotrSH(Eh, El, 18) ^ u64.rotrBH(Eh, El, 41);
212
+ const sigma1l = u64.rotrSL(Eh, El, 14) ^ u64.rotrSL(Eh, El, 18) ^ u64.rotrBL(Eh, El, 41);
213
+ //const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
214
+ const CHIh = (Eh & Fh) ^ (~Eh & Gh);
215
+ const CHIl = (El & Fl) ^ (~El & Gl);
216
+ // T1 = H + sigma1 + Chi(E, F, G) + SHA512_K[i] + SHA512_W[i]
217
+ // prettier-ignore
218
+ const T1ll = u64.add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
219
+ const T1h = u64.add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
220
+ const T1l = T1ll | 0;
221
+ // S0 := (a rightrotate 28) xor (a rightrotate 34) xor (a rightrotate 39)
222
+ const sigma0h = u64.rotrSH(Ah, Al, 28) ^ u64.rotrBH(Ah, Al, 34) ^ u64.rotrBH(Ah, Al, 39);
223
+ const sigma0l = u64.rotrSL(Ah, Al, 28) ^ u64.rotrBL(Ah, Al, 34) ^ u64.rotrBL(Ah, Al, 39);
224
+ const MAJh = (Ah & Bh) ^ (Ah & Ch) ^ (Bh & Ch);
225
+ const MAJl = (Al & Bl) ^ (Al & Cl) ^ (Bl & Cl);
226
+ Hh = Gh | 0;
227
+ Hl = Gl | 0;
228
+ Gh = Fh | 0;
229
+ Gl = Fl | 0;
230
+ Fh = Eh | 0;
231
+ Fl = El | 0;
232
+ ({ h: Eh, l: El } = u64.add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
233
+ Dh = Ch | 0;
234
+ Dl = Cl | 0;
235
+ Ch = Bh | 0;
236
+ Cl = Bl | 0;
237
+ Bh = Ah | 0;
238
+ Bl = Al | 0;
239
+ const All = u64.add3L(T1l, sigma0l, MAJl);
240
+ Ah = u64.add3H(All, T1h, sigma0h, MAJh);
241
+ Al = All | 0;
242
+ }
243
+ // Add the compressed chunk to the current hash value
244
+ ({ h: Ah, l: Al } = u64.add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
245
+ ({ h: Bh, l: Bl } = u64.add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
246
+ ({ h: Ch, l: Cl } = u64.add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
247
+ ({ h: Dh, l: Dl } = u64.add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
248
+ ({ h: Eh, l: El } = u64.add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
249
+ ({ h: Fh, l: Fl } = u64.add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
250
+ ({ h: Gh, l: Gl } = u64.add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
251
+ ({ h: Hh, l: Hl } = u64.add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
252
+ this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
253
+ }
254
+ roundClean() {
255
+ clean(SHA512_W_H, SHA512_W_L);
256
+ }
257
+ destroy() {
258
+ clean(this.buffer);
259
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
260
+ }
261
+ }
262
+ /** Internal SHA2-512 hash class. */
263
+ export class _SHA512 extends SHA2_64B {
264
+ Ah = SHA512_IV[0] | 0;
265
+ Al = SHA512_IV[1] | 0;
266
+ Bh = SHA512_IV[2] | 0;
267
+ Bl = SHA512_IV[3] | 0;
268
+ Ch = SHA512_IV[4] | 0;
269
+ Cl = SHA512_IV[5] | 0;
270
+ Dh = SHA512_IV[6] | 0;
271
+ Dl = SHA512_IV[7] | 0;
272
+ Eh = SHA512_IV[8] | 0;
273
+ El = SHA512_IV[9] | 0;
274
+ Fh = SHA512_IV[10] | 0;
275
+ Fl = SHA512_IV[11] | 0;
276
+ Gh = SHA512_IV[12] | 0;
277
+ Gl = SHA512_IV[13] | 0;
278
+ Hh = SHA512_IV[14] | 0;
279
+ Hl = SHA512_IV[15] | 0;
280
+ constructor() {
281
+ super(64);
282
+ }
283
+ }
284
+ /** Internal SHA2-384 hash class. */
285
+ export class _SHA384 extends SHA2_64B {
286
+ Ah = SHA384_IV[0] | 0;
287
+ Al = SHA384_IV[1] | 0;
288
+ Bh = SHA384_IV[2] | 0;
289
+ Bl = SHA384_IV[3] | 0;
290
+ Ch = SHA384_IV[4] | 0;
291
+ Cl = SHA384_IV[5] | 0;
292
+ Dh = SHA384_IV[6] | 0;
293
+ Dl = SHA384_IV[7] | 0;
294
+ Eh = SHA384_IV[8] | 0;
295
+ El = SHA384_IV[9] | 0;
296
+ Fh = SHA384_IV[10] | 0;
297
+ Fl = SHA384_IV[11] | 0;
298
+ Gh = SHA384_IV[12] | 0;
299
+ Gl = SHA384_IV[13] | 0;
300
+ Hh = SHA384_IV[14] | 0;
301
+ Hl = SHA384_IV[15] | 0;
302
+ constructor() {
303
+ super(48);
304
+ }
305
+ }
306
+ /**
307
+ * Truncated SHA512/256 and SHA512/224.
308
+ * SHA512_IV is XORed with 0xa5a5a5a5a5a5a5a5, then used as "intermediary" IV of SHA512/t.
309
+ * Then t hashes string to produce result IV.
310
+ * See `test/misc/sha2-gen-iv.js`.
311
+ */
312
+ /** SHA512/224 IV */
313
+ const T224_IV = /* @__PURE__ */ Uint32Array.from([
314
+ 0x8c3d37c8, 0x19544da2, 0x73e19966, 0x89dcd4d6, 0x1dfab7ae, 0x32ff9c82, 0x679dd514, 0x582f9fcf,
315
+ 0x0f6d2b69, 0x7bd44da8, 0x77e36f73, 0x04c48942, 0x3f9d85a8, 0x6a1d36c8, 0x1112e6ad, 0x91d692a1,
316
+ ]);
317
+ /** SHA512/256 IV */
318
+ const T256_IV = /* @__PURE__ */ Uint32Array.from([
319
+ 0x22312194, 0xfc2bf72c, 0x9f555fa3, 0xc84c64c2, 0x2393b86b, 0x6f53b151, 0x96387719, 0x5940eabd,
320
+ 0x96283ee2, 0xa88effe3, 0xbe5e1e25, 0x53863992, 0x2b0199fc, 0x2c85b8aa, 0x0eb72ddc, 0x81c52ca2,
321
+ ]);
322
+ /** Internal SHA2-512/224 hash class. */
323
+ export class _SHA512_224 extends SHA2_64B {
324
+ Ah = T224_IV[0] | 0;
325
+ Al = T224_IV[1] | 0;
326
+ Bh = T224_IV[2] | 0;
327
+ Bl = T224_IV[3] | 0;
328
+ Ch = T224_IV[4] | 0;
329
+ Cl = T224_IV[5] | 0;
330
+ Dh = T224_IV[6] | 0;
331
+ Dl = T224_IV[7] | 0;
332
+ Eh = T224_IV[8] | 0;
333
+ El = T224_IV[9] | 0;
334
+ Fh = T224_IV[10] | 0;
335
+ Fl = T224_IV[11] | 0;
336
+ Gh = T224_IV[12] | 0;
337
+ Gl = T224_IV[13] | 0;
338
+ Hh = T224_IV[14] | 0;
339
+ Hl = T224_IV[15] | 0;
340
+ constructor() {
341
+ super(28);
342
+ }
343
+ }
344
+ /** Internal SHA2-512/256 hash class. */
345
+ export class _SHA512_256 extends SHA2_64B {
346
+ Ah = T256_IV[0] | 0;
347
+ Al = T256_IV[1] | 0;
348
+ Bh = T256_IV[2] | 0;
349
+ Bl = T256_IV[3] | 0;
350
+ Ch = T256_IV[4] | 0;
351
+ Cl = T256_IV[5] | 0;
352
+ Dh = T256_IV[6] | 0;
353
+ Dl = T256_IV[7] | 0;
354
+ Eh = T256_IV[8] | 0;
355
+ El = T256_IV[9] | 0;
356
+ Fh = T256_IV[10] | 0;
357
+ Fl = T256_IV[11] | 0;
358
+ Gh = T256_IV[12] | 0;
359
+ Gl = T256_IV[13] | 0;
360
+ Hh = T256_IV[14] | 0;
361
+ Hl = T256_IV[15] | 0;
362
+ constructor() {
363
+ super(32);
364
+ }
365
+ }
366
+ /**
367
+ * SHA2-256 hash function from RFC 4634. In JS it's the fastest: even faster than Blake3. Some info:
368
+ *
369
+ * - Trying 2^128 hashes would get 50% chance of collision, using birthday attack.
370
+ * - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
371
+ * - Each sha256 hash is executing 2^18 bit operations.
372
+ * - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
373
+ */
374
+ export const sha256 = /* @__PURE__ */ createHasher(() => new _SHA256(),
375
+ /* @__PURE__ */ oidNist(0x01));
376
+ /** SHA2-224 hash function from RFC 4634 */
377
+ export const sha224 = /* @__PURE__ */ createHasher(() => new _SHA224(),
378
+ /* @__PURE__ */ oidNist(0x04));
379
+ /** SHA2-512 hash function from RFC 4634. */
380
+ export const sha512 = /* @__PURE__ */ createHasher(() => new _SHA512(),
381
+ /* @__PURE__ */ oidNist(0x03));
382
+ /** SHA2-384 hash function from RFC 4634. */
383
+ export const sha384 = /* @__PURE__ */ createHasher(() => new _SHA384(),
384
+ /* @__PURE__ */ oidNist(0x02));
385
+ /**
386
+ * SHA2-512/256 "truncated" hash function, with improved resistance to length extension attacks.
387
+ * See the paper on [truncated SHA512](https://eprint.iacr.org/2010/548.pdf).
388
+ */
389
+ export const sha512_256 = /* @__PURE__ */ createHasher(() => new _SHA512_256(),
390
+ /* @__PURE__ */ oidNist(0x06));
391
+ /**
392
+ * SHA2-512/224 "truncated" hash function, with improved resistance to length extension attacks.
393
+ * See the paper on [truncated SHA512](https://eprint.iacr.org/2010/548.pdf).
394
+ */
395
+ export const sha512_224 = /* @__PURE__ */ createHasher(() => new _SHA512_224(),
396
+ /* @__PURE__ */ oidNist(0x05));
397
+ //# sourceMappingURL=sha2.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hive-p2p/browser",
3
- "version": "1.0.38",
3
+ "version": "1.0.40",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -156,10 +156,10 @@ class NetworkVisualizer {
156
156
  const previousIndex = (currentIndex - 1 + peerIds.length) % peerIds.length;
157
157
  this.#setSelectedPeer(peerIds[previousIndex]);
158
158
  }
159
- #setCurrentPeer(id, clearNetworkOneChange = true) {
159
+ #setCurrentPeer(id, clearNetworkOnChange = true) {
160
160
  this.currentPeerId = id;
161
161
  this.simulationInterface.currentPeerId = id;
162
- this.networkRenderer.setCurrentPeer(id, clearNetworkOneChange);
162
+ this.networkRenderer.setCurrentPeer(id, clearNetworkOnChange);
163
163
  }
164
164
  #getSimulatorSettings() {
165
165
  return {
@@ -2,10 +2,12 @@ import { Converter } from './converter.mjs';
2
2
  const IS_BROWSER = typeof window !== 'undefined';
3
3
 
4
4
  // ED25519 EXPOSURE NODEJS/BROWSER COMPATIBLE ---------------------------------
5
- const [ed_, {sha512}] = await Promise.all([
6
- import(IS_BROWSER ? 'https://unpkg.com/@noble/ed25519@3.0.0/index.js' : '@noble/ed25519'),
7
- import(IS_BROWSER ? 'https://unpkg.com/@noble/hashes@2.0.0/sha2.js' : '@noble/hashes/sha2.js')
8
- ]);
5
+ /*const [ed_, {sha512}] = await Promise.all([
6
+ import(IS_BROWSER ? '../libs/ed25519-3.0.0.js' : '@noble/ed25519'),
7
+ import(IS_BROWSER ? '../libs/hashes-2.0.0.js' : '@noble/hashes/sha2.js')
8
+ ]);*/
9
+ import * as ed_ from '@noble/ed25519';
10
+ import { sha512 } from '@noble/hashes/sha2.js';
9
11
 
10
12
  /** @type {import('@noble/ed25519')} */
11
13
  const ed25519 = ed_;