@bsv/sdk 1.6.18 → 1.6.20
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.
- package/README.md +11 -11
- package/dist/cjs/package.json +5 -9
- package/dist/cjs/src/auth/transports/SimplifiedFetchTransport.js +39 -39
- package/dist/cjs/src/auth/transports/SimplifiedFetchTransport.js.map +1 -1
- package/dist/cjs/src/primitives/ECDSA.js +69 -167
- package/dist/cjs/src/primitives/ECDSA.js.map +1 -1
- package/dist/cjs/src/primitives/Hash.js +660 -436
- package/dist/cjs/src/primitives/Hash.js.map +1 -1
- package/dist/cjs/src/primitives/Point.js +285 -293
- package/dist/cjs/src/primitives/Point.js.map +1 -1
- package/dist/cjs/src/script/ScriptEvaluationError.js +27 -0
- package/dist/cjs/src/script/ScriptEvaluationError.js.map +1 -0
- package/dist/cjs/src/script/Spend.js +13 -7
- package/dist/cjs/src/script/Spend.js.map +1 -1
- package/dist/cjs/src/script/index.js +3 -1
- package/dist/cjs/src/script/index.js.map +1 -1
- package/dist/cjs/src/transaction/Beef.js +4 -4
- package/dist/cjs/src/transaction/Transaction.js +3 -3
- package/dist/cjs/src/transaction/Transaction.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/auth/transports/SimplifiedFetchTransport.js +39 -39
- package/dist/esm/src/auth/transports/SimplifiedFetchTransport.js.map +1 -1
- package/dist/esm/src/primitives/ECDSA.js +69 -167
- package/dist/esm/src/primitives/ECDSA.js.map +1 -1
- package/dist/esm/src/primitives/Hash.js +672 -444
- package/dist/esm/src/primitives/Hash.js.map +1 -1
- package/dist/esm/src/primitives/Point.js +268 -293
- package/dist/esm/src/primitives/Point.js.map +1 -1
- package/dist/esm/src/script/ScriptEvaluationError.js +33 -0
- package/dist/esm/src/script/ScriptEvaluationError.js.map +1 -0
- package/dist/esm/src/script/Spend.js +14 -8
- package/dist/esm/src/script/Spend.js.map +1 -1
- package/dist/esm/src/script/index.js +1 -0
- package/dist/esm/src/script/index.js.map +1 -1
- package/dist/esm/src/transaction/Beef.js +4 -4
- package/dist/esm/src/transaction/Transaction.js +3 -3
- package/dist/esm/src/transaction/Transaction.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/auth/transports/SimplifiedFetchTransport.d.ts +1 -1
- package/dist/types/src/auth/transports/SimplifiedFetchTransport.d.ts.map +1 -1
- package/dist/types/src/primitives/ECDSA.d.ts.map +1 -1
- package/dist/types/src/primitives/Hash.d.ts +12 -19
- package/dist/types/src/primitives/Hash.d.ts.map +1 -1
- package/dist/types/src/primitives/Point.d.ts +37 -5
- package/dist/types/src/primitives/Point.d.ts.map +1 -1
- package/dist/types/src/script/ScriptEvaluationError.d.ts +24 -0
- package/dist/types/src/script/ScriptEvaluationError.d.ts.map +1 -0
- package/dist/types/src/script/Spend.d.ts.map +1 -1
- package/dist/types/src/script/index.d.ts +1 -0
- package/dist/types/src/script/index.d.ts.map +1 -1
- package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +20 -1
- package/dist/umd/bundle.js.map +1 -0
- package/package.json +5 -9
- package/src/auth/transports/SimplifiedFetchTransport.ts +64 -67
- package/src/primitives/ECDSA.ts +80 -222
- package/src/primitives/Hash.ts +752 -589
- package/src/primitives/Point.ts +277 -336
- package/src/script/ScriptEvaluationError.ts +44 -0
- package/src/script/Spend.ts +14 -12
- package/src/script/index.ts +1 -0
- package/src/transaction/Beef.ts +4 -4
- package/src/transaction/Transaction.ts +11 -3
|
@@ -296,6 +296,12 @@ function zero8(word) {
|
|
|
296
296
|
return word;
|
|
297
297
|
}
|
|
298
298
|
}
|
|
299
|
+
function bytesToHex(data) {
|
|
300
|
+
let res = '';
|
|
301
|
+
for (const b of data)
|
|
302
|
+
res += b.toString(16).padStart(2, '0');
|
|
303
|
+
return res;
|
|
304
|
+
}
|
|
299
305
|
function join32(msg, start, end, endian) {
|
|
300
306
|
const len = end - start;
|
|
301
307
|
assert(len % 4 === 0);
|
|
@@ -359,6 +365,7 @@ function FT_1(s, x, y, z) {
|
|
|
359
365
|
if (s === 2) {
|
|
360
366
|
return maj32(x, y, z);
|
|
361
367
|
}
|
|
368
|
+
return 0;
|
|
362
369
|
}
|
|
363
370
|
function ch32(x, y, z) {
|
|
364
371
|
return (x & y) ^ (~x & z);
|
|
@@ -456,72 +463,6 @@ function Kh(j) {
|
|
|
456
463
|
return 0x00000000;
|
|
457
464
|
}
|
|
458
465
|
}
|
|
459
|
-
function sum64(buf, pos, ah, al) {
|
|
460
|
-
const bh = buf[pos];
|
|
461
|
-
const bl = buf[pos + 1];
|
|
462
|
-
const lo = (al + bl) >>> 0;
|
|
463
|
-
const hi = (lo < al ? 1 : 0) + ah + bh;
|
|
464
|
-
buf[pos] = hi >>> 0;
|
|
465
|
-
buf[pos + 1] = lo;
|
|
466
|
-
}
|
|
467
|
-
function sum64HI(ah, al, bh, bl) {
|
|
468
|
-
const lo = (al + bl) >>> 0;
|
|
469
|
-
const hi = (lo < al ? 1 : 0) + ah + bh;
|
|
470
|
-
return hi >>> 0;
|
|
471
|
-
}
|
|
472
|
-
function sum64LO(ah, al, bh, bl) {
|
|
473
|
-
const lo = al + bl;
|
|
474
|
-
return lo >>> 0;
|
|
475
|
-
}
|
|
476
|
-
function sum64and4HI(ah, al, bh, bl, ch, cl, dh, dl) {
|
|
477
|
-
let carry = 0;
|
|
478
|
-
let lo = al;
|
|
479
|
-
lo = (lo + bl) >>> 0;
|
|
480
|
-
carry += lo < al ? 1 : 0;
|
|
481
|
-
lo = (lo + cl) >>> 0;
|
|
482
|
-
carry += lo < cl ? 1 : 0;
|
|
483
|
-
lo = (lo + dl) >>> 0;
|
|
484
|
-
carry += lo < dl ? 1 : 0;
|
|
485
|
-
const hi = ah + bh + ch + dh + carry;
|
|
486
|
-
return hi >>> 0;
|
|
487
|
-
}
|
|
488
|
-
function sum64and4LO(ah, al, bh, bl, ch, cl, dh, dl) {
|
|
489
|
-
const lo = al + bl + cl + dl;
|
|
490
|
-
return lo >>> 0;
|
|
491
|
-
}
|
|
492
|
-
function sum64and5HI(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
|
|
493
|
-
let carry = 0;
|
|
494
|
-
let lo = al;
|
|
495
|
-
lo = (lo + bl) >>> 0;
|
|
496
|
-
carry += lo < al ? 1 : 0;
|
|
497
|
-
lo = (lo + cl) >>> 0;
|
|
498
|
-
carry += lo < cl ? 1 : 0;
|
|
499
|
-
lo = (lo + dl) >>> 0;
|
|
500
|
-
carry += lo < dl ? 1 : 0;
|
|
501
|
-
lo = (lo + el) >>> 0;
|
|
502
|
-
carry += lo < el ? 1 : 0;
|
|
503
|
-
const hi = ah + bh + ch + dh + eh + carry;
|
|
504
|
-
return hi >>> 0;
|
|
505
|
-
}
|
|
506
|
-
function sum64and5LO(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
|
|
507
|
-
const lo = al + bl + cl + dl + el;
|
|
508
|
-
return lo >>> 0;
|
|
509
|
-
}
|
|
510
|
-
function rotr64HI(ah, al, num) {
|
|
511
|
-
const r = (al << (32 - num)) | (ah >>> num);
|
|
512
|
-
return r >>> 0;
|
|
513
|
-
}
|
|
514
|
-
function rotr64LO(ah, al, num) {
|
|
515
|
-
const r = (ah << (32 - num)) | (al >>> num);
|
|
516
|
-
return r >>> 0;
|
|
517
|
-
}
|
|
518
|
-
function shr64HI(ah, al, num) {
|
|
519
|
-
return ah >>> num;
|
|
520
|
-
}
|
|
521
|
-
function shr64LO(ah, al, num) {
|
|
522
|
-
const r = (ah << (32 - num)) | (al >>> num);
|
|
523
|
-
return r >>> 0;
|
|
524
|
-
}
|
|
525
466
|
/**
|
|
526
467
|
* An implementation of RIPEMD160 cryptographic hash function. Extends the BaseHash class.
|
|
527
468
|
* It provides a way to compute a 'digest' for any kind of input data; transforming the data
|
|
@@ -606,76 +547,20 @@ exports.RIPEMD160 = RIPEMD160;
|
|
|
606
547
|
* @property W - Provides a way to recycle usage of the array memory.
|
|
607
548
|
* @property k - The round constants used for each round of SHA-256
|
|
608
549
|
*/
|
|
609
|
-
class SHA256
|
|
550
|
+
class SHA256 {
|
|
610
551
|
constructor() {
|
|
611
|
-
|
|
612
|
-
this.h = [
|
|
613
|
-
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c,
|
|
614
|
-
0x1f83d9ab, 0x5be0cd19
|
|
615
|
-
];
|
|
616
|
-
this.k = [
|
|
617
|
-
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
|
618
|
-
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
619
|
-
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
|
620
|
-
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
621
|
-
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
|
622
|
-
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
623
|
-
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
|
624
|
-
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
625
|
-
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
|
626
|
-
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
627
|
-
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
628
|
-
];
|
|
629
|
-
this.W = new Array(64);
|
|
552
|
+
this.h = new FastSHA256();
|
|
630
553
|
}
|
|
631
|
-
|
|
632
|
-
const
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
start = 0;
|
|
636
|
-
}
|
|
637
|
-
let i;
|
|
638
|
-
for (i = 0; i < 16; i++) {
|
|
639
|
-
W[i] = msg[start + i];
|
|
640
|
-
}
|
|
641
|
-
for (; i < W.length; i++) {
|
|
642
|
-
W[i] = SUM32_4(G1_256(W[i - 2]), W[i - 7], G0_256(W[i - 15]), W[i - 16]);
|
|
643
|
-
}
|
|
644
|
-
let a = this.h[0];
|
|
645
|
-
let b = this.h[1];
|
|
646
|
-
let c = this.h[2];
|
|
647
|
-
let d = this.h[3];
|
|
648
|
-
let e = this.h[4];
|
|
649
|
-
let f = this.h[5];
|
|
650
|
-
let g = this.h[6];
|
|
651
|
-
let h = this.h[7];
|
|
652
|
-
assert(this.k.length === W.length);
|
|
653
|
-
for (i = 0; i < W.length; i++) {
|
|
654
|
-
const T1 = SUM32_5(h, S1_256(e), ch32(e, f, g), this.k[i], W[i]);
|
|
655
|
-
const T2 = sum32(S0_256(a), maj32(a, b, c));
|
|
656
|
-
h = g;
|
|
657
|
-
g = f;
|
|
658
|
-
f = e;
|
|
659
|
-
e = sum32(d, T1);
|
|
660
|
-
d = c;
|
|
661
|
-
c = b;
|
|
662
|
-
b = a;
|
|
663
|
-
a = sum32(T1, T2);
|
|
664
|
-
}
|
|
665
|
-
this.h[0] = sum32(this.h[0], a);
|
|
666
|
-
this.h[1] = sum32(this.h[1], b);
|
|
667
|
-
this.h[2] = sum32(this.h[2], c);
|
|
668
|
-
this.h[3] = sum32(this.h[3], d);
|
|
669
|
-
this.h[4] = sum32(this.h[4], e);
|
|
670
|
-
this.h[5] = sum32(this.h[5], f);
|
|
671
|
-
this.h[6] = sum32(this.h[6], g);
|
|
672
|
-
this.h[7] = sum32(this.h[7], h);
|
|
554
|
+
update(msg, enc) {
|
|
555
|
+
const data = Uint8Array.from(toArray(msg, enc));
|
|
556
|
+
this.h.update(data);
|
|
557
|
+
return this;
|
|
673
558
|
}
|
|
674
|
-
|
|
675
|
-
return
|
|
559
|
+
digest() {
|
|
560
|
+
return Array.from(this.h.digest());
|
|
676
561
|
}
|
|
677
|
-
|
|
678
|
-
return
|
|
562
|
+
digestHex() {
|
|
563
|
+
return bytesToHex(this.h.digest());
|
|
679
564
|
}
|
|
680
565
|
}
|
|
681
566
|
exports.SHA256 = SHA256;
|
|
@@ -765,246 +650,23 @@ exports.SHA1 = SHA1;
|
|
|
765
650
|
* @property W - Provides a way to recycle usage of the array memory.
|
|
766
651
|
* @property k - The round constants used for each round of SHA-512.
|
|
767
652
|
*/
|
|
768
|
-
class SHA512
|
|
653
|
+
class SHA512 {
|
|
769
654
|
constructor() {
|
|
770
|
-
|
|
771
|
-
this.h = [
|
|
772
|
-
0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b,
|
|
773
|
-
0xa54ff53a, 0x5f1d36f1, 0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f,
|
|
774
|
-
0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179
|
|
775
|
-
];
|
|
776
|
-
this.k = [
|
|
777
|
-
0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd, 0xb5c0fbcf, 0xec4d3b2f,
|
|
778
|
-
0xe9b5dba5, 0x8189dbbc, 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
|
|
779
|
-
0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118, 0xd807aa98, 0xa3030242,
|
|
780
|
-
0x12835b01, 0x45706fbe, 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
|
|
781
|
-
0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1, 0x9bdc06a7, 0x25c71235,
|
|
782
|
-
0xc19bf174, 0xcf692694, 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
|
|
783
|
-
0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65, 0x2de92c6f, 0x592b0275,
|
|
784
|
-
0x4a7484aa, 0x6ea6e483, 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
|
|
785
|
-
0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210, 0xb00327c8, 0x98fb213f,
|
|
786
|
-
0xbf597fc7, 0xbeef0ee4, 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
|
|
787
|
-
0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70, 0x27b70a85, 0x46d22ffc,
|
|
788
|
-
0x2e1b2138, 0x5c26c926, 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
|
|
789
|
-
0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8, 0x81c2c92e, 0x47edaee6,
|
|
790
|
-
0x92722c85, 0x1482353b, 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
|
|
791
|
-
0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30, 0xd192e819, 0xd6ef5218,
|
|
792
|
-
0xd6990624, 0x5565a910, 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
|
|
793
|
-
0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53, 0x2748774c, 0xdf8eeb99,
|
|
794
|
-
0x34b0bcb5, 0xe19b48a8, 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
|
|
795
|
-
0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3, 0x748f82ee, 0x5defb2fc,
|
|
796
|
-
0x78a5636f, 0x43172f60, 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
|
|
797
|
-
0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9, 0xbef9a3f7, 0xb2c67915,
|
|
798
|
-
0xc67178f2, 0xe372532b, 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
|
|
799
|
-
0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178, 0x06f067aa, 0x72176fba,
|
|
800
|
-
0x0a637dc5, 0xa2c898a6, 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
|
|
801
|
-
0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493, 0x3c9ebe0a, 0x15c9bebc,
|
|
802
|
-
0x431d67c4, 0x9c100d4c, 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
|
|
803
|
-
0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
|
|
804
|
-
];
|
|
805
|
-
this.W = new Array(160);
|
|
806
|
-
}
|
|
807
|
-
_prepareBlock(msg, start) {
|
|
808
|
-
const W = this.W;
|
|
809
|
-
// 32 x 32bit words
|
|
810
|
-
let i;
|
|
811
|
-
for (i = 0; i < 32; i++) {
|
|
812
|
-
W[i] = msg[start + i];
|
|
813
|
-
}
|
|
814
|
-
for (; i < W.length; i += 2) {
|
|
815
|
-
const c0Hi = g1_512_hi(W[i - 4], W[i - 3]); // i - 2
|
|
816
|
-
const c0Lo = g1_512_lo(W[i - 4], W[i - 3]);
|
|
817
|
-
const c1Hi = W[i - 14]; // i - 7
|
|
818
|
-
const c1Lo = W[i - 13];
|
|
819
|
-
const c2Hi = g0_512_hi(W[i - 30], W[i - 29]); // i - 15
|
|
820
|
-
const c2Lo = g0_512_lo(W[i - 30], W[i - 29]);
|
|
821
|
-
const c3Hi = W[i - 32]; // i - 16
|
|
822
|
-
const c3Lo = W[i - 31];
|
|
823
|
-
W[i] = sum64and4HI(c0Hi, c0Lo, c1Hi, c1Lo, c2Hi, c2Lo, c3Hi, c3Lo);
|
|
824
|
-
W[i + 1] = sum64and4LO(c0Hi, c0Lo, c1Hi, c1Lo, c2Hi, c2Lo, c3Hi, c3Lo);
|
|
825
|
-
}
|
|
655
|
+
this.h = new FastSHA512();
|
|
826
656
|
}
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
let aLow = this.h[1];
|
|
832
|
-
let bHigh = this.h[2];
|
|
833
|
-
let bLow = this.h[3];
|
|
834
|
-
let cHigh = this.h[4];
|
|
835
|
-
let cLow = this.h[5];
|
|
836
|
-
let dHigh = this.h[6];
|
|
837
|
-
let dLow = this.h[7];
|
|
838
|
-
let eHigh = this.h[8];
|
|
839
|
-
let eLow = this.h[9];
|
|
840
|
-
let fHigh = this.h[10];
|
|
841
|
-
let fLow = this.h[11];
|
|
842
|
-
let gHigh = this.h[12];
|
|
843
|
-
let gLow = this.h[13];
|
|
844
|
-
let hHigh = this.h[14];
|
|
845
|
-
let hLow = this.h[15];
|
|
846
|
-
assert(this.k.length === W.length);
|
|
847
|
-
for (let i = 0; i < W.length; i += 2) {
|
|
848
|
-
let temp0High = hHigh;
|
|
849
|
-
let temp0Low = hLow;
|
|
850
|
-
let temp1High = s1_512_hi(eHigh, eLow);
|
|
851
|
-
let temp1Low = s1_512_lo(eHigh, eLow);
|
|
852
|
-
const temp2High = ch64_hi(eHigh, eLow, fHigh, fLow, gHigh, gLow);
|
|
853
|
-
const temp2Low = ch64_lo(eHigh, eLow, fHigh, fLow, gHigh, gLow);
|
|
854
|
-
const temp3High = this.k[i];
|
|
855
|
-
const temp3Low = this.k[i + 1];
|
|
856
|
-
const temp4High = W[i];
|
|
857
|
-
const temp4Low = W[i + 1];
|
|
858
|
-
const t1High = sum64and5HI(temp0High, temp0Low, temp1High, temp1Low, temp2High, temp2Low, temp3High, temp3Low, temp4High, temp4Low);
|
|
859
|
-
const t1Low = sum64and5LO(temp0High, temp0Low, temp1High, temp1Low, temp2High, temp2Low, temp3High, temp3Low, temp4High, temp4Low);
|
|
860
|
-
temp0High = s0_512_hi(aHigh, aLow);
|
|
861
|
-
temp0Low = s0_512_lo(aHigh, aLow);
|
|
862
|
-
temp1High = maj64_hi(aHigh, aLow, bHigh, bLow, cHigh, cLow);
|
|
863
|
-
temp1Low = maj64_lo(aHigh, aLow, bHigh, bLow, cHigh, cLow);
|
|
864
|
-
const t2High = sum64HI(temp0High, temp0Low, temp1High, temp1Low);
|
|
865
|
-
const t2Low = sum64LO(temp0High, temp0Low, temp1High, temp1Low);
|
|
866
|
-
hHigh = gHigh;
|
|
867
|
-
hLow = gLow;
|
|
868
|
-
gHigh = fHigh;
|
|
869
|
-
gLow = fLow;
|
|
870
|
-
fHigh = eHigh;
|
|
871
|
-
fLow = eLow;
|
|
872
|
-
eHigh = sum64HI(dHigh, dLow, t1High, t1Low);
|
|
873
|
-
eLow = sum64LO(dLow, dLow, t1High, t1Low);
|
|
874
|
-
dHigh = cHigh;
|
|
875
|
-
dLow = cLow;
|
|
876
|
-
cHigh = bHigh;
|
|
877
|
-
cLow = bLow;
|
|
878
|
-
bHigh = aHigh;
|
|
879
|
-
bLow = aLow;
|
|
880
|
-
aHigh = sum64HI(t1High, t1Low, t2High, t2Low);
|
|
881
|
-
aLow = sum64LO(t1High, t1Low, t2High, t2Low);
|
|
882
|
-
}
|
|
883
|
-
sum64(this.h, 0, aHigh, aLow);
|
|
884
|
-
sum64(this.h, 2, bHigh, bLow);
|
|
885
|
-
sum64(this.h, 4, cHigh, cLow);
|
|
886
|
-
sum64(this.h, 6, dHigh, dLow);
|
|
887
|
-
sum64(this.h, 8, eHigh, eLow);
|
|
888
|
-
sum64(this.h, 10, fHigh, fLow);
|
|
889
|
-
sum64(this.h, 12, gHigh, gLow);
|
|
890
|
-
sum64(this.h, 14, hHigh, hLow);
|
|
657
|
+
update(msg, enc) {
|
|
658
|
+
const data = Uint8Array.from(toArray(msg, enc));
|
|
659
|
+
this.h.update(data);
|
|
660
|
+
return this;
|
|
891
661
|
}
|
|
892
|
-
|
|
893
|
-
return
|
|
662
|
+
digest() {
|
|
663
|
+
return Array.from(this.h.digest());
|
|
894
664
|
}
|
|
895
|
-
|
|
896
|
-
return
|
|
665
|
+
digestHex() {
|
|
666
|
+
return bytesToHex(this.h.digest());
|
|
897
667
|
}
|
|
898
668
|
}
|
|
899
669
|
exports.SHA512 = SHA512;
|
|
900
|
-
function ch64_hi(xh, xl, yh, yl, zh, zl) {
|
|
901
|
-
let r = (xh & yh) ^ (~xh & zh);
|
|
902
|
-
if (r < 0) {
|
|
903
|
-
r += 0x100000000;
|
|
904
|
-
}
|
|
905
|
-
return r;
|
|
906
|
-
}
|
|
907
|
-
function ch64_lo(xh, xl, yh, yl, zh, zl) {
|
|
908
|
-
let r = (xl & yl) ^ (~xl & zl);
|
|
909
|
-
if (r < 0) {
|
|
910
|
-
r += 0x100000000;
|
|
911
|
-
}
|
|
912
|
-
return r;
|
|
913
|
-
}
|
|
914
|
-
function maj64_hi(xh, xl, yh, yl, zh, zl) {
|
|
915
|
-
let r = (xh & yh) ^ (xh & zh) ^ (yh & zh);
|
|
916
|
-
if (r < 0) {
|
|
917
|
-
r += 0x100000000;
|
|
918
|
-
}
|
|
919
|
-
return r;
|
|
920
|
-
}
|
|
921
|
-
function maj64_lo(xh, xl, yh, yl, zh, zl) {
|
|
922
|
-
let r = (xl & yl) ^ (xl & zl) ^ (yl & zl);
|
|
923
|
-
if (r < 0) {
|
|
924
|
-
r += 0x100000000;
|
|
925
|
-
}
|
|
926
|
-
return r;
|
|
927
|
-
}
|
|
928
|
-
function s0_512_hi(xh, xl) {
|
|
929
|
-
const c0_hi = rotr64HI(xh, xl, 28);
|
|
930
|
-
const c1_hi = rotr64HI(xl, xh, 2); // 34
|
|
931
|
-
const c2_hi = rotr64HI(xl, xh, 7); // 39
|
|
932
|
-
let r = c0_hi ^ c1_hi ^ c2_hi;
|
|
933
|
-
if (r < 0) {
|
|
934
|
-
r += 0x100000000;
|
|
935
|
-
}
|
|
936
|
-
return r;
|
|
937
|
-
}
|
|
938
|
-
function s0_512_lo(xh, xl) {
|
|
939
|
-
const c0_lo = rotr64LO(xh, xl, 28);
|
|
940
|
-
const c1_lo = rotr64LO(xl, xh, 2); // 34
|
|
941
|
-
const c2_lo = rotr64LO(xl, xh, 7); // 39
|
|
942
|
-
let r = c0_lo ^ c1_lo ^ c2_lo;
|
|
943
|
-
if (r < 0) {
|
|
944
|
-
r += 0x100000000;
|
|
945
|
-
}
|
|
946
|
-
return r;
|
|
947
|
-
}
|
|
948
|
-
function s1_512_hi(xh, xl) {
|
|
949
|
-
const c0_hi = rotr64HI(xh, xl, 14);
|
|
950
|
-
const c1_hi = rotr64HI(xh, xl, 18);
|
|
951
|
-
const c2_hi = rotr64HI(xl, xh, 9); // 41
|
|
952
|
-
let r = c0_hi ^ c1_hi ^ c2_hi;
|
|
953
|
-
if (r < 0) {
|
|
954
|
-
r += 0x100000000;
|
|
955
|
-
}
|
|
956
|
-
return r;
|
|
957
|
-
}
|
|
958
|
-
function s1_512_lo(xh, xl) {
|
|
959
|
-
const c0_lo = rotr64LO(xh, xl, 14);
|
|
960
|
-
const c1_lo = rotr64LO(xh, xl, 18);
|
|
961
|
-
const c2_lo = rotr64LO(xl, xh, 9); // 41
|
|
962
|
-
let r = c0_lo ^ c1_lo ^ c2_lo;
|
|
963
|
-
if (r < 0) {
|
|
964
|
-
r += 0x100000000;
|
|
965
|
-
}
|
|
966
|
-
return r;
|
|
967
|
-
}
|
|
968
|
-
function g0_512_hi(xh, xl) {
|
|
969
|
-
const c0_hi = rotr64HI(xh, xl, 1);
|
|
970
|
-
const c1_hi = rotr64HI(xh, xl, 8);
|
|
971
|
-
const c2_hi = shr64HI(xh, xl, 7);
|
|
972
|
-
let r = c0_hi ^ c1_hi ^ c2_hi;
|
|
973
|
-
if (r < 0) {
|
|
974
|
-
r += 0x100000000;
|
|
975
|
-
}
|
|
976
|
-
return r;
|
|
977
|
-
}
|
|
978
|
-
function g0_512_lo(xh, xl) {
|
|
979
|
-
const c0_lo = rotr64LO(xh, xl, 1);
|
|
980
|
-
const c1_lo = rotr64LO(xh, xl, 8);
|
|
981
|
-
const c2_lo = shr64LO(xh, xl, 7);
|
|
982
|
-
let r = c0_lo ^ c1_lo ^ c2_lo;
|
|
983
|
-
if (r < 0) {
|
|
984
|
-
r += 0x100000000;
|
|
985
|
-
}
|
|
986
|
-
return r;
|
|
987
|
-
}
|
|
988
|
-
function g1_512_hi(xh, xl) {
|
|
989
|
-
const c0_hi = rotr64HI(xh, xl, 19);
|
|
990
|
-
const c1_hi = rotr64HI(xl, xh, 29); // 61
|
|
991
|
-
const c2_hi = shr64HI(xh, xl, 6);
|
|
992
|
-
let r = c0_hi ^ c1_hi ^ c2_hi;
|
|
993
|
-
if (r < 0) {
|
|
994
|
-
r += 0x100000000;
|
|
995
|
-
}
|
|
996
|
-
return r;
|
|
997
|
-
}
|
|
998
|
-
function g1_512_lo(xh, xl) {
|
|
999
|
-
const c0_lo = rotr64LO(xh, xl, 19);
|
|
1000
|
-
const c1_lo = rotr64LO(xl, xh, 29); // 61
|
|
1001
|
-
const c2_lo = shr64LO(xh, xl, 6);
|
|
1002
|
-
let r = c0_lo ^ c1_lo ^ c2_lo;
|
|
1003
|
-
if (r < 0) {
|
|
1004
|
-
r += 0x100000000;
|
|
1005
|
-
}
|
|
1006
|
-
return r;
|
|
1007
|
-
}
|
|
1008
670
|
/**
|
|
1009
671
|
* The `SHA256HMAC` class is used to create Hash-based Message Authentication Code (HMAC) using the SHA-256 cryptographic hash function.
|
|
1010
672
|
*
|
|
@@ -1034,26 +696,8 @@ class SHA256HMAC {
|
|
|
1034
696
|
constructor(key) {
|
|
1035
697
|
this.blockSize = 64;
|
|
1036
698
|
this.outSize = 32;
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
if (key.length > this.blockSize) {
|
|
1040
|
-
key = new SHA256().update(key).digest();
|
|
1041
|
-
}
|
|
1042
|
-
assert(key.length <= this.blockSize);
|
|
1043
|
-
// Add padding to key
|
|
1044
|
-
let i;
|
|
1045
|
-
for (i = key.length; i < this.blockSize; i++) {
|
|
1046
|
-
key.push(0);
|
|
1047
|
-
}
|
|
1048
|
-
for (i = 0; i < key.length; i++) {
|
|
1049
|
-
key[i] ^= 0x36;
|
|
1050
|
-
}
|
|
1051
|
-
this.inner = new SHA256().update(key);
|
|
1052
|
-
// 0x36 ^ 0x5c = 0x6a
|
|
1053
|
-
for (i = 0; i < key.length; i++) {
|
|
1054
|
-
key[i] ^= 0x6a;
|
|
1055
|
-
}
|
|
1056
|
-
this.outer = new SHA256().update(key);
|
|
699
|
+
const k = Uint8Array.from(toArray(key, 'hex'));
|
|
700
|
+
this.h = new HMAC(sha256Fast, k);
|
|
1057
701
|
}
|
|
1058
702
|
/**
|
|
1059
703
|
* Updates the `SHA256HMAC` object with part of the message to be hashed.
|
|
@@ -1067,7 +711,7 @@ class SHA256HMAC {
|
|
|
1067
711
|
* myHMAC.update('deadbeef', 'hex');
|
|
1068
712
|
*/
|
|
1069
713
|
update(msg, enc) {
|
|
1070
|
-
this.
|
|
714
|
+
this.h.update(Uint8Array.from(toArray(msg, enc)));
|
|
1071
715
|
return this;
|
|
1072
716
|
}
|
|
1073
717
|
/**
|
|
@@ -1080,8 +724,7 @@ class SHA256HMAC {
|
|
|
1080
724
|
* let hashedMessage = myHMAC.digest();
|
|
1081
725
|
*/
|
|
1082
726
|
digest() {
|
|
1083
|
-
|
|
1084
|
-
return this.outer.digest();
|
|
727
|
+
return Array.from(this.h.digest());
|
|
1085
728
|
}
|
|
1086
729
|
/**
|
|
1087
730
|
* Finalizes the HMAC computation and returns the resultant hash as a hex string.
|
|
@@ -1093,8 +736,7 @@ class SHA256HMAC {
|
|
|
1093
736
|
* let hashedMessage = myHMAC.digestHex();
|
|
1094
737
|
*/
|
|
1095
738
|
digestHex() {
|
|
1096
|
-
|
|
1097
|
-
return this.outer.digestHex();
|
|
739
|
+
return bytesToHex(this.h.digest());
|
|
1098
740
|
}
|
|
1099
741
|
}
|
|
1100
742
|
exports.SHA256HMAC = SHA256HMAC;
|
|
@@ -1164,26 +806,8 @@ class SHA512HMAC {
|
|
|
1164
806
|
constructor(key) {
|
|
1165
807
|
this.blockSize = 128;
|
|
1166
808
|
this.outSize = 32;
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
if (key.length > this.blockSize) {
|
|
1170
|
-
key = new SHA512().update(key).digest();
|
|
1171
|
-
}
|
|
1172
|
-
assert(key.length <= this.blockSize);
|
|
1173
|
-
// Add padding to key
|
|
1174
|
-
let i;
|
|
1175
|
-
for (i = key.length; i < this.blockSize; i++) {
|
|
1176
|
-
key.push(0);
|
|
1177
|
-
}
|
|
1178
|
-
for (i = 0; i < key.length; i++) {
|
|
1179
|
-
key[i] ^= 0x36;
|
|
1180
|
-
}
|
|
1181
|
-
this.inner = new SHA512().update(key);
|
|
1182
|
-
// 0x36 ^ 0x5c = 0x6a
|
|
1183
|
-
for (i = 0; i < key.length; i++) {
|
|
1184
|
-
key[i] ^= 0x6a;
|
|
1185
|
-
}
|
|
1186
|
-
this.outer = new SHA512().update(key);
|
|
809
|
+
const k = Uint8Array.from(toArray(key, 'hex'));
|
|
810
|
+
this.h = new HMAC(sha512Fast, k);
|
|
1187
811
|
}
|
|
1188
812
|
/**
|
|
1189
813
|
* Updates the `SHA512HMAC` object with part of the message to be hashed.
|
|
@@ -1197,7 +821,7 @@ class SHA512HMAC {
|
|
|
1197
821
|
* myHMAC.update('deadbeef', 'hex');
|
|
1198
822
|
*/
|
|
1199
823
|
update(msg, enc) {
|
|
1200
|
-
this.
|
|
824
|
+
this.h.update(Uint8Array.from(toArray(msg, enc)));
|
|
1201
825
|
return this;
|
|
1202
826
|
}
|
|
1203
827
|
/**
|
|
@@ -1210,8 +834,7 @@ class SHA512HMAC {
|
|
|
1210
834
|
* let hashedMessage = myHMAC.digest();
|
|
1211
835
|
*/
|
|
1212
836
|
digest() {
|
|
1213
|
-
|
|
1214
|
-
return this.outer.digest();
|
|
837
|
+
return Array.from(this.h.digest());
|
|
1215
838
|
}
|
|
1216
839
|
/**
|
|
1217
840
|
* Finalizes the HMAC computation and returns the resultant hash as a hex string.
|
|
@@ -1223,8 +846,7 @@ class SHA512HMAC {
|
|
|
1223
846
|
* let hashedMessage = myHMAC.digestHex();
|
|
1224
847
|
*/
|
|
1225
848
|
digestHex() {
|
|
1226
|
-
|
|
1227
|
-
return this.outer.digestHex();
|
|
849
|
+
return bytesToHex(this.h.digest());
|
|
1228
850
|
}
|
|
1229
851
|
}
|
|
1230
852
|
exports.SHA512HMAC = SHA512HMAC;
|
|
@@ -1355,6 +977,611 @@ const sha512hmac = (key, msg, enc) => {
|
|
|
1355
977
|
return new SHA512HMAC(key).update(msg, enc).digest();
|
|
1356
978
|
};
|
|
1357
979
|
exports.sha512hmac = sha512hmac;
|
|
980
|
+
// BEGIN fast-pbkdf2 helpers
|
|
981
|
+
// Utils
|
|
982
|
+
function isBytes(a) {
|
|
983
|
+
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
|
984
|
+
}
|
|
985
|
+
function anumber(n) {
|
|
986
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
987
|
+
throw new Error(`positive integer expected, got ${n}`);
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
function abytes(b, ...lengths) {
|
|
991
|
+
if (!isBytes(b))
|
|
992
|
+
throw new Error('Uint8Array expected');
|
|
993
|
+
if (lengths.length > 0 && !lengths.includes(b.length)) {
|
|
994
|
+
const lens = lengths.join(',');
|
|
995
|
+
throw new Error(`Uint8Array expected of length ${lens}, got length=${b.length}`);
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
function ahash(h) {
|
|
999
|
+
if (typeof h !== 'function' || typeof h.create !== 'function') {
|
|
1000
|
+
throw new Error('Hash should be wrapped by utils.createHasher');
|
|
1001
|
+
}
|
|
1002
|
+
anumber(h.outputLen);
|
|
1003
|
+
anumber(h.blockLen);
|
|
1004
|
+
}
|
|
1005
|
+
function aexists(instance, checkFinished = true) {
|
|
1006
|
+
if (instance.destroyed === true)
|
|
1007
|
+
throw new Error('Hash instance has been destroyed');
|
|
1008
|
+
if (checkFinished && instance.finished === true) {
|
|
1009
|
+
throw new Error('Hash#digest() has already been called');
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
function aoutput(out, instance) {
|
|
1013
|
+
abytes(out);
|
|
1014
|
+
const min = instance.outputLen;
|
|
1015
|
+
if (out.length < min) {
|
|
1016
|
+
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
function clean(...arrays) {
|
|
1020
|
+
for (let i = 0; i < arrays.length; i++)
|
|
1021
|
+
arrays[i].fill(0);
|
|
1022
|
+
}
|
|
1023
|
+
function createView(arr) {
|
|
1024
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1025
|
+
}
|
|
1026
|
+
function toBytes(data) {
|
|
1027
|
+
if (typeof data === 'string')
|
|
1028
|
+
data = utf8ToBytes(data);
|
|
1029
|
+
abytes(data);
|
|
1030
|
+
return data;
|
|
1031
|
+
}
|
|
1032
|
+
function utf8ToBytes(str) {
|
|
1033
|
+
if (typeof str !== 'string')
|
|
1034
|
+
throw new Error('string expected');
|
|
1035
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
1036
|
+
}
|
|
1037
|
+
function kdfInputToBytes(data) {
|
|
1038
|
+
if (typeof data === 'string')
|
|
1039
|
+
data = utf8ToBytes(data);
|
|
1040
|
+
abytes(data);
|
|
1041
|
+
return data;
|
|
1042
|
+
}
|
|
1043
|
+
class Hash {
|
|
1044
|
+
}
|
|
1045
|
+
function createHasher(hashCons) {
|
|
1046
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
1047
|
+
const tmp = hashCons();
|
|
1048
|
+
hashC.outputLen = tmp.outputLen;
|
|
1049
|
+
hashC.blockLen = tmp.blockLen;
|
|
1050
|
+
hashC.create = () => hashCons();
|
|
1051
|
+
return hashC;
|
|
1052
|
+
}
|
|
1053
|
+
// u64 helpers
|
|
1054
|
+
const U32_MASK64 = BigInt(2 ** 32 - 1);
|
|
1055
|
+
const _32n = BigInt(32);
|
|
1056
|
+
function fromBig(n, le = false) {
|
|
1057
|
+
if (le)
|
|
1058
|
+
return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
|
|
1059
|
+
return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
1060
|
+
}
|
|
1061
|
+
function split(lst, le = false) {
|
|
1062
|
+
const len = lst.length;
|
|
1063
|
+
const Ah = new Uint32Array(len);
|
|
1064
|
+
const Al = new Uint32Array(len);
|
|
1065
|
+
for (let i = 0; i < len; i++) {
|
|
1066
|
+
const { h, l } = fromBig(lst[i], le);
|
|
1067
|
+
Ah[i] = h;
|
|
1068
|
+
Al[i] = l;
|
|
1069
|
+
}
|
|
1070
|
+
return [Ah, Al];
|
|
1071
|
+
}
|
|
1072
|
+
const shrSH = (h, _l, s) => h >>> s;
|
|
1073
|
+
const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
1074
|
+
const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
|
|
1075
|
+
const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
1076
|
+
const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
|
|
1077
|
+
const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
|
|
1078
|
+
function add(Ah, Al, Bh, Bl) {
|
|
1079
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
1080
|
+
return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
|
|
1081
|
+
}
|
|
1082
|
+
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
1083
|
+
const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
|
|
1084
|
+
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
1085
|
+
const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
|
|
1086
|
+
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
1087
|
+
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
|
|
1088
|
+
// _md helpers
|
|
1089
|
+
class HashMD extends Hash {
|
|
1090
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
1091
|
+
super();
|
|
1092
|
+
this.finished = false;
|
|
1093
|
+
this.length = 0;
|
|
1094
|
+
this.pos = 0;
|
|
1095
|
+
this.destroyed = false;
|
|
1096
|
+
this.blockLen = blockLen;
|
|
1097
|
+
this.outputLen = outputLen;
|
|
1098
|
+
this.padOffset = padOffset;
|
|
1099
|
+
this.isLE = isLE;
|
|
1100
|
+
this.buffer = new Uint8Array(blockLen);
|
|
1101
|
+
this.view = createView(this.buffer);
|
|
1102
|
+
}
|
|
1103
|
+
update(data) {
|
|
1104
|
+
aexists(this);
|
|
1105
|
+
data = toBytes(data);
|
|
1106
|
+
abytes(data);
|
|
1107
|
+
const { view, buffer, blockLen } = this;
|
|
1108
|
+
const len = data.length;
|
|
1109
|
+
for (let pos = 0; pos < len;) {
|
|
1110
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
1111
|
+
if (take === blockLen) {
|
|
1112
|
+
const dataView = createView(data);
|
|
1113
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
1114
|
+
this.process(dataView, pos);
|
|
1115
|
+
continue;
|
|
1116
|
+
}
|
|
1117
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
1118
|
+
this.pos += take;
|
|
1119
|
+
pos += take;
|
|
1120
|
+
if (this.pos === blockLen) {
|
|
1121
|
+
this.process(view, 0);
|
|
1122
|
+
this.pos = 0;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
this.length += data.length;
|
|
1126
|
+
this.roundClean();
|
|
1127
|
+
return this;
|
|
1128
|
+
}
|
|
1129
|
+
digestInto(out) {
|
|
1130
|
+
aexists(this);
|
|
1131
|
+
aoutput(out, this);
|
|
1132
|
+
this.finished = true;
|
|
1133
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
1134
|
+
let { pos } = this;
|
|
1135
|
+
buffer[pos++] = 0b10000000;
|
|
1136
|
+
clean(this.buffer.subarray(pos));
|
|
1137
|
+
if (this.padOffset > blockLen - pos) {
|
|
1138
|
+
this.process(view, 0);
|
|
1139
|
+
pos = 0;
|
|
1140
|
+
}
|
|
1141
|
+
for (let i = pos; i < blockLen; i++)
|
|
1142
|
+
buffer[i] = 0;
|
|
1143
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
1144
|
+
this.process(view, 0);
|
|
1145
|
+
const oview = createView(out);
|
|
1146
|
+
const len = this.outputLen;
|
|
1147
|
+
if (len % 4 !== 0)
|
|
1148
|
+
throw new Error('_sha2: outputLen should be aligned to 32bit');
|
|
1149
|
+
const outLen = len / 4;
|
|
1150
|
+
const state = this.get();
|
|
1151
|
+
if (outLen > state.length)
|
|
1152
|
+
throw new Error('_sha2: outputLen bigger than state');
|
|
1153
|
+
for (let i = 0; i < outLen; i++)
|
|
1154
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
1155
|
+
}
|
|
1156
|
+
digest() {
|
|
1157
|
+
const { buffer, outputLen } = this;
|
|
1158
|
+
this.digestInto(buffer);
|
|
1159
|
+
const res = buffer.slice(0, outputLen);
|
|
1160
|
+
this.destroy();
|
|
1161
|
+
return res;
|
|
1162
|
+
}
|
|
1163
|
+
_cloneInto(to) {
|
|
1164
|
+
to || (to = new this.constructor());
|
|
1165
|
+
to.set(...this.get());
|
|
1166
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
1167
|
+
to.destroyed = destroyed;
|
|
1168
|
+
to.finished = finished;
|
|
1169
|
+
to.length = length;
|
|
1170
|
+
to.pos = pos;
|
|
1171
|
+
if (length % blockLen !== 0)
|
|
1172
|
+
to.buffer.set(buffer);
|
|
1173
|
+
return to;
|
|
1174
|
+
}
|
|
1175
|
+
clone() {
|
|
1176
|
+
return this._cloneInto();
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
function setBigUint64(view, byteOffset, value, isLE) {
|
|
1180
|
+
if (typeof view.setBigUint64 === 'function')
|
|
1181
|
+
return view.setBigUint64(byteOffset, value, isLE);
|
|
1182
|
+
const _32n = BigInt(32);
|
|
1183
|
+
const _u32_max = BigInt(0xffffffff);
|
|
1184
|
+
const wh = Number((value >> _32n) & _u32_max);
|
|
1185
|
+
const wl = Number(value & _u32_max);
|
|
1186
|
+
const h = isLE ? 4 : 0;
|
|
1187
|
+
const l = isLE ? 0 : 4;
|
|
1188
|
+
view.setUint32(byteOffset + h, wh, isLE);
|
|
1189
|
+
view.setUint32(byteOffset + l, wl, isLE);
|
|
1190
|
+
}
|
|
1191
|
+
// sha256 fast constants
|
|
1192
|
+
const SHA256_IV = Uint32Array.from([
|
|
1193
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
|
1194
|
+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
|
1195
|
+
]);
|
|
1196
|
+
const K256 = Uint32Array.from([
|
|
1197
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
|
1198
|
+
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
1199
|
+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
|
1200
|
+
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
1201
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
|
1202
|
+
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
1203
|
+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
|
1204
|
+
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
1205
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
|
1206
|
+
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
1207
|
+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
1208
|
+
]);
|
|
1209
|
+
const SHA256_W = new Uint32Array(64);
|
|
1210
|
+
class FastSHA256 extends HashMD {
|
|
1211
|
+
constructor(outputLen = 32) {
|
|
1212
|
+
super(64, outputLen, 8, false);
|
|
1213
|
+
this.A = SHA256_IV[0] | 0;
|
|
1214
|
+
this.B = SHA256_IV[1] | 0;
|
|
1215
|
+
this.C = SHA256_IV[2] | 0;
|
|
1216
|
+
this.D = SHA256_IV[3] | 0;
|
|
1217
|
+
this.E = SHA256_IV[4] | 0;
|
|
1218
|
+
this.F = SHA256_IV[5] | 0;
|
|
1219
|
+
this.G = SHA256_IV[6] | 0;
|
|
1220
|
+
this.H = SHA256_IV[7] | 0;
|
|
1221
|
+
}
|
|
1222
|
+
get() {
|
|
1223
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
1224
|
+
return [A, B, C, D, E, F, G, H];
|
|
1225
|
+
}
|
|
1226
|
+
set(A, B, C, D, E, F, G, H) {
|
|
1227
|
+
this.A = A | 0;
|
|
1228
|
+
this.B = B | 0;
|
|
1229
|
+
this.C = C | 0;
|
|
1230
|
+
this.D = D | 0;
|
|
1231
|
+
this.E = E | 0;
|
|
1232
|
+
this.F = F | 0;
|
|
1233
|
+
this.G = G | 0;
|
|
1234
|
+
this.H = H | 0;
|
|
1235
|
+
}
|
|
1236
|
+
process(view, offset) {
|
|
1237
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
1238
|
+
SHA256_W[i] = view.getUint32(offset);
|
|
1239
|
+
}
|
|
1240
|
+
for (let i = 16; i < 64; i++) {
|
|
1241
|
+
const w15 = SHA256_W[i - 15];
|
|
1242
|
+
const w2 = SHA256_W[i - 2];
|
|
1243
|
+
const s0 = G0_256(w15);
|
|
1244
|
+
const s1 = G1_256(w2);
|
|
1245
|
+
SHA256_W[i] = sum32(sum32(s0, SHA256_W[i - 7]), sum32(s1, SHA256_W[i - 16]));
|
|
1246
|
+
}
|
|
1247
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
1248
|
+
for (let i = 0; i < 64; i++) {
|
|
1249
|
+
const T1 = SUM32_5(H, S1_256(E), ch32(E, F, G), K256[i], SHA256_W[i]);
|
|
1250
|
+
const T2 = sum32(S0_256(A), maj32(A, B, C));
|
|
1251
|
+
H = G;
|
|
1252
|
+
G = F;
|
|
1253
|
+
F = E;
|
|
1254
|
+
E = sum32(D, T1);
|
|
1255
|
+
D = C;
|
|
1256
|
+
C = B;
|
|
1257
|
+
B = A;
|
|
1258
|
+
A = sum32(T1, T2);
|
|
1259
|
+
}
|
|
1260
|
+
this.A = sum32(this.A, A);
|
|
1261
|
+
this.B = sum32(this.B, B);
|
|
1262
|
+
this.C = sum32(this.C, C);
|
|
1263
|
+
this.D = sum32(this.D, D);
|
|
1264
|
+
this.E = sum32(this.E, E);
|
|
1265
|
+
this.F = sum32(this.F, F);
|
|
1266
|
+
this.G = sum32(this.G, G);
|
|
1267
|
+
this.H = sum32(this.H, H);
|
|
1268
|
+
}
|
|
1269
|
+
roundClean() {
|
|
1270
|
+
clean(SHA256_W);
|
|
1271
|
+
}
|
|
1272
|
+
destroy() {
|
|
1273
|
+
clean(this.buffer);
|
|
1274
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
const sha256Fast = createHasher(() => new FastSHA256());
|
|
1278
|
+
// sha512
|
|
1279
|
+
const SHA512_IV = Uint32Array.from([
|
|
1280
|
+
0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
|
|
1281
|
+
0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179
|
|
1282
|
+
]);
|
|
1283
|
+
const K512 = (() => split([
|
|
1284
|
+
'0x428a2f98d728ae22',
|
|
1285
|
+
'0x7137449123ef65cd',
|
|
1286
|
+
'0xb5c0fbcfec4d3b2f',
|
|
1287
|
+
'0xe9b5dba58189dbbc',
|
|
1288
|
+
'0x3956c25bf348b538',
|
|
1289
|
+
'0x59f111f1b605d019',
|
|
1290
|
+
'0x923f82a4af194f9b',
|
|
1291
|
+
'0xab1c5ed5da6d8118',
|
|
1292
|
+
'0xd807aa98a3030242',
|
|
1293
|
+
'0x12835b0145706fbe',
|
|
1294
|
+
'0x243185be4ee4b28c',
|
|
1295
|
+
'0x550c7dc3d5ffb4e2',
|
|
1296
|
+
'0x72be5d74f27b896f',
|
|
1297
|
+
'0x80deb1fe3b1696b1',
|
|
1298
|
+
'0x9bdc06a725c71235',
|
|
1299
|
+
'0xc19bf174cf692694',
|
|
1300
|
+
'0xe49b69c19ef14ad2',
|
|
1301
|
+
'0xefbe4786384f25e3',
|
|
1302
|
+
'0x0fc19dc68b8cd5b5',
|
|
1303
|
+
'0x240ca1cc77ac9c65',
|
|
1304
|
+
'0x2de92c6f592b0275',
|
|
1305
|
+
'0x4a7484aa6ea6e483',
|
|
1306
|
+
'0x5cb0a9dcbd41fbd4',
|
|
1307
|
+
'0x76f988da831153b5',
|
|
1308
|
+
'0x983e5152ee66dfab',
|
|
1309
|
+
'0xa831c66d2db43210',
|
|
1310
|
+
'0xb00327c898fb213f',
|
|
1311
|
+
'0xbf597fc7beef0ee4',
|
|
1312
|
+
'0xc6e00bf33da88fc2',
|
|
1313
|
+
'0xd5a79147930aa725',
|
|
1314
|
+
'0x06ca6351e003826f',
|
|
1315
|
+
'0x142929670a0e6e70',
|
|
1316
|
+
'0x27b70a8546d22ffc',
|
|
1317
|
+
'0x2e1b21385c26c926',
|
|
1318
|
+
'0x4d2c6dfc5ac42aed',
|
|
1319
|
+
'0x53380d139d95b3df',
|
|
1320
|
+
'0x650a73548baf63de',
|
|
1321
|
+
'0x766a0abb3c77b2a8',
|
|
1322
|
+
'0x81c2c92e47edaee6',
|
|
1323
|
+
'0x92722c851482353b',
|
|
1324
|
+
'0xa2bfe8a14cf10364',
|
|
1325
|
+
'0xa81a664bbc423001',
|
|
1326
|
+
'0xc24b8b70d0f89791',
|
|
1327
|
+
'0xc76c51a30654be30',
|
|
1328
|
+
'0xd192e819d6ef5218',
|
|
1329
|
+
'0xd69906245565a910',
|
|
1330
|
+
'0xf40e35855771202a',
|
|
1331
|
+
'0x106aa07032bbd1b8',
|
|
1332
|
+
'0x19a4c116b8d2d0c8',
|
|
1333
|
+
'0x1e376c085141ab53',
|
|
1334
|
+
'0x2748774cdf8eeb99',
|
|
1335
|
+
'0x34b0bcb5e19b48a8',
|
|
1336
|
+
'0x391c0cb3c5c95a63',
|
|
1337
|
+
'0x4ed8aa4ae3418acb',
|
|
1338
|
+
'0x5b9cca4f7763e373',
|
|
1339
|
+
'0x682e6ff3d6b2b8a3',
|
|
1340
|
+
'0x748f82ee5defb2fc',
|
|
1341
|
+
'0x78a5636f43172f60',
|
|
1342
|
+
'0x84c87814a1f0ab72',
|
|
1343
|
+
'0x8cc702081a6439ec',
|
|
1344
|
+
'0x90befffa23631e28',
|
|
1345
|
+
'0xa4506cebde82bde9',
|
|
1346
|
+
'0xbef9a3f7b2c67915',
|
|
1347
|
+
'0xc67178f2e372532b',
|
|
1348
|
+
'0xca273eceea26619c',
|
|
1349
|
+
'0xd186b8c721c0c207',
|
|
1350
|
+
'0xeada7dd6cde0eb1e',
|
|
1351
|
+
'0xf57d4f7fee6ed178',
|
|
1352
|
+
'0x06f067aa72176fba',
|
|
1353
|
+
'0x0a637dc5a2c898a6',
|
|
1354
|
+
'0x113f9804bef90dae',
|
|
1355
|
+
'0x1b710b35131c471b',
|
|
1356
|
+
'0x28db77f523047d84',
|
|
1357
|
+
'0x32caab7b40c72493',
|
|
1358
|
+
'0x3c9ebe0a15c9bebc',
|
|
1359
|
+
'0x431d67c49c100d4c',
|
|
1360
|
+
'0x4cc5d4becb3e42b6',
|
|
1361
|
+
'0x597f299cfc657e2a',
|
|
1362
|
+
'0x5fcb6fab3ad6faec',
|
|
1363
|
+
'0x6c44198c4a475817'
|
|
1364
|
+
].map((n) => BigInt(n))))();
|
|
1365
|
+
const SHA512_Kh = (() => K512[0])();
|
|
1366
|
+
const SHA512_Kl = (() => K512[1])();
|
|
1367
|
+
const SHA512_W_H = new Uint32Array(80);
|
|
1368
|
+
const SHA512_W_L = new Uint32Array(80);
|
|
1369
|
+
class FastSHA512 extends HashMD {
|
|
1370
|
+
constructor(outputLen = 64) {
|
|
1371
|
+
super(128, outputLen, 16, false);
|
|
1372
|
+
this.Ah = SHA512_IV[0] | 0;
|
|
1373
|
+
this.Al = SHA512_IV[1] | 0;
|
|
1374
|
+
this.Bh = SHA512_IV[2] | 0;
|
|
1375
|
+
this.Bl = SHA512_IV[3] | 0;
|
|
1376
|
+
this.Ch = SHA512_IV[4] | 0;
|
|
1377
|
+
this.Cl = SHA512_IV[5] | 0;
|
|
1378
|
+
this.Dh = SHA512_IV[6] | 0;
|
|
1379
|
+
this.Dl = SHA512_IV[7] | 0;
|
|
1380
|
+
this.Eh = SHA512_IV[8] | 0;
|
|
1381
|
+
this.El = SHA512_IV[9] | 0;
|
|
1382
|
+
this.Fh = SHA512_IV[10] | 0;
|
|
1383
|
+
this.Fl = SHA512_IV[11] | 0;
|
|
1384
|
+
this.Gh = SHA512_IV[12] | 0;
|
|
1385
|
+
this.Gl = SHA512_IV[13] | 0;
|
|
1386
|
+
this.Hh = SHA512_IV[14] | 0;
|
|
1387
|
+
this.Hl = SHA512_IV[15] | 0;
|
|
1388
|
+
}
|
|
1389
|
+
get() {
|
|
1390
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1391
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
1392
|
+
}
|
|
1393
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
1394
|
+
this.Ah = Ah | 0;
|
|
1395
|
+
this.Al = Al | 0;
|
|
1396
|
+
this.Bh = Bh | 0;
|
|
1397
|
+
this.Bl = Bl | 0;
|
|
1398
|
+
this.Ch = Ch | 0;
|
|
1399
|
+
this.Cl = Cl | 0;
|
|
1400
|
+
this.Dh = Dh | 0;
|
|
1401
|
+
this.Dl = Dl | 0;
|
|
1402
|
+
this.Eh = Eh | 0;
|
|
1403
|
+
this.El = El | 0;
|
|
1404
|
+
this.Fh = Fh | 0;
|
|
1405
|
+
this.Fl = Fl | 0;
|
|
1406
|
+
this.Gh = Gh | 0;
|
|
1407
|
+
this.Gl = Gl | 0;
|
|
1408
|
+
this.Hh = Hh | 0;
|
|
1409
|
+
this.Hl = Hl | 0;
|
|
1410
|
+
}
|
|
1411
|
+
process(view, offset) {
|
|
1412
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
1413
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
1414
|
+
SHA512_W_L[i] = view.getUint32((offset += 4));
|
|
1415
|
+
}
|
|
1416
|
+
for (let i = 16; i < 80; i++) {
|
|
1417
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
1418
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
1419
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
1420
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
1421
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
1422
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
1423
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
1424
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
1425
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
1426
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
1427
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
1428
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
1429
|
+
}
|
|
1430
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1431
|
+
for (let i = 0; i < 80; i++) {
|
|
1432
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
1433
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
1434
|
+
const CHIh = (Eh & Fh) ^ (~Eh & Gh);
|
|
1435
|
+
const CHIl = (El & Fl) ^ (~El & Gl);
|
|
1436
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
1437
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
1438
|
+
const T1l = T1ll | 0;
|
|
1439
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
1440
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
1441
|
+
const MAJh = (Ah & Bh) ^ (Ah & Ch) ^ (Bh & Ch);
|
|
1442
|
+
const MAJl = (Al & Bl) ^ (Al & Cl) ^ (Bl & Cl);
|
|
1443
|
+
Hh = Gh | 0;
|
|
1444
|
+
Hl = Gl | 0;
|
|
1445
|
+
Gh = Fh | 0;
|
|
1446
|
+
Gl = Fl | 0;
|
|
1447
|
+
Fh = Eh | 0;
|
|
1448
|
+
Fl = El | 0;
|
|
1449
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
1450
|
+
Dh = Ch | 0;
|
|
1451
|
+
Dl = Cl | 0;
|
|
1452
|
+
Ch = Bh | 0;
|
|
1453
|
+
Cl = Bl | 0;
|
|
1454
|
+
Bh = Ah | 0;
|
|
1455
|
+
Bl = Al | 0;
|
|
1456
|
+
const T2l = add3L(sigma0l, MAJl, T1l);
|
|
1457
|
+
Ah = add3H(T2l, sigma0h, MAJh, T1h);
|
|
1458
|
+
Al = T2l | 0;
|
|
1459
|
+
}
|
|
1460
|
+
;
|
|
1461
|
+
({ h: Ah, l: Al } = add(Ah, Al, this.Ah, this.Al));
|
|
1462
|
+
({ h: Bh, l: Bl } = add(Bh, Bl, this.Bh, this.Bl));
|
|
1463
|
+
({ h: Ch, l: Cl } = add(Ch, Cl, this.Ch, this.Cl));
|
|
1464
|
+
({ h: Dh, l: Dl } = add(Dh, Dl, this.Dh, this.Dl));
|
|
1465
|
+
({ h: Eh, l: El } = add(Eh, El, this.Eh, this.El));
|
|
1466
|
+
({ h: Fh, l: Fl } = add(Fh, Fl, this.Fh, this.Fl));
|
|
1467
|
+
({ h: Gh, l: Gl } = add(Gh, Gl, this.Gh, this.Gl));
|
|
1468
|
+
({ h: Hh, l: Hl } = add(Hh, Hl, this.Hh, this.Hl));
|
|
1469
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
1470
|
+
}
|
|
1471
|
+
roundClean() {
|
|
1472
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
1473
|
+
}
|
|
1474
|
+
destroy() {
|
|
1475
|
+
clean(this.buffer);
|
|
1476
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
const sha512Fast = createHasher(() => new FastSHA512());
|
|
1480
|
+
class HMAC extends Hash {
|
|
1481
|
+
constructor(hash, _key) {
|
|
1482
|
+
super();
|
|
1483
|
+
this.finished = false;
|
|
1484
|
+
this.destroyed = false;
|
|
1485
|
+
ahash(hash);
|
|
1486
|
+
const key = toBytes(_key);
|
|
1487
|
+
this.iHash = hash.create();
|
|
1488
|
+
if (typeof this.iHash.update !== 'function') {
|
|
1489
|
+
throw new Error('Expected instance of class which extends utils.Hash');
|
|
1490
|
+
}
|
|
1491
|
+
this.blockLen = this.iHash.blockLen;
|
|
1492
|
+
this.outputLen = this.iHash.outputLen;
|
|
1493
|
+
const blockLen = this.blockLen;
|
|
1494
|
+
const pad = new Uint8Array(blockLen);
|
|
1495
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
1496
|
+
for (let i = 0; i < pad.length; i++)
|
|
1497
|
+
pad[i] ^= 0x36;
|
|
1498
|
+
this.iHash.update(pad);
|
|
1499
|
+
this.oHash = hash.create();
|
|
1500
|
+
for (let i = 0; i < pad.length; i++)
|
|
1501
|
+
pad[i] ^= 0x36 ^ 0x5c;
|
|
1502
|
+
this.oHash.update(pad);
|
|
1503
|
+
clean(pad);
|
|
1504
|
+
}
|
|
1505
|
+
update(buf) {
|
|
1506
|
+
aexists(this);
|
|
1507
|
+
this.iHash.update(buf);
|
|
1508
|
+
return this;
|
|
1509
|
+
}
|
|
1510
|
+
digestInto(out) {
|
|
1511
|
+
aexists(this);
|
|
1512
|
+
abytes(out, this.outputLen);
|
|
1513
|
+
this.finished = true;
|
|
1514
|
+
this.iHash.digestInto(out);
|
|
1515
|
+
this.oHash.update(out);
|
|
1516
|
+
this.oHash.digestInto(out);
|
|
1517
|
+
this.destroy();
|
|
1518
|
+
}
|
|
1519
|
+
digest() {
|
|
1520
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
1521
|
+
this.digestInto(out);
|
|
1522
|
+
return out;
|
|
1523
|
+
}
|
|
1524
|
+
_cloneInto(to) {
|
|
1525
|
+
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
|
1526
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
1527
|
+
to = to;
|
|
1528
|
+
to.finished = finished;
|
|
1529
|
+
to.destroyed = destroyed;
|
|
1530
|
+
to.blockLen = blockLen;
|
|
1531
|
+
to.outputLen = outputLen;
|
|
1532
|
+
to.oHash = oHash._cloneInto(to.oHash ?? undefined);
|
|
1533
|
+
to.iHash = iHash._cloneInto(to.iHash ?? undefined);
|
|
1534
|
+
return to;
|
|
1535
|
+
}
|
|
1536
|
+
clone() {
|
|
1537
|
+
return this._cloneInto();
|
|
1538
|
+
}
|
|
1539
|
+
destroy() {
|
|
1540
|
+
this.destroyed = true;
|
|
1541
|
+
this.oHash.destroy();
|
|
1542
|
+
this.iHash.destroy();
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
function pbkdf2Core(hash, password, salt, opts) {
|
|
1546
|
+
ahash(hash);
|
|
1547
|
+
const { c, dkLen } = Object.assign({ dkLen: 32 }, opts);
|
|
1548
|
+
anumber(c);
|
|
1549
|
+
anumber(dkLen);
|
|
1550
|
+
if (c < 1)
|
|
1551
|
+
throw new Error('iterations (c) should be >= 1');
|
|
1552
|
+
const pwd = kdfInputToBytes(password);
|
|
1553
|
+
const slt = kdfInputToBytes(salt);
|
|
1554
|
+
const DK = new Uint8Array(dkLen);
|
|
1555
|
+
const PRF = hmac.create(hash, pwd);
|
|
1556
|
+
const PRFSalt = PRF._cloneInto().update(slt);
|
|
1557
|
+
let prfW;
|
|
1558
|
+
const arr = new Uint8Array(4);
|
|
1559
|
+
const view = createView(arr);
|
|
1560
|
+
const u = new Uint8Array(PRF.outputLen);
|
|
1561
|
+
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
|
|
1562
|
+
const Ti = DK.subarray(pos, pos + PRF.outputLen);
|
|
1563
|
+
view.setInt32(0, ti, false);
|
|
1564
|
+
(prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
|
|
1565
|
+
Ti.set(u.subarray(0, Ti.length));
|
|
1566
|
+
for (let ui = 1; ui < c; ui++) {
|
|
1567
|
+
PRF._cloneInto(prfW).update(u).digestInto(u);
|
|
1568
|
+
for (let i = 0; i < Ti.length; i++)
|
|
1569
|
+
Ti[i] ^= u[i];
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
PRF.destroy();
|
|
1573
|
+
PRFSalt.destroy();
|
|
1574
|
+
if (prfW != null)
|
|
1575
|
+
prfW.destroy();
|
|
1576
|
+
clean(u);
|
|
1577
|
+
return DK;
|
|
1578
|
+
}
|
|
1579
|
+
const hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
|
1580
|
+
hmac.create = (hash, key) => new HMAC(hash, key);
|
|
1581
|
+
function pbkdf2Fast(password, salt, iterations, keylen) {
|
|
1582
|
+
return pbkdf2Core(sha512Fast, password, salt, { c: iterations, dkLen: keylen });
|
|
1583
|
+
}
|
|
1584
|
+
// END fast-pbkdf2 helpers
|
|
1358
1585
|
/**
|
|
1359
1586
|
* Limited SHA-512-only PBKDF2 function for use in deprecated BIP39 code.
|
|
1360
1587
|
* @function pbkdf2
|
|
@@ -1370,29 +1597,26 @@ function pbkdf2(password, salt, iterations, keylen, digest = 'sha512') {
|
|
|
1370
1597
|
if (digest !== 'sha512') {
|
|
1371
1598
|
throw new Error('Only sha512 is supported in this PBKDF2 implementation');
|
|
1372
1599
|
}
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
let U = T;
|
|
1385
|
-
for (let j = 1; j < iterations; j++) {
|
|
1386
|
-
U = (0, exports.sha512hmac)(password, U);
|
|
1387
|
-
for (let k = 0; k < hLen; k++)
|
|
1388
|
-
T[k] ^= U[k];
|
|
1600
|
+
// Attempt to use the native Node.js implementation if available as it is
|
|
1601
|
+
// considerably faster than the pure TypeScript fallback below. If the crypto
|
|
1602
|
+
// module isn't present (for example in a browser build) we'll silently fall
|
|
1603
|
+
// back to the original implementation.
|
|
1604
|
+
try {
|
|
1605
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
1606
|
+
const nodeCrypto = require('crypto');
|
|
1607
|
+
if (typeof nodeCrypto.pbkdf2Sync === 'function') {
|
|
1608
|
+
const p = Buffer.from(password);
|
|
1609
|
+
const s = Buffer.from(salt);
|
|
1610
|
+
return [...nodeCrypto.pbkdf2Sync(p, s, iterations, keylen, digest)];
|
|
1389
1611
|
}
|
|
1390
|
-
for (let i = 0; i < T.length; i++) {
|
|
1391
|
-
DK[destPos + i] = T[i];
|
|
1392
|
-
}
|
|
1393
|
-
destPos += hLen;
|
|
1394
1612
|
}
|
|
1395
|
-
|
|
1613
|
+
catch {
|
|
1614
|
+
// ignore
|
|
1615
|
+
}
|
|
1616
|
+
const p = Uint8Array.from(password);
|
|
1617
|
+
const s = Uint8Array.from(salt);
|
|
1618
|
+
const out = pbkdf2Fast(p, s, iterations, keylen);
|
|
1619
|
+
return Array.from(out);
|
|
1396
1620
|
}
|
|
1397
1621
|
exports.pbkdf2 = pbkdf2;
|
|
1398
1622
|
//# sourceMappingURL=Hash.js.map
|