forked from ms2008/Xshell-OTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jumper.js
2344 lines (2131 loc) · 84.3 KB
/
jumper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Article about TOTP on my blog https://stapp.space/generate-totp-in-postman/
/**
* @preserve A JavaScript implementation of the SHA family of hashes, as
* defined in FIPS PUB 180-4 and FIPS PUB 202, as well as the corresponding
* HMAC implementation as defined in FIPS PUB 198a
*
* Copyright Brian Turek 2008-2017
* Distributed under the BSD License
* See http://caligatio.github.com/jsSHA/ for more information
*
* Several functions taken from Paul Johnston
*/
/*jslint
bitwise: true, multivar: true, for: true, this: true, sub: true, esversion: 3
*/
/**
* SUPPORTED_ALGS is the stub for a compile flag that will cause pruning of
* functions that are not needed when a limited number of SHA families are
* selected
*
* @define {number} ORed value of SHA variants to be supported
* 1 = SHA-1, 2 = SHA-224/SHA-256, 4 = SHA-384/SHA-512, 8 = SHA3
*/
var SUPPORTED_ALGS = 8 | 4 | 2 | 1;
var X={};
(function (global)
{
"use strict";
/* Globals */
var TWO_PWR_32 = 4294967296;
/**
* Int_64 is a object for 2 32-bit numbers emulating a 64-bit number
*
* @private
* @constructor
* @this {Int_64}
* @param {number} msint_32 The most significant 32-bits of a 64-bit number
* @param {number} lsint_32 The least significant 32-bits of a 64-bit number
*/
function Int_64(msint_32, lsint_32)
{
this.highOrder = msint_32;
this.lowOrder = lsint_32;
}
/**
* Convert a string to an array of big-endian words
*
* There is a known bug with an odd number of existing bytes and using a
* UTF-16 encoding. However, this function is used such that the existing
* bytes are always a result of a previous UTF-16 str2packed call and
* therefore there should never be an odd number of existing bytes
*
* @private
* @param {string} str String to be converted to binary representation
* @param {string} utfType The Unicode type, UTF8 or UTF16BE, UTF16LE, to
* use to encode the source string
* @param {Array<number>} existingPacked A packed int array of bytes to
* append the results to
* @param {number} existingPackedLen The number of bits in the existingPacked
* array
* @param {number} bigEndianMod Modifier for whether hash function is
* big or small endian
* @return {{value : Array<number>, binLen : number}} Hash list where
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
function str2packed(str, utfType, existingPacked, existingPackedLen, bigEndianMod)
{
var packed, codePnt, codePntArr, byteCnt = 0, i, j, existingByteLen,
intOffset, byteOffset, shiftModifier, transposeBytes;
packed = existingPacked || [0];
existingPackedLen = existingPackedLen || 0;
existingByteLen = existingPackedLen >>> 3;
if ("UTF8" === utfType)
{
shiftModifier = (bigEndianMod === -1) ? 3 : 0;
for (i = 0; i < str.length; i += 1)
{
codePnt = str.charCodeAt(i);
codePntArr = [];
if (0x80 > codePnt)
{
codePntArr.push(codePnt);
}
else if (0x800 > codePnt)
{
codePntArr.push(0xC0 | (codePnt >>> 6));
codePntArr.push(0x80 | (codePnt & 0x3F));
}
else if ((0xd800 > codePnt) || (0xe000 <= codePnt)) {
codePntArr.push(
0xe0 | (codePnt >>> 12),
0x80 | ((codePnt >>> 6) & 0x3f),
0x80 | (codePnt & 0x3f)
);
}
else
{
i += 1;
codePnt = 0x10000 + (((codePnt & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
codePntArr.push(
0xf0 | (codePnt >>> 18),
0x80 | ((codePnt >>> 12) & 0x3f),
0x80 | ((codePnt >>> 6) & 0x3f),
0x80 | (codePnt & 0x3f)
);
}
for (j = 0; j < codePntArr.length; j += 1)
{
byteOffset = byteCnt + existingByteLen;
intOffset = byteOffset >>> 2;
while (packed.length <= intOffset)
{
packed.push(0);
}
/* Known bug kicks in here */
packed[intOffset] |= codePntArr[j] << (8 * (shiftModifier + bigEndianMod * (byteOffset % 4)));
byteCnt += 1;
}
}
}
else if (("UTF16BE" === utfType) || "UTF16LE" === utfType)
{
shiftModifier = (bigEndianMod === -1) ? 2 : 0;
/* Internally strings are UTF-16BE so transpose bytes under two conditions:
* need LE and not switching endianness due to SHA-3
* need BE and switching endianness due to SHA-3 */
transposeBytes = (("UTF16LE" === utfType) && (bigEndianMod !== 1)) || (("UTF16LE" !== utfType) && (bigEndianMod === 1));
for (i = 0; i < str.length; i += 1)
{
codePnt = str.charCodeAt(i);
if (transposeBytes === true)
{
j = codePnt & 0xFF;
codePnt = (j << 8) | (codePnt >>> 8);
}
byteOffset = byteCnt + existingByteLen;
intOffset = byteOffset >>> 2;
while (packed.length <= intOffset)
{
packed.push(0);
}
packed[intOffset] |= codePnt << (8 * (shiftModifier + bigEndianMod * (byteOffset % 4)));
byteCnt += 2;
}
}
return {"value" : packed, "binLen" : byteCnt * 8 + existingPackedLen};
}
/**
* Convert a hex string to an array of big-endian words
*
* @private
* @param {string} str String to be converted to binary representation
* @param {Array<number>} existingPacked A packed int array of bytes to
* append the results to
* @param {number} existingPackedLen The number of bits in the existingPacked
* array
* @param {number} bigEndianMod Modifier for whether hash function is
* big or small endian
* @return {{value : Array<number>, binLen : number}} Hash list where
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
function hex2packed(str, existingPacked, existingPackedLen, bigEndianMod)
{
var packed, length = str.length, i, num, intOffset, byteOffset,
existingByteLen, shiftModifier;
if (0 !== (length % 2))
{
throw new Error("String of HEX type must be in byte increments");
}
packed = existingPacked || [0];
existingPackedLen = existingPackedLen || 0;
existingByteLen = existingPackedLen >>> 3;
shiftModifier = (bigEndianMod === -1) ? 3 : 0;
for (i = 0; i < length; i += 2)
{
num = parseInt(str.substr(i, 2), 16);
if (!isNaN(num))
{
byteOffset = (i >>> 1) + existingByteLen;
intOffset = byteOffset >>> 2;
while (packed.length <= intOffset)
{
packed.push(0);
}
packed[intOffset] |= num << (8 * (shiftModifier + bigEndianMod * (byteOffset % 4)));
}
else
{
throw new Error("String of HEX type contains invalid characters");
}
}
return {"value" : packed, "binLen" : length * 4 + existingPackedLen};
}
/**
* Convert a string of raw bytes to an array of big-endian words
*
* @private
* @param {string} str String of raw bytes to be converted to binary representation
* @param {Array<number>} existingPacked A packed int array of bytes to
* append the results to
* @param {number} existingPackedLen The number of bits in the existingPacked
* array
* @param {number} bigEndianMod Modifier for whether hash function is
* big or small endian
* @return {{value : Array<number>, binLen : number}} Hash list where
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
function bytes2packed(str, existingPacked, existingPackedLen, bigEndianMod)
{
var packed, codePnt, i, existingByteLen, intOffset,
byteOffset, shiftModifier;
packed = existingPacked || [0];
existingPackedLen = existingPackedLen || 0;
existingByteLen = existingPackedLen >>> 3;
shiftModifier = (bigEndianMod === -1) ? 3 : 0;
for (i = 0; i < str.length; i += 1)
{
codePnt = str.charCodeAt(i);
byteOffset = i + existingByteLen;
intOffset = byteOffset >>> 2;
if (packed.length <= intOffset)
{
packed.push(0);
}
packed[intOffset] |= codePnt << (8 * (shiftModifier + bigEndianMod * (byteOffset % 4)));
}
return {"value" : packed, "binLen" : str.length * 8 + existingPackedLen};
}
/**
* Convert a base-64 string to an array of big-endian words
*
* @private
* @param {string} str String to be converted to binary representation
* @param {Array<number>} existingPacked A packed int array of bytes to
* append the results to
* @param {number} existingPackedLen The number of bits in the existingPacked
* array
* @param {number} bigEndianMod Modifier for whether hash function is
* big or small endian
* @return {{value : Array<number>, binLen : number}} Hash list where
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
function b642packed(str, existingPacked, existingPackedLen, bigEndianMod)
{
var packed, byteCnt = 0, index, i, j, tmpInt, strPart, firstEqual,
b64Tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
existingByteLen, intOffset, byteOffset, shiftModifier;
if (-1 === str.search(/^[a-zA-Z0-9=+\/]+$/))
{
throw new Error("Invalid character in base-64 string");
}
firstEqual = str.indexOf("=");
str = str.replace(/\=/g, "");
if ((-1 !== firstEqual) && (firstEqual < str.length))
{
throw new Error("Invalid '=' found in base-64 string");
}
packed = existingPacked || [0];
existingPackedLen = existingPackedLen || 0;
existingByteLen = existingPackedLen >>> 3;
shiftModifier = (bigEndianMod === -1) ? 3 : 0;
for (i = 0; i < str.length; i += 4)
{
strPart = str.substr(i, 4);
tmpInt = 0;
for (j = 0; j < strPart.length; j += 1)
{
index = b64Tab.indexOf(strPart[j]);
tmpInt |= index << (18 - (6 * j));
}
for (j = 0; j < strPart.length - 1; j += 1)
{
byteOffset = byteCnt + existingByteLen;
intOffset = byteOffset >>> 2;
while (packed.length <= intOffset)
{
packed.push(0);
}
packed[intOffset] |= ((tmpInt >>> (16 - (j * 8))) & 0xFF) <<
(8 * (shiftModifier + bigEndianMod * (byteOffset % 4)));
byteCnt += 1;
}
}
return {"value" : packed, "binLen" : byteCnt * 8 + existingPackedLen};
}
/**
* Convert an ArrayBuffer to an array of big-endian words
*
* @private
* @param {ArrayBuffer} arr ArrayBuffer to be converted to binary
* representation
* @param {Array<number>} existingPacked A packed int array of bytes to
* append the results to
* @param {number} existingPackedLen The number of bits in the existingPacked
* array
* @param {number} bigEndianMod Modifier for whether hash function is
* big or small endian
* @return {{value : Array<number>, binLen : number}} Hash list where
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
function arraybuffer2packed(arr, existingPacked, existingPackedLen, bigEndianMod)
{
var packed, i, existingByteLen, intOffset, byteOffset, shiftModifier, arrView;
packed = existingPacked || [0];
existingPackedLen = existingPackedLen || 0;
existingByteLen = existingPackedLen >>> 3;
shiftModifier = (bigEndianMod === -1) ? 3 : 0;
arrView = new Uint8Array(arr);
for (i = 0; i < arr.byteLength; i += 1)
{
byteOffset = i + existingByteLen;
intOffset = byteOffset >>> 2;
if (packed.length <= intOffset)
{
packed.push(0);
}
packed[intOffset] |= arrView[i] << (8 * (shiftModifier + bigEndianMod * (byteOffset % 4)));
}
return {"value" : packed, "binLen" : arr.byteLength * 8 + existingPackedLen};
}
/**
* Convert an array of big-endian words to a hex string.
*
* @private
* @param {Array<number>} packed Array of integers to be converted to
* hexidecimal representation
* @param {number} outputLength Length of output in bits
* @param {number} bigEndianMod Modifier for whether hash function is
* big or small endian
* @param {{outputUpper : boolean, b64Pad : string}} formatOpts Hash list
* containing validated output formatting options
* @return {string} Hexidecimal representation of the parameter in string
* form
*/
function packed2hex(packed, outputLength, bigEndianMod, formatOpts)
{
var hex_tab = "0123456789abcdef", str = "",
length = outputLength / 8, i, srcByte, shiftModifier;
shiftModifier = (bigEndianMod === -1) ? 3 : 0;
for (i = 0; i < length; i += 1)
{
/* The below is more than a byte but it gets taken care of later */
srcByte = packed[i >>> 2] >>> (8 * (shiftModifier + bigEndianMod * (i % 4)));
str += hex_tab.charAt((srcByte >>> 4) & 0xF) +
hex_tab.charAt(srcByte & 0xF);
}
return (formatOpts["outputUpper"]) ? str.toUpperCase() : str;
}
/**
* Convert an array of big-endian words to a base-64 string
*
* @private
* @param {Array<number>} packed Array of integers to be converted to
* base-64 representation
* @param {number} outputLength Length of output in bits
* @param {number} bigEndianMod Modifier for whether hash function is
* big or small endian
* @param {{outputUpper : boolean, b64Pad : string}} formatOpts Hash list
* containing validated output formatting options
* @return {string} Base-64 encoded representation of the parameter in
* string form
*/
function packed2b64(packed, outputLength, bigEndianMod, formatOpts)
{
var str = "", length = outputLength / 8, i, j, triplet, int1, int2, shiftModifier,
b64Tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
shiftModifier = (bigEndianMod === -1) ? 3 : 0;
for (i = 0; i < length; i += 3)
{
int1 = ((i + 1) < length) ? packed[(i + 1) >>> 2] : 0;
int2 = ((i + 2) < length) ? packed[(i + 2) >>> 2] : 0;
triplet = (((packed[i >>> 2] >>> (8 * (shiftModifier + bigEndianMod * (i % 4)))) & 0xFF) << 16) |
(((int1 >>> (8 * (shiftModifier + bigEndianMod * ((i + 1) % 4)))) & 0xFF) << 8) |
((int2 >>> (8 * (shiftModifier + bigEndianMod * ((i + 2) % 4)))) & 0xFF);
for (j = 0; j < 4; j += 1)
{
if (i * 8 + j * 6 <= outputLength)
{
str += b64Tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F);
}
else
{
str += formatOpts["b64Pad"];
}
}
}
return str;
}
/**
* Convert an array of big-endian words to raw bytes string
*
* @private
* @param {Array<number>} packed Array of integers to be converted to
* a raw bytes string representation
* @param {number} outputLength Length of output in bits
* @param {number} bigEndianMod Modifier for whether hash function is
* big or small endian
* @return {string} Raw bytes representation of the parameter in string
* form
*/
function packed2bytes(packed, outputLength, bigEndianMod)
{
var str = "", length = outputLength / 8, i, srcByte, shiftModifier;
shiftModifier = (bigEndianMod === -1) ? 3 : 0;
for (i = 0; i < length; i += 1)
{
srcByte = (packed[i >>> 2] >>> (8 * (shiftModifier + bigEndianMod * (i % 4)))) & 0xFF;
str += String.fromCharCode(srcByte);
}
return str;
}
/**
* Convert an array of big-endian words to an ArrayBuffer
*
* @private
* @param {Array<number>} packed Array of integers to be converted to
* an ArrayBuffer
* @param {number} outputLength Length of output in bits
* @param {number} bigEndianMod Modifier for whether hash function is
* big or small endian
* @return {ArrayBuffer} Raw bytes representation of the parameter in an
* ArrayBuffer
*/
function packed2arraybuffer(packed, outputLength, bigEndianMod)
{
var length = outputLength / 8, i, retVal = new ArrayBuffer(length), shiftModifier, arrView;
arrView = new Uint8Array(retVal);
shiftModifier = (bigEndianMod === -1) ? 3 : 0;
for (i = 0; i < length; i += 1)
{
arrView[i] = (packed[i >>> 2] >>> (8 * (shiftModifier + bigEndianMod * (i % 4)))) & 0xFF;
}
return retVal;
}
/**
* Validate hash list containing output formatting options, ensuring
* presence of every option or adding the default value
*
* @private
* @param {{outputUpper : (boolean|undefined), b64Pad : (string|undefined),
* shakeLen : (number|undefined)}=} options Hash list of output formatting options
* @return {{outputUpper : boolean, b64Pad : string, shakeLen : number}} Validated
* hash list containing output formatting options
*/
function getOutputOpts(options)
{
var retVal = {"outputUpper" : false, "b64Pad" : "=", "shakeLen" : -1},
outputOptions;
outputOptions = options || {};
retVal["outputUpper"] = outputOptions["outputUpper"] || false;
if (true === outputOptions.hasOwnProperty("b64Pad"))
{
retVal["b64Pad"] = outputOptions["b64Pad"];
}
if ((true === outputOptions.hasOwnProperty("shakeLen")) && ((8 & SUPPORTED_ALGS) !== 0))
{
if (outputOptions["shakeLen"] % 8 !== 0)
{
throw new Error("shakeLen must be a multiple of 8");
}
retVal["shakeLen"] = outputOptions["shakeLen"];
}
if ("boolean" !== typeof(retVal["outputUpper"]))
{
throw new Error("Invalid outputUpper formatting option");
}
if ("string" !== typeof(retVal["b64Pad"]))
{
throw new Error("Invalid b64Pad formatting option");
}
return retVal;
}
/**
* Function that takes an input format and UTF encoding and returns the
* appropriate function used to convert the input.
*
* @private
* @param {string} format The format of the string to be converted
* @param {string} utfType The string encoding to use (UTF8, UTF16BE,
* UTF16LE)
* @param {number} bigEndianMod Modifier for whether hash function is
* big or small endian
* @return {function((string|ArrayBuffer), Array<number>=, number=): {value :
* Array<number>, binLen : number}} Function that will convert an input
* string to a packed int array
*/
function getStrConverter(format, utfType, bigEndianMod)
{
var retVal;
/* Validate encoding */
switch (utfType)
{
case "UTF8":
/* Fallthrough */
case "UTF16BE":
/* Fallthrough */
case "UTF16LE":
/* Fallthrough */
break;
default:
throw new Error("encoding must be UTF8, UTF16BE, or UTF16LE");
}
/* Map inputFormat to the appropriate converter */
switch (format)
{
case "HEX":
/**
* @param {string} str String of raw bytes to be converted to binary representation
* @param {Array<number>} existingBin A packed int array of bytes to
* append the results to
* @param {number} existingBinLen The number of bits in the existingBin
* array
* @return {{value : Array<number>, binLen : number}} Hash list where
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
retVal = function(str, existingBin, existingBinLen)
{
return hex2packed(str, existingBin, existingBinLen, bigEndianMod);
};
break;
case "TEXT":
/**
* @param {string} str String of raw bytes to be converted to binary representation
* @param {Array<number>} existingBin A packed int array of bytes to
* append the results to
* @param {number} existingBinLen The number of bits in the existingBin
* array
* @return {{value : Array<number>, binLen : number}} Hash list where
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
retVal = function(str, existingBin, existingBinLen)
{
return str2packed(str, utfType, existingBin, existingBinLen, bigEndianMod);
};
break;
case "B64":
/**
* @param {string} str String of raw bytes to be converted to binary representation
* @param {Array<number>} existingBin A packed int array of bytes to
* append the results to
* @param {number} existingBinLen The number of bits in the existingBin
* array
* @return {{value : Array<number>, binLen : number}} Hash list where
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
retVal = function(str, existingBin, existingBinLen)
{
return b642packed(str, existingBin, existingBinLen, bigEndianMod);
};
break;
case "BYTES":
/**
* @param {string} str String of raw bytes to be converted to binary representation
* @param {Array<number>} existingBin A packed int array of bytes to
* append the results to
* @param {number} existingBinLen The number of bits in the existingBin
* array
* @return {{value : Array<number>, binLen : number}} Hash list where
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
retVal = function(str, existingBin, existingBinLen)
{
return bytes2packed(str, existingBin, existingBinLen, bigEndianMod);
};
break;
case "ARRAYBUFFER":
try {
retVal = new ArrayBuffer(0);
} catch(ignore) {
throw new Error("ARRAYBUFFER not supported by this environment");
}
/**
* @param {ArrayBuffer} arr ArrayBuffer to be converted to binary
* representation
* @param {Array<number>} existingBin A packed int array of bytes to
* append the results to
* @param {number} existingBinLen The number of bits in the existingBin
* array
* @return {{value : Array<number>, binLen : number}} Hash list where
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
retVal = function(arr, existingBin, existingBinLen)
{
return arraybuffer2packed(arr, existingBin, existingBinLen, bigEndianMod);
};
break;
default:
throw new Error("format must be HEX, TEXT, B64, BYTES, or ARRAYBUFFER");
}
return retVal;
}
/**
* The 32-bit implementation of circular rotate left
*
* @private
* @param {number} x The 32-bit integer argument
* @param {number} n The number of bits to shift
* @return {number} The x shifted circularly by n bits
*/
function rotl_32(x, n)
{
return (x << n) | (x >>> (32 - n));
}
/**
* The 64-bit implementation of circular rotate left
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @param {number} n The number of bits to shift
* @return {Int_64} The x shifted circularly by n bits
*/
function rotl_64(x, n)
{
if (n > 32)
{
n = n - 32;
return new Int_64(
x.lowOrder << n | x.highOrder >>> (32 - n),
x.highOrder << n | x.lowOrder >>> (32 - n)
);
}
else if (0 !== n)
{
return new Int_64(
x.highOrder << n | x.lowOrder >>> (32 - n),
x.lowOrder << n | x.highOrder >>> (32 - n)
);
}
else
{
return x;
}
}
/**
* The 32-bit implementation of circular rotate right
*
* @private
* @param {number} x The 32-bit integer argument
* @param {number} n The number of bits to shift
* @return {number} The x shifted circularly by n bits
*/
function rotr_32(x, n)
{
return (x >>> n) | (x << (32 - n));
}
/**
* The 64-bit implementation of circular rotate right
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @param {number} n The number of bits to shift
* @return {Int_64} The x shifted circularly by n bits
*/
function rotr_64(x, n)
{
var retVal = null, tmp = new Int_64(x.highOrder, x.lowOrder);
if (32 >= n)
{
retVal = new Int_64(
(tmp.highOrder >>> n) | ((tmp.lowOrder << (32 - n)) & 0xFFFFFFFF),
(tmp.lowOrder >>> n) | ((tmp.highOrder << (32 - n)) & 0xFFFFFFFF)
);
}
else
{
retVal = new Int_64(
(tmp.lowOrder >>> (n - 32)) | ((tmp.highOrder << (64 - n)) & 0xFFFFFFFF),
(tmp.highOrder >>> (n - 32)) | ((tmp.lowOrder << (64 - n)) & 0xFFFFFFFF)
);
}
return retVal;
}
/**
* The 32-bit implementation of shift right
*
* @private
* @param {number} x The 32-bit integer argument
* @param {number} n The number of bits to shift
* @return {number} The x shifted by n bits
*/
function shr_32(x, n)
{
return x >>> n;
}
/**
* The 64-bit implementation of shift right
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @param {number} n The number of bits to shift
* @return {Int_64} The x shifted by n bits
*/
function shr_64(x, n)
{
var retVal = null;
if (32 >= n)
{
retVal = new Int_64(
x.highOrder >>> n,
x.lowOrder >>> n | ((x.highOrder << (32 - n)) & 0xFFFFFFFF)
);
}
else
{
retVal = new Int_64(
0,
x.highOrder >>> (n - 32)
);
}
return retVal;
}
/**
* The 32-bit implementation of the NIST specified Parity function
*
* @private
* @param {number} x The first 32-bit integer argument
* @param {number} y The second 32-bit integer argument
* @param {number} z The third 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function parity_32(x, y, z)
{
return x ^ y ^ z;
}
/**
* The 32-bit implementation of the NIST specified Ch function
*
* @private
* @param {number} x The first 32-bit integer argument
* @param {number} y The second 32-bit integer argument
* @param {number} z The third 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function ch_32(x, y, z)
{
return (x & y) ^ (~x & z);
}
/**
* The 64-bit implementation of the NIST specified Ch function
*
* @private
* @param {Int_64} x The first 64-bit integer argument
* @param {Int_64} y The second 64-bit integer argument
* @param {Int_64} z The third 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function ch_64(x, y, z)
{
return new Int_64(
(x.highOrder & y.highOrder) ^ (~x.highOrder & z.highOrder),
(x.lowOrder & y.lowOrder) ^ (~x.lowOrder & z.lowOrder)
);
}
/**
* The 32-bit implementation of the NIST specified Maj function
*
* @private
* @param {number} x The first 32-bit integer argument
* @param {number} y The second 32-bit integer argument
* @param {number} z The third 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function maj_32(x, y, z)
{
return (x & y) ^ (x & z) ^ (y & z);
}
/**
* The 64-bit implementation of the NIST specified Maj function
*
* @private
* @param {Int_64} x The first 64-bit integer argument
* @param {Int_64} y The second 64-bit integer argument
* @param {Int_64} z The third 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function maj_64(x, y, z)
{
return new Int_64(
(x.highOrder & y.highOrder) ^
(x.highOrder & z.highOrder) ^
(y.highOrder & z.highOrder),
(x.lowOrder & y.lowOrder) ^
(x.lowOrder & z.lowOrder) ^
(y.lowOrder & z.lowOrder)
);
}
/**
* The 32-bit implementation of the NIST specified Sigma0 function
*
* @private
* @param {number} x The 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function sigma0_32(x)
{
return rotr_32(x, 2) ^ rotr_32(x, 13) ^ rotr_32(x, 22);
}
/**
* The 64-bit implementation of the NIST specified Sigma0 function
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function sigma0_64(x)
{
var rotr28 = rotr_64(x, 28), rotr34 = rotr_64(x, 34),
rotr39 = rotr_64(x, 39);
return new Int_64(
rotr28.highOrder ^ rotr34.highOrder ^ rotr39.highOrder,
rotr28.lowOrder ^ rotr34.lowOrder ^ rotr39.lowOrder);
}
/**
* The 32-bit implementation of the NIST specified Sigma1 function
*
* @private
* @param {number} x The 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function sigma1_32(x)
{
return rotr_32(x, 6) ^ rotr_32(x, 11) ^ rotr_32(x, 25);
}
/**
* The 64-bit implementation of the NIST specified Sigma1 function
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function sigma1_64(x)
{
var rotr14 = rotr_64(x, 14), rotr18 = rotr_64(x, 18),
rotr41 = rotr_64(x, 41);
return new Int_64(
rotr14.highOrder ^ rotr18.highOrder ^ rotr41.highOrder,
rotr14.lowOrder ^ rotr18.lowOrder ^ rotr41.lowOrder);
}
/**
* The 32-bit implementation of the NIST specified Gamma0 function
*
* @private
* @param {number} x The 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function gamma0_32(x)
{
return rotr_32(x, 7) ^ rotr_32(x, 18) ^ shr_32(x, 3);
}
/**
* The 64-bit implementation of the NIST specified Gamma0 function
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function gamma0_64(x)
{
var rotr1 = rotr_64(x, 1), rotr8 = rotr_64(x, 8), shr7 = shr_64(x, 7);
return new Int_64(
rotr1.highOrder ^ rotr8.highOrder ^ shr7.highOrder,
rotr1.lowOrder ^ rotr8.lowOrder ^ shr7.lowOrder
);
}
/**
* The 32-bit implementation of the NIST specified Gamma1 function
*
* @private
* @param {number} x The 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function gamma1_32(x)
{
return rotr_32(x, 17) ^ rotr_32(x, 19) ^ shr_32(x, 10);
}
/**
* The 64-bit implementation of the NIST specified Gamma1 function
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function gamma1_64(x)
{
var rotr19 = rotr_64(x, 19), rotr61 = rotr_64(x, 61),
shr6 = shr_64(x, 6);
return new Int_64(
rotr19.highOrder ^ rotr61.highOrder ^ shr6.highOrder,
rotr19.lowOrder ^ rotr61.lowOrder ^ shr6.lowOrder
);
}
/**
* Add two 32-bit integers, wrapping at 2^32. This uses 16-bit operations
* internally to work around bugs in some JS interpreters.
*
* @private
* @param {number} a The first 32-bit integer argument to be added
* @param {number} b The second 32-bit integer argument to be added
* @return {number} The sum of a + b
*/
function safeAdd_32_2(a, b)
{