@livechat/accounts-sdk 2.0.3 → 2.0.4
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/dist/accounts-sdk.js +555 -1169
- package/dist/accounts-sdk.js.map +1 -1
- package/dist/accounts-sdk.min.js +4 -4
- package/dist/accounts-sdk.min.js.map +1 -1
- package/dist/index.cjs.js +666 -3
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +665 -1
- package/dist/index.esm.js.map +1 -1
- package/package.json +2 -3
- package/readme.md +37 -20
package/dist/index.esm.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import qs from 'qs';
|
|
2
2
|
import Cookie from 'js-cookie';
|
|
3
|
-
import sjcl from 'sjcl';
|
|
4
3
|
|
|
5
4
|
var errors = {
|
|
6
5
|
extend: function (error) {
|
|
@@ -491,6 +490,671 @@ class Transaction {
|
|
|
491
490
|
}
|
|
492
491
|
}
|
|
493
492
|
|
|
493
|
+
/** @fileOverview Javascript cryptography implementation.
|
|
494
|
+
*
|
|
495
|
+
* Crush to remove comments, shorten variable names and
|
|
496
|
+
* generally reduce transmission size.
|
|
497
|
+
*
|
|
498
|
+
* @author Emily Stark
|
|
499
|
+
* @author Mike Hamburg
|
|
500
|
+
* @author Dan Boneh
|
|
501
|
+
*/
|
|
502
|
+
/*jslint indent: 2, bitwise: false, nomen: false, plusplus: false, white: false, regexp: false */
|
|
503
|
+
/*global document, window, escape, unescape, module, require, Uint32Array */
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* The Stanford Javascript Crypto Library, top-level namespace.
|
|
507
|
+
* @namespace
|
|
508
|
+
*/
|
|
509
|
+
var sjcl = {
|
|
510
|
+
/**
|
|
511
|
+
* Symmetric ciphers.
|
|
512
|
+
* @namespace
|
|
513
|
+
*/
|
|
514
|
+
cipher: {},
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Hash functions. Right now only SHA256 is implemented.
|
|
518
|
+
* @namespace
|
|
519
|
+
*/
|
|
520
|
+
hash: {},
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Key exchange functions. Right now only SRP is implemented.
|
|
524
|
+
* @namespace
|
|
525
|
+
*/
|
|
526
|
+
keyexchange: {},
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Cipher modes of operation.
|
|
530
|
+
* @namespace
|
|
531
|
+
*/
|
|
532
|
+
mode: {},
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Miscellaneous. HMAC and PBKDF2.
|
|
536
|
+
* @namespace
|
|
537
|
+
*/
|
|
538
|
+
misc: {},
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Bit array encoders and decoders.
|
|
542
|
+
* @namespace
|
|
543
|
+
*
|
|
544
|
+
* @description
|
|
545
|
+
* The members of this namespace are functions which translate between
|
|
546
|
+
* SJCL's bitArrays and other objects (usually strings). Because it
|
|
547
|
+
* isn't always clear which direction is encoding and which is decoding,
|
|
548
|
+
* the method names are "fromBits" and "toBits".
|
|
549
|
+
*/
|
|
550
|
+
codec: {},
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Exceptions.
|
|
554
|
+
* @namespace
|
|
555
|
+
*/
|
|
556
|
+
exception: {
|
|
557
|
+
/**
|
|
558
|
+
* Ciphertext is corrupt.
|
|
559
|
+
* @constructor
|
|
560
|
+
*/
|
|
561
|
+
corrupt: function (message) {
|
|
562
|
+
this.toString = function () {
|
|
563
|
+
return 'CORRUPT: ' + this.message;
|
|
564
|
+
};
|
|
565
|
+
this.message = message;
|
|
566
|
+
},
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Invalid parameter.
|
|
570
|
+
* @constructor
|
|
571
|
+
*/
|
|
572
|
+
invalid: function (message) {
|
|
573
|
+
this.toString = function () {
|
|
574
|
+
return 'INVALID: ' + this.message;
|
|
575
|
+
};
|
|
576
|
+
this.message = message;
|
|
577
|
+
},
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Bug or missing feature in SJCL.
|
|
581
|
+
* @constructor
|
|
582
|
+
*/
|
|
583
|
+
bug: function (message) {
|
|
584
|
+
this.toString = function () {
|
|
585
|
+
return 'BUG: ' + this.message;
|
|
586
|
+
};
|
|
587
|
+
this.message = message;
|
|
588
|
+
},
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Something isn't ready.
|
|
592
|
+
* @constructor
|
|
593
|
+
*/
|
|
594
|
+
notReady: function (message) {
|
|
595
|
+
this.toString = function () {
|
|
596
|
+
return 'NOT READY: ' + this.message;
|
|
597
|
+
};
|
|
598
|
+
this.message = message;
|
|
599
|
+
},
|
|
600
|
+
},
|
|
601
|
+
};
|
|
602
|
+
/** @fileOverview Arrays of bits, encoded as arrays of Numbers.
|
|
603
|
+
*
|
|
604
|
+
* @author Emily Stark
|
|
605
|
+
* @author Mike Hamburg
|
|
606
|
+
* @author Dan Boneh
|
|
607
|
+
*/
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Arrays of bits, encoded as arrays of Numbers.
|
|
611
|
+
* @namespace
|
|
612
|
+
* @description
|
|
613
|
+
* <p>
|
|
614
|
+
* These objects are the currency accepted by SJCL's crypto functions.
|
|
615
|
+
* </p>
|
|
616
|
+
*
|
|
617
|
+
* <p>
|
|
618
|
+
* Most of our crypto primitives operate on arrays of 4-byte words internally,
|
|
619
|
+
* but many of them can take arguments that are not a multiple of 4 bytes.
|
|
620
|
+
* This library encodes arrays of bits (whose size need not be a multiple of 8
|
|
621
|
+
* bits) as arrays of 32-bit words. The bits are packed, big-endian, into an
|
|
622
|
+
* array of words, 32 bits at a time. Since the words are double-precision
|
|
623
|
+
* floating point numbers, they fit some extra data. We use this (in a private,
|
|
624
|
+
* possibly-changing manner) to encode the number of bits actually present
|
|
625
|
+
* in the last word of the array.
|
|
626
|
+
* </p>
|
|
627
|
+
*
|
|
628
|
+
* <p>
|
|
629
|
+
* Because bitwise ops clear this out-of-band data, these arrays can be passed
|
|
630
|
+
* to ciphers like AES which want arrays of words.
|
|
631
|
+
* </p>
|
|
632
|
+
*/
|
|
633
|
+
sjcl.bitArray = {
|
|
634
|
+
/**
|
|
635
|
+
* Array slices in units of bits.
|
|
636
|
+
* @param {bitArray} a The array to slice.
|
|
637
|
+
* @param {Number} bstart The offset to the start of the slice, in bits.
|
|
638
|
+
* @param {Number} bend The offset to the end of the slice, in bits. If this is undefined,
|
|
639
|
+
* slice until the end of the array.
|
|
640
|
+
* @return {bitArray} The requested slice.
|
|
641
|
+
*/
|
|
642
|
+
bitSlice: function (a, bstart, bend) {
|
|
643
|
+
a = sjcl.bitArray
|
|
644
|
+
._shiftRight(a.slice(bstart / 32), 32 - (bstart & 31))
|
|
645
|
+
.slice(1);
|
|
646
|
+
return bend === undefined ? a : sjcl.bitArray.clamp(a, bend - bstart);
|
|
647
|
+
},
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* Extract a number packed into a bit array.
|
|
651
|
+
* @param {bitArray} a The array to slice.
|
|
652
|
+
* @param {Number} bstart The offset to the start of the slice, in bits.
|
|
653
|
+
* @param {Number} blength The length of the number to extract.
|
|
654
|
+
* @return {Number} The requested slice.
|
|
655
|
+
*/
|
|
656
|
+
extract: function (a, bstart, blength) {
|
|
657
|
+
// FIXME: this Math.floor is not necessary at all, but for some reason
|
|
658
|
+
// seems to suppress a bug in the Chromium JIT.
|
|
659
|
+
var x,
|
|
660
|
+
sh = Math.floor((-bstart - blength) & 31);
|
|
661
|
+
if (((bstart + blength - 1) ^ bstart) & -32) {
|
|
662
|
+
// it crosses a boundary
|
|
663
|
+
x =
|
|
664
|
+
(a[(bstart / 32) | 0] << (32 - sh)) ^ (a[(bstart / 32 + 1) | 0] >>> sh);
|
|
665
|
+
} else {
|
|
666
|
+
// within a single word
|
|
667
|
+
x = a[(bstart / 32) | 0] >>> sh;
|
|
668
|
+
}
|
|
669
|
+
return x & ((1 << blength) - 1);
|
|
670
|
+
},
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* Concatenate two bit arrays.
|
|
674
|
+
* @param {bitArray} a1 The first array.
|
|
675
|
+
* @param {bitArray} a2 The second array.
|
|
676
|
+
* @return {bitArray} The concatenation of a1 and a2.
|
|
677
|
+
*/
|
|
678
|
+
concat: function (a1, a2) {
|
|
679
|
+
if (a1.length === 0 || a2.length === 0) {
|
|
680
|
+
return a1.concat(a2);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
var last = a1[a1.length - 1],
|
|
684
|
+
shift = sjcl.bitArray.getPartial(last);
|
|
685
|
+
if (shift === 32) {
|
|
686
|
+
return a1.concat(a2);
|
|
687
|
+
} else {
|
|
688
|
+
return sjcl.bitArray._shiftRight(
|
|
689
|
+
a2,
|
|
690
|
+
shift,
|
|
691
|
+
last | 0,
|
|
692
|
+
a1.slice(0, a1.length - 1)
|
|
693
|
+
);
|
|
694
|
+
}
|
|
695
|
+
},
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Find the length of an array of bits.
|
|
699
|
+
* @param {bitArray} a The array.
|
|
700
|
+
* @return {Number} The length of a, in bits.
|
|
701
|
+
*/
|
|
702
|
+
bitLength: function (a) {
|
|
703
|
+
var l = a.length,
|
|
704
|
+
x;
|
|
705
|
+
if (l === 0) {
|
|
706
|
+
return 0;
|
|
707
|
+
}
|
|
708
|
+
x = a[l - 1];
|
|
709
|
+
return (l - 1) * 32 + sjcl.bitArray.getPartial(x);
|
|
710
|
+
},
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* Truncate an array.
|
|
714
|
+
* @param {bitArray} a The array.
|
|
715
|
+
* @param {Number} len The length to truncate to, in bits.
|
|
716
|
+
* @return {bitArray} A new array, truncated to len bits.
|
|
717
|
+
*/
|
|
718
|
+
clamp: function (a, len) {
|
|
719
|
+
if (a.length * 32 < len) {
|
|
720
|
+
return a;
|
|
721
|
+
}
|
|
722
|
+
a = a.slice(0, Math.ceil(len / 32));
|
|
723
|
+
var l = a.length;
|
|
724
|
+
len = len & 31;
|
|
725
|
+
if (l > 0 && len) {
|
|
726
|
+
a[l - 1] = sjcl.bitArray.partial(
|
|
727
|
+
len,
|
|
728
|
+
a[l - 1] & (0x80000000 >> (len - 1)),
|
|
729
|
+
1
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
return a;
|
|
733
|
+
},
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* Make a partial word for a bit array.
|
|
737
|
+
* @param {Number} len The number of bits in the word.
|
|
738
|
+
* @param {Number} x The bits.
|
|
739
|
+
* @param {Number} [_end=0] Pass 1 if x has already been shifted to the high side.
|
|
740
|
+
* @return {Number} The partial word.
|
|
741
|
+
*/
|
|
742
|
+
partial: function (len, x, _end) {
|
|
743
|
+
if (len === 32) {
|
|
744
|
+
return x;
|
|
745
|
+
}
|
|
746
|
+
return (_end ? x | 0 : x << (32 - len)) + len * 0x10000000000;
|
|
747
|
+
},
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Get the number of bits used by a partial word.
|
|
751
|
+
* @param {Number} x The partial word.
|
|
752
|
+
* @return {Number} The number of bits used by the partial word.
|
|
753
|
+
*/
|
|
754
|
+
getPartial: function (x) {
|
|
755
|
+
return Math.round(x / 0x10000000000) || 32;
|
|
756
|
+
},
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* Compare two arrays for equality in a predictable amount of time.
|
|
760
|
+
* @param {bitArray} a The first array.
|
|
761
|
+
* @param {bitArray} b The second array.
|
|
762
|
+
* @return {boolean} true if a == b; false otherwise.
|
|
763
|
+
*/
|
|
764
|
+
equal: function (a, b) {
|
|
765
|
+
if (sjcl.bitArray.bitLength(a) !== sjcl.bitArray.bitLength(b)) {
|
|
766
|
+
return false;
|
|
767
|
+
}
|
|
768
|
+
var x = 0,
|
|
769
|
+
i;
|
|
770
|
+
for (i = 0; i < a.length; i++) {
|
|
771
|
+
x |= a[i] ^ b[i];
|
|
772
|
+
}
|
|
773
|
+
return x === 0;
|
|
774
|
+
},
|
|
775
|
+
|
|
776
|
+
/** Shift an array right.
|
|
777
|
+
* @param {bitArray} a The array to shift.
|
|
778
|
+
* @param {Number} shift The number of bits to shift.
|
|
779
|
+
* @param {Number} [carry=0] A byte to carry in
|
|
780
|
+
* @param {bitArray} [out=[]] An array to prepend to the output.
|
|
781
|
+
* @private
|
|
782
|
+
*/
|
|
783
|
+
_shiftRight: function (a, shift, carry, out) {
|
|
784
|
+
var i,
|
|
785
|
+
last2 = 0,
|
|
786
|
+
shift2;
|
|
787
|
+
if (out === undefined) {
|
|
788
|
+
out = [];
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
for (; shift >= 32; shift -= 32) {
|
|
792
|
+
out.push(carry);
|
|
793
|
+
carry = 0;
|
|
794
|
+
}
|
|
795
|
+
if (shift === 0) {
|
|
796
|
+
return out.concat(a);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
for (i = 0; i < a.length; i++) {
|
|
800
|
+
out.push(carry | (a[i] >>> shift));
|
|
801
|
+
carry = a[i] << (32 - shift);
|
|
802
|
+
}
|
|
803
|
+
last2 = a.length ? a[a.length - 1] : 0;
|
|
804
|
+
shift2 = sjcl.bitArray.getPartial(last2);
|
|
805
|
+
out.push(
|
|
806
|
+
sjcl.bitArray.partial(
|
|
807
|
+
(shift + shift2) & 31,
|
|
808
|
+
shift + shift2 > 32 ? carry : out.pop(),
|
|
809
|
+
1
|
|
810
|
+
)
|
|
811
|
+
);
|
|
812
|
+
return out;
|
|
813
|
+
},
|
|
814
|
+
|
|
815
|
+
/** xor a block of 4 words together.
|
|
816
|
+
* @private
|
|
817
|
+
*/
|
|
818
|
+
_xor4: function (x, y) {
|
|
819
|
+
return [x[0] ^ y[0], x[1] ^ y[1], x[2] ^ y[2], x[3] ^ y[3]];
|
|
820
|
+
},
|
|
821
|
+
|
|
822
|
+
/** byteswap a word array inplace.
|
|
823
|
+
* (does not handle partial words)
|
|
824
|
+
* @param {sjcl.bitArray} a word array
|
|
825
|
+
* @return {sjcl.bitArray} byteswapped array
|
|
826
|
+
*/
|
|
827
|
+
byteswapM: function (a) {
|
|
828
|
+
var i,
|
|
829
|
+
v,
|
|
830
|
+
m = 0xff00;
|
|
831
|
+
for (i = 0; i < a.length; ++i) {
|
|
832
|
+
v = a[i];
|
|
833
|
+
a[i] = (v >>> 24) | ((v >>> 8) & m) | ((v & m) << 8) | (v << 24);
|
|
834
|
+
}
|
|
835
|
+
return a;
|
|
836
|
+
},
|
|
837
|
+
};
|
|
838
|
+
/** @fileOverview Bit array codec implementations.
|
|
839
|
+
*
|
|
840
|
+
* @author Emily Stark
|
|
841
|
+
* @author Mike Hamburg
|
|
842
|
+
* @author Dan Boneh
|
|
843
|
+
*/
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* UTF-8 strings
|
|
847
|
+
* @namespace
|
|
848
|
+
*/
|
|
849
|
+
sjcl.codec.utf8String = {
|
|
850
|
+
/** Convert from a bitArray to a UTF-8 string. */
|
|
851
|
+
fromBits: function (arr) {
|
|
852
|
+
var out = '',
|
|
853
|
+
bl = sjcl.bitArray.bitLength(arr),
|
|
854
|
+
i,
|
|
855
|
+
tmp;
|
|
856
|
+
for (i = 0; i < bl / 8; i++) {
|
|
857
|
+
if ((i & 3) === 0) {
|
|
858
|
+
tmp = arr[i / 4];
|
|
859
|
+
}
|
|
860
|
+
out += String.fromCharCode(((tmp >>> 8) >>> 8) >>> 8);
|
|
861
|
+
tmp <<= 8;
|
|
862
|
+
}
|
|
863
|
+
return decodeURIComponent(escape(out));
|
|
864
|
+
},
|
|
865
|
+
|
|
866
|
+
/** Convert from a UTF-8 string to a bitArray. */
|
|
867
|
+
toBits: function (str) {
|
|
868
|
+
str = unescape(encodeURIComponent(str));
|
|
869
|
+
var out = [],
|
|
870
|
+
i,
|
|
871
|
+
tmp = 0;
|
|
872
|
+
for (i = 0; i < str.length; i++) {
|
|
873
|
+
tmp = (tmp << 8) | str.charCodeAt(i);
|
|
874
|
+
if ((i & 3) === 3) {
|
|
875
|
+
out.push(tmp);
|
|
876
|
+
tmp = 0;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
if (i & 3) {
|
|
880
|
+
out.push(sjcl.bitArray.partial(8 * (i & 3), tmp));
|
|
881
|
+
}
|
|
882
|
+
return out;
|
|
883
|
+
},
|
|
884
|
+
};
|
|
885
|
+
/** @fileOverview Javascript SHA-256 implementation.
|
|
886
|
+
*
|
|
887
|
+
* An older version of this implementation is available in the public
|
|
888
|
+
* domain, but this one is (c) Emily Stark, Mike Hamburg, Dan Boneh,
|
|
889
|
+
* Stanford University 2008-2010 and BSD-licensed for liability
|
|
890
|
+
* reasons.
|
|
891
|
+
*
|
|
892
|
+
* Special thanks to Aldo Cortesi for pointing out several bugs in
|
|
893
|
+
* this code.
|
|
894
|
+
*
|
|
895
|
+
* @author Emily Stark
|
|
896
|
+
* @author Mike Hamburg
|
|
897
|
+
* @author Dan Boneh
|
|
898
|
+
*/
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Context for a SHA-256 operation in progress.
|
|
902
|
+
* @constructor
|
|
903
|
+
*/
|
|
904
|
+
sjcl.hash.sha256 = function (hash) {
|
|
905
|
+
if (!this._key[0]) {
|
|
906
|
+
this._precompute();
|
|
907
|
+
}
|
|
908
|
+
if (hash) {
|
|
909
|
+
this._h = hash._h.slice(0);
|
|
910
|
+
this._buffer = hash._buffer.slice(0);
|
|
911
|
+
this._length = hash._length;
|
|
912
|
+
} else {
|
|
913
|
+
this.reset();
|
|
914
|
+
}
|
|
915
|
+
};
|
|
916
|
+
|
|
917
|
+
/**
|
|
918
|
+
* Hash a string or an array of words.
|
|
919
|
+
* @static
|
|
920
|
+
* @param {bitArray|String} data the data to hash.
|
|
921
|
+
* @return {bitArray} The hash value, an array of 16 big-endian words.
|
|
922
|
+
*/
|
|
923
|
+
sjcl.hash.sha256.hash = function (data) {
|
|
924
|
+
return new sjcl.hash.sha256().update(data).finalize();
|
|
925
|
+
};
|
|
926
|
+
|
|
927
|
+
sjcl.hash.sha256.prototype = {
|
|
928
|
+
/**
|
|
929
|
+
* The hash's block size, in bits.
|
|
930
|
+
* @constant
|
|
931
|
+
*/
|
|
932
|
+
blockSize: 512,
|
|
933
|
+
|
|
934
|
+
/**
|
|
935
|
+
* Reset the hash state.
|
|
936
|
+
* @return this
|
|
937
|
+
*/
|
|
938
|
+
reset: function () {
|
|
939
|
+
this._h = this._init.slice(0);
|
|
940
|
+
this._buffer = [];
|
|
941
|
+
this._length = 0;
|
|
942
|
+
return this;
|
|
943
|
+
},
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* Input several words to the hash.
|
|
947
|
+
* @param {bitArray|String} data the data to hash.
|
|
948
|
+
* @return this
|
|
949
|
+
*/
|
|
950
|
+
update: function (data) {
|
|
951
|
+
if (typeof data === 'string') {
|
|
952
|
+
data = sjcl.codec.utf8String.toBits(data);
|
|
953
|
+
}
|
|
954
|
+
var i,
|
|
955
|
+
b = (this._buffer = sjcl.bitArray.concat(this._buffer, data)),
|
|
956
|
+
ol = this._length,
|
|
957
|
+
nl = (this._length = ol + sjcl.bitArray.bitLength(data));
|
|
958
|
+
if (nl > 9007199254740991) {
|
|
959
|
+
throw new sjcl.exception.invalid('Cannot hash more than 2^53 - 1 bits');
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
if (typeof Uint32Array !== 'undefined') {
|
|
963
|
+
var c = new Uint32Array(b);
|
|
964
|
+
var j = 0;
|
|
965
|
+
for (i = 512 + ol - ((512 + ol) & 511); i <= nl; i += 512) {
|
|
966
|
+
this._block(c.subarray(16 * j, 16 * (j + 1)));
|
|
967
|
+
j += 1;
|
|
968
|
+
}
|
|
969
|
+
b.splice(0, 16 * j);
|
|
970
|
+
} else {
|
|
971
|
+
for (i = 512 + ol - ((512 + ol) & 511); i <= nl; i += 512) {
|
|
972
|
+
this._block(b.splice(0, 16));
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
return this;
|
|
976
|
+
},
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* Complete hashing and output the hash value.
|
|
980
|
+
* @return {bitArray} The hash value, an array of 8 big-endian words.
|
|
981
|
+
*/
|
|
982
|
+
finalize: function () {
|
|
983
|
+
var i,
|
|
984
|
+
b = this._buffer,
|
|
985
|
+
h = this._h;
|
|
986
|
+
|
|
987
|
+
// Round out and push the buffer
|
|
988
|
+
b = sjcl.bitArray.concat(b, [sjcl.bitArray.partial(1, 1)]);
|
|
989
|
+
|
|
990
|
+
// Round out the buffer to a multiple of 16 words, less the 2 length words.
|
|
991
|
+
for (i = b.length + 2; i & 15; i++) {
|
|
992
|
+
b.push(0);
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
// append the length
|
|
996
|
+
b.push(Math.floor(this._length / 0x100000000));
|
|
997
|
+
b.push(this._length | 0);
|
|
998
|
+
|
|
999
|
+
while (b.length) {
|
|
1000
|
+
this._block(b.splice(0, 16));
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
this.reset();
|
|
1004
|
+
return h;
|
|
1005
|
+
},
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* The SHA-256 initialization vector, to be precomputed.
|
|
1009
|
+
* @private
|
|
1010
|
+
*/
|
|
1011
|
+
_init: [],
|
|
1012
|
+
/*
|
|
1013
|
+
_init:[0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19],
|
|
1014
|
+
*/
|
|
1015
|
+
|
|
1016
|
+
/**
|
|
1017
|
+
* The SHA-256 hash key, to be precomputed.
|
|
1018
|
+
* @private
|
|
1019
|
+
*/
|
|
1020
|
+
_key: [],
|
|
1021
|
+
/*
|
|
1022
|
+
_key:
|
|
1023
|
+
[0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
1024
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
1025
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
1026
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
1027
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
1028
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
1029
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
1030
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2],
|
|
1031
|
+
*/
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* Function to precompute _init and _key.
|
|
1035
|
+
* @private
|
|
1036
|
+
*/
|
|
1037
|
+
_precompute: function () {
|
|
1038
|
+
var i = 0,
|
|
1039
|
+
prime = 2,
|
|
1040
|
+
factor,
|
|
1041
|
+
isPrime;
|
|
1042
|
+
|
|
1043
|
+
function frac(x) {
|
|
1044
|
+
return ((x - Math.floor(x)) * 0x100000000) | 0;
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
for (; i < 64; prime++) {
|
|
1048
|
+
isPrime = true;
|
|
1049
|
+
for (factor = 2; factor * factor <= prime; factor++) {
|
|
1050
|
+
if (prime % factor === 0) {
|
|
1051
|
+
isPrime = false;
|
|
1052
|
+
break;
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
if (isPrime) {
|
|
1056
|
+
if (i < 8) {
|
|
1057
|
+
this._init[i] = frac(Math.pow(prime, 1 / 2));
|
|
1058
|
+
}
|
|
1059
|
+
this._key[i] = frac(Math.pow(prime, 1 / 3));
|
|
1060
|
+
i++;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
},
|
|
1064
|
+
|
|
1065
|
+
/**
|
|
1066
|
+
* Perform one cycle of SHA-256.
|
|
1067
|
+
* @param {Uint32Array|bitArray} w one block of words.
|
|
1068
|
+
* @private
|
|
1069
|
+
*/
|
|
1070
|
+
_block: function (w) {
|
|
1071
|
+
var i,
|
|
1072
|
+
tmp,
|
|
1073
|
+
a,
|
|
1074
|
+
b,
|
|
1075
|
+
h = this._h,
|
|
1076
|
+
k = this._key,
|
|
1077
|
+
h0 = h[0],
|
|
1078
|
+
h1 = h[1],
|
|
1079
|
+
h2 = h[2],
|
|
1080
|
+
h3 = h[3],
|
|
1081
|
+
h4 = h[4],
|
|
1082
|
+
h5 = h[5],
|
|
1083
|
+
h6 = h[6],
|
|
1084
|
+
h7 = h[7];
|
|
1085
|
+
|
|
1086
|
+
/* Rationale for placement of |0 :
|
|
1087
|
+
* If a value can overflow is original 32 bits by a factor of more than a few
|
|
1088
|
+
* million (2^23 ish), there is a possibility that it might overflow the
|
|
1089
|
+
* 53-bit mantissa and lose precision.
|
|
1090
|
+
*
|
|
1091
|
+
* To avoid this, we clamp back to 32 bits by |'ing with 0 on any value that
|
|
1092
|
+
* propagates around the loop, and on the hash state h[]. I don't believe
|
|
1093
|
+
* that the clamps on h4 and on h0 are strictly necessary, but it's close
|
|
1094
|
+
* (for h4 anyway), and better safe than sorry.
|
|
1095
|
+
*
|
|
1096
|
+
* The clamps on h[] are necessary for the output to be correct even in the
|
|
1097
|
+
* common case and for short inputs.
|
|
1098
|
+
*/
|
|
1099
|
+
for (i = 0; i < 64; i++) {
|
|
1100
|
+
// load up the input word for this round
|
|
1101
|
+
if (i < 16) {
|
|
1102
|
+
tmp = w[i];
|
|
1103
|
+
} else {
|
|
1104
|
+
a = w[(i + 1) & 15];
|
|
1105
|
+
b = w[(i + 14) & 15];
|
|
1106
|
+
tmp = w[i & 15] =
|
|
1107
|
+
(((a >>> 7) ^ (a >>> 18) ^ (a >>> 3) ^ (a << 25) ^ (a << 14)) +
|
|
1108
|
+
((b >>> 17) ^ (b >>> 19) ^ (b >>> 10) ^ (b << 15) ^ (b << 13)) +
|
|
1109
|
+
w[i & 15] +
|
|
1110
|
+
w[(i + 9) & 15]) |
|
|
1111
|
+
0;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
tmp =
|
|
1115
|
+
tmp +
|
|
1116
|
+
h7 +
|
|
1117
|
+
((h4 >>> 6) ^
|
|
1118
|
+
(h4 >>> 11) ^
|
|
1119
|
+
(h4 >>> 25) ^
|
|
1120
|
+
(h4 << 26) ^
|
|
1121
|
+
(h4 << 21) ^
|
|
1122
|
+
(h4 << 7)) +
|
|
1123
|
+
(h6 ^ (h4 & (h5 ^ h6))) +
|
|
1124
|
+
k[i]; // | 0;
|
|
1125
|
+
|
|
1126
|
+
// shift register
|
|
1127
|
+
h7 = h6;
|
|
1128
|
+
h6 = h5;
|
|
1129
|
+
h5 = h4;
|
|
1130
|
+
h4 = (h3 + tmp) | 0;
|
|
1131
|
+
h3 = h2;
|
|
1132
|
+
h2 = h1;
|
|
1133
|
+
h1 = h0;
|
|
1134
|
+
|
|
1135
|
+
h0 =
|
|
1136
|
+
(tmp +
|
|
1137
|
+
((h1 & h2) ^ (h3 & (h1 ^ h2))) +
|
|
1138
|
+
((h1 >>> 2) ^
|
|
1139
|
+
(h1 >>> 13) ^
|
|
1140
|
+
(h1 >>> 22) ^
|
|
1141
|
+
(h1 << 30) ^
|
|
1142
|
+
(h1 << 19) ^
|
|
1143
|
+
(h1 << 10))) |
|
|
1144
|
+
0;
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
h[0] = (h[0] + h0) | 0;
|
|
1148
|
+
h[1] = (h[1] + h1) | 0;
|
|
1149
|
+
h[2] = (h[2] + h2) | 0;
|
|
1150
|
+
h[3] = (h[3] + h3) | 0;
|
|
1151
|
+
h[4] = (h[4] + h4) | 0;
|
|
1152
|
+
h[5] = (h[5] + h5) | 0;
|
|
1153
|
+
h[6] = (h[6] + h6) | 0;
|
|
1154
|
+
h[7] = (h[7] + h7) | 0;
|
|
1155
|
+
},
|
|
1156
|
+
};
|
|
1157
|
+
|
|
494
1158
|
// eslint-disable-next-line require-jsdoc
|
|
495
1159
|
function base64URLEncode(str) {
|
|
496
1160
|
return btoa(str).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|