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