forked from h2non/jshashes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashes.js
1765 lines (1587 loc) · 58.2 KB
/
hashes.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
/**
* jshashes - https://github.com/h2non/jshashes
* Released under the "New BSD" license
*
* Algorithms specification:
*
* MD5 - http://www.ietf.org/rfc/rfc1321.txt
* RIPEMD-160 - http://homes.esat.kuleuven.be/~bosselae/ripemd160.html
* SHA1 - http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
* SHA256 - http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
* SHA512 - http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
* HMAC - http://www.ietf.org/rfc/rfc2104.txt
*/
(function() {
var Hashes;
function utf8Encode(str) {
var x, y, output = '',
i = -1,
l;
if (str && str.length) {
l = str.length;
while ((i += 1) < l) {
/* Decode utf-16 surrogate pairs */
x = str.charCodeAt(i);
y = i + 1 < l ? str.charCodeAt(i + 1) : 0;
if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
i += 1;
}
/* Encode output as utf-8 */
if (x <= 0x7F) {
output += String.fromCharCode(x);
} else if (x <= 0x7FF) {
output += String.fromCharCode(0xC0 | ((x >>> 6) & 0x1F),
0x80 | (x & 0x3F));
} else if (x <= 0xFFFF) {
output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
0x80 | ((x >>> 6) & 0x3F),
0x80 | (x & 0x3F));
} else if (x <= 0x1FFFFF) {
output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
0x80 | ((x >>> 12) & 0x3F),
0x80 | ((x >>> 6) & 0x3F),
0x80 | (x & 0x3F));
}
}
}
return output;
}
function utf8Decode(str) {
var i, ac, c1, c2, c3, arr = [],
l;
i = ac = c1 = c2 = c3 = 0;
if (str && str.length) {
l = str.length;
str += '';
while (i < l) {
c1 = str.charCodeAt(i);
ac += 1;
if (c1 < 128) {
arr[ac] = String.fromCharCode(c1);
i += 1;
} else if (c1 > 191 && c1 < 224) {
c2 = str.charCodeAt(i + 1);
arr[ac] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = str.charCodeAt(i + 1);
c3 = str.charCodeAt(i + 2);
arr[ac] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
}
return arr.join('');
}
/**
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
function safe_add(x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF),
msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
/**
* Bitwise rotate a 32-bit number to the left.
*/
function bit_rol(num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
/**
* Convert a raw string to a hex string
*/
function rstr2hex(input, hexcase) {
var hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef',
output = '',
x, i = 0,
l = input.length;
for (; i < l; i += 1) {
x = input.charCodeAt(i);
output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt(x & 0x0F);
}
return output;
}
/**
* Encode a string as utf-16
*/
function str2rstr_utf16le(input) {
var i, l = input.length,
output = '';
for (i = 0; i < l; i += 1) {
output += String.fromCharCode(input.charCodeAt(i) & 0xFF, (input.charCodeAt(i) >>> 8) & 0xFF);
}
return output;
}
function str2rstr_utf16be(input) {
var i, l = input.length,
output = '';
for (i = 0; i < l; i += 1) {
output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF, input.charCodeAt(i) & 0xFF);
}
return output;
}
/**
* Convert an array of big-endian words to a string
*/
function binb2rstr(input) {
var i, l = input.length * 32,
output = '';
for (i = 0; i < l; i += 8) {
output += String.fromCharCode((input[i >> 5] >>> (24 - i % 32)) & 0xFF);
}
return output;
}
/**
* Convert an array of little-endian words to a string
*/
function binl2rstr(input) {
var i, l = input.length * 32,
output = '';
for (i = 0; i < l; i += 8) {
output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
}
return output;
}
/**
* Convert a raw string to an array of little-endian words
* Characters >255 have their high-byte silently ignored.
*/
function rstr2binl(input) {
var i, l = input.length * 8,
output = Array(input.length >> 2),
lo = output.length;
for (i = 0; i < lo; i += 1) {
output[i] = 0;
}
for (i = 0; i < l; i += 8) {
output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
}
return output;
}
/**
* Convert a raw string to an array of big-endian words
* Characters >255 have their high-byte silently ignored.
*/
function rstr2binb(input) {
var i, l = input.length * 8,
output = Array(input.length >> 2),
lo = output.length;
for (i = 0; i < lo; i += 1) {
output[i] = 0;
}
for (i = 0; i < l; i += 8) {
output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
}
return output;
}
/**
* Convert a raw string to an arbitrary string encoding
*/
function rstr2any(input, encoding) {
var divisor = encoding.length,
remainders = Array(),
i, q, x, ld, quotient, dividend, output, full_length;
/* Convert to an array of 16-bit big-endian values, forming the dividend */
dividend = Array(Math.ceil(input.length / 2));
ld = dividend.length;
for (i = 0; i < ld; i += 1) {
dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
}
/**
* Repeatedly perform a long division. The binary array forms the dividend,
* the length of the encoding is the divisor. Once computed, the quotient
* forms the dividend for the next step. We stop when the dividend is zerHashes.
* All remainders are stored for later use.
*/
while (dividend.length > 0) {
quotient = Array();
x = 0;
for (i = 0; i < dividend.length; i += 1) {
x = (x << 16) + dividend[i];
q = Math.floor(x / divisor);
x -= q * divisor;
if (quotient.length > 0 || q > 0) {
quotient[quotient.length] = q;
}
}
remainders[remainders.length] = x;
dividend = quotient;
}
/* Convert the remainders to the output string */
output = '';
for (i = remainders.length - 1; i >= 0; i--) {
output += encoding.charAt(remainders[i]);
}
/* Append leading zero equivalents */
full_length = Math.ceil(input.length * 8 / (Math.log(encoding.length) / Math.log(2)));
for (i = output.length; i < full_length; i += 1) {
output = encoding[0] + output;
}
return output;
}
/**
* Convert a raw string to a base-64 string
*/
function rstr2b64(input, b64pad) {
var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
output = '',
len = input.length,
i, j, triplet;
b64pad = b64pad || '=';
for (i = 0; i < len; i += 3) {
triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i + 2) : 0);
for (j = 0; j < 4; j += 1) {
if (i * 8 + j * 6 > input.length * 8) {
output += b64pad;
} else {
output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F);
}
}
}
return output;
}
Hashes = {
/**
* @property {String} version
* @readonly
*/
VERSION: '1.0.6',
/**
* @member Hashes
* @class Base64
* @constructor
*/
Base64: function() {
// private properties
var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
pad = '=', // default pad according with the RFC standard
url = false, // URL encoding support @todo
utf8 = true; // by default enable UTF-8 support encoding
// public method for encoding
this.encode = function(input) {
var i, j, triplet,
output = '',
len = input.length;
pad = pad || '=';
input = (utf8) ? utf8Encode(input) : input;
for (i = 0; i < len; i += 3) {
triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i + 2) : 0);
for (j = 0; j < 4; j += 1) {
if (i * 8 + j * 6 > len * 8) {
output += pad;
} else {
output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F);
}
}
}
return output;
};
// public method for decoding
this.decode = function(input) {
// var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
var i, o1, o2, o3, h1, h2, h3, h4, bits, ac,
dec = '',
arr = [];
if (!input) {
return input;
}
i = ac = 0;
input = input.replace(new RegExp('\\' + pad, 'gi'), ''); // use '='
//input += '';
do { // unpack four hexets into three octets using index points in b64
h1 = tab.indexOf(input.charAt(i += 1));
h2 = tab.indexOf(input.charAt(i += 1));
h3 = tab.indexOf(input.charAt(i += 1));
h4 = tab.indexOf(input.charAt(i += 1));
bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
o1 = bits >> 16 & 0xff;
o2 = bits >> 8 & 0xff;
o3 = bits & 0xff;
ac += 1;
if (h3 === 64) {
arr[ac] = String.fromCharCode(o1);
} else if (h4 === 64) {
arr[ac] = String.fromCharCode(o1, o2);
} else {
arr[ac] = String.fromCharCode(o1, o2, o3);
}
} while (i < input.length);
dec = arr.join('');
dec = (utf8) ? utf8Decode(dec) : dec;
return dec;
};
// set custom pad string
this.setPad = function(str) {
pad = str || pad;
return this;
};
// set custom tab string characters
this.setTab = function(str) {
tab = str || tab;
return this;
};
this.setUTF8 = function(bool) {
if (typeof bool === 'boolean') {
utf8 = bool;
}
return this;
};
},
/**
* CRC-32 calculation
* @member Hashes
* @method CRC32
* @static
* @param {String} str Input String
* @return {String}
*/
CRC32: function(str) {
var crc = 0,
x = 0,
y = 0,
table, i, iTop;
str = utf8Encode(str);
table = [
'00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 ',
'79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 ',
'84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F ',
'63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD ',
'A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC ',
'51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 ',
'B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 ',
'06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 ',
'E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 ',
'12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 ',
'D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 ',
'33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 ',
'CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 ',
'9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E ',
'7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D ',
'806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 ',
'60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA ',
'AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 ',
'5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 ',
'B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 ',
'05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 ',
'F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA ',
'11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 ',
'D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F ',
'30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E ',
'C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D'
].join('');
crc = crc ^ (-1);
for (i = 0, iTop = str.length; i < iTop; i += 1) {
y = (crc ^ str.charCodeAt(i)) & 0xFF;
x = '0x' + table.substr(y * 9, 8);
crc = (crc >>> 8) ^ x;
}
// always return a positive number (that's what >>> 0 does)
return (crc ^ (-1)) >>> 0;
},
/**
* @member Hashes
* @class MD5
* @constructor
* @param {Object} [config]
*
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* See <http://pajhome.org.uk/crypt/md5> for more infHashes.
*/
MD5: function(options) {
/**
* Private config properties. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
* See {@link Hashes.MD5#method-setUpperCase} and {@link Hashes.SHA1#method-setUpperCase}
*/
var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase
b64pad = (options && typeof options.pad === 'string') ? options.pad : '=', // base-64 pad character. Defaults to '=' for strict RFC compliance
utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true; // enable/disable utf8 encoding
// privileged (public) methods
this.hex = function(s) {
return rstr2hex(rstr(s, utf8), hexcase);
};
this.b64 = function(s) {
return rstr2b64(rstr(s), b64pad);
};
this.any = function(s, e) {
return rstr2any(rstr(s, utf8), e);
};
this.raw = function(s) {
return rstr(s, utf8);
};
this.hex_hmac = function(k, d) {
return rstr2hex(rstr_hmac(k, d), hexcase);
};
this.b64_hmac = function(k, d) {
return rstr2b64(rstr_hmac(k, d), b64pad);
};
this.any_hmac = function(k, d, e) {
return rstr2any(rstr_hmac(k, d), e);
};
/**
* Perform a simple self-test to see if the VM is working
* @return {String} Hexadecimal hash sample
*/
this.vm_test = function() {
return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
};
/**
* Enable/disable uppercase hexadecimal returned string
* @param {Boolean}
* @return {Object} this
*/
this.setUpperCase = function(a) {
if (typeof a === 'boolean') {
hexcase = a;
}
return this;
};
/**
* Defines a base64 pad string
* @param {String} Pad
* @return {Object} this
*/
this.setPad = function(a) {
b64pad = a || b64pad;
return this;
};
/**
* Defines a base64 pad string
* @param {Boolean}
* @return {Object} [this]
*/
this.setUTF8 = function(a) {
if (typeof a === 'boolean') {
utf8 = a;
}
return this;
};
// private methods
/**
* Calculate the MD5 of a raw string
*/
function rstr(s) {
s = (utf8) ? utf8Encode(s) : s;
return binl2rstr(binl(rstr2binl(s), s.length * 8));
}
/**
* Calculate the HMAC-MD5, of a key and some data (raw strings)
*/
function rstr_hmac(key, data) {
var bkey, ipad, opad, hash, i;
key = (utf8) ? utf8Encode(key) : key;
data = (utf8) ? utf8Encode(data) : data;
bkey = rstr2binl(key);
if (bkey.length > 16) {
bkey = binl(bkey, key.length * 8);
}
ipad = Array(16), opad = Array(16);
for (i = 0; i < 16; i += 1) {
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
hash = binl(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
return binl2rstr(binl(opad.concat(hash), 512 + 128));
}
/**
* Calculate the MD5 of an array of little-endian words, and a bit length.
*/
function binl(x, len) {
var i, olda, oldb, oldc, oldd,
a = 1732584193,
b = -271733879,
c = -1732584194,
d = 271733878;
/* append padding */
x[len >> 5] |= 0x80 << ((len) % 32);
x[(((len + 64) >>> 9) << 4) + 14] = len;
for (i = 0; i < x.length; i += 16) {
olda = a;
oldb = b;
oldc = c;
oldd = d;
a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
}
return Array(a, b, c, d);
}
/**
* These functions implement the four basic operations the algorithm uses.
*/
function md5_cmn(q, a, b, x, s, t) {
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
}
function md5_ff(a, b, c, d, x, s, t) {
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t) {
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t) {
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t) {
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}
},
/**
* @member Hashes
* @class Hashes.SHA1
* @param {Object} [config]
* @constructor
*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined in FIPS 180-1
* Version 2.2 Copyright Paul Johnston 2000 - 2009.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* See http://pajhome.org.uk/crypt/md5 for details.
*/
SHA1: function(options) {
/**
* Private config properties. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
* See {@link Hashes.MD5#method-setUpperCase} and {@link Hashes.SHA1#method-setUpperCase}
*/
var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase
b64pad = (options && typeof options.pad === 'string') ? options.pad : '=', // base-64 pad character. Defaults to '=' for strict RFC compliance
utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true; // enable/disable utf8 encoding
// public methods
this.hex = function(s) {
return rstr2hex(rstr(s, utf8), hexcase);
};
this.b64 = function(s) {
return rstr2b64(rstr(s, utf8), b64pad);
};
this.any = function(s, e) {
return rstr2any(rstr(s, utf8), e);
};
this.raw = function(s) {
return rstr(s, utf8);
};
this.hex_hmac = function(k, d) {
return rstr2hex(rstr_hmac(k, d));
};
this.b64_hmac = function(k, d) {
return rstr2b64(rstr_hmac(k, d), b64pad);
};
this.any_hmac = function(k, d, e) {
return rstr2any(rstr_hmac(k, d), e);
};
/**
* Perform a simple self-test to see if the VM is working
* @return {String} Hexadecimal hash sample
* @public
*/
this.vm_test = function() {
return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
};
/**
* @description Enable/disable uppercase hexadecimal returned string
* @param {boolean}
* @return {Object} this
* @public
*/
this.setUpperCase = function(a) {
if (typeof a === 'boolean') {
hexcase = a;
}
return this;
};
/**
* @description Defines a base64 pad string
* @param {string} Pad
* @return {Object} this
* @public
*/
this.setPad = function(a) {
b64pad = a || b64pad;
return this;
};
/**
* @description Defines a base64 pad string
* @param {boolean}
* @return {Object} this
* @public
*/
this.setUTF8 = function(a) {
if (typeof a === 'boolean') {
utf8 = a;
}
return this;
};
// private methods
/**
* Calculate the SHA-512 of a raw string
*/
function rstr(s) {
s = (utf8) ? utf8Encode(s) : s;
return binb2rstr(binb(rstr2binb(s), s.length * 8));
}
/**
* Calculate the HMAC-SHA1 of a key and some data (raw strings)
*/
function rstr_hmac(key, data) {
var bkey, ipad, opad, i, hash;
key = (utf8) ? utf8Encode(key) : key;
data = (utf8) ? utf8Encode(data) : data;
bkey = rstr2binb(key);
if (bkey.length > 16) {
bkey = binb(bkey, key.length * 8);
}
ipad = Array(16), opad = Array(16);
for (i = 0; i < 16; i += 1) {
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
hash = binb(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
return binb2rstr(binb(opad.concat(hash), 512 + 160));
}
/**
* Calculate the SHA-1 of an array of big-endian words, and a bit length
*/
function binb(x, len) {
var i, j, t, olda, oldb, oldc, oldd, olde,
w = Array(80),
a = 1732584193,
b = -271733879,
c = -1732584194,
d = 271733878,
e = -1009589776;
/* append padding */
x[len >> 5] |= 0x80 << (24 - len % 32);
x[((len + 64 >> 9) << 4) + 15] = len;
for (i = 0; i < x.length; i += 16) {
olda = a;
oldb = b;
oldc = c;
oldd = d;
olde = e;
for (j = 0; j < 80; j += 1) {
if (j < 16) {
w[j] = x[i + j];
} else {
w[j] = bit_rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
}
t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)),
safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d;
d = c;
c = bit_rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
return Array(a, b, c, d, e);
}
/**
* Perform the appropriate triplet combination function for the current
* iteration
*/
function sha1_ft(t, b, c, d) {
if (t < 20) {
return (b & c) | ((~b) & d);
}
if (t < 40) {
return b ^ c ^ d;
}
if (t < 60) {
return (b & c) | (b & d) | (c & d);
}
return b ^ c ^ d;
}
/**
* Determine the appropriate additive constant for the current iteration
*/
function sha1_kt(t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
(t < 60) ? -1894007588 : -899497514;
}
},
/**
* @class Hashes.SHA256
* @param {config}
*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined in FIPS 180-2
* Version 2.2 Copyright Angel Marin, Paul Johnston 2000 - 2009.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* See http://pajhome.org.uk/crypt/md5 for details.
* Also http://anmar.eu.org/projects/jssha2/
*/
SHA256: function(options) {
/**
* Private properties configuration variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
* @see this.setUpperCase() method
* @see this.setPad() method
*/
var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase */
b64pad = (options && typeof options.pad === 'string') ? options.pad : '=',
/* base-64 pad character. Default '=' for strict RFC compliance */
utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true,
/* enable/disable utf8 encoding */
sha256_K;
/* privileged (public) methods */
this.hex = function(s) {
return rstr2hex(rstr(s, utf8));
};
this.b64 = function(s) {
return rstr2b64(rstr(s, utf8), b64pad);
};
this.any = function(s, e) {
return rstr2any(rstr(s, utf8), e);
};
this.raw = function(s) {
return rstr(s, utf8);
};
this.hex_hmac = function(k, d) {
return rstr2hex(rstr_hmac(k, d));
};
this.b64_hmac = function(k, d) {
return rstr2b64(rstr_hmac(k, d), b64pad);
};
this.any_hmac = function(k, d, e) {
return rstr2any(rstr_hmac(k, d), e);
};
/**
* Perform a simple self-test to see if the VM is working
* @return {String} Hexadecimal hash sample
* @public
*/
this.vm_test = function() {
return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
};
/**
* Enable/disable uppercase hexadecimal returned string
* @param {boolean}
* @return {Object} this
* @public
*/
this.setUpperCase = function(a) {
if (typeof a === 'boolean') {
hexcase = a;
}
return this;
};
/**
* @description Defines a base64 pad string
* @param {string} Pad
* @return {Object} this
* @public
*/
this.setPad = function(a) {
b64pad = a || b64pad;
return this;
};
/**
* Defines a base64 pad string
* @param {boolean}
* @return {Object} this
* @public
*/
this.setUTF8 = function(a) {
if (typeof a === 'boolean') {
utf8 = a;
}
return this;
};
// private methods
/**
* Calculate the SHA-512 of a raw string
*/
function rstr(s, utf8) {
s = (utf8) ? utf8Encode(s) : s;
return binb2rstr(binb(rstr2binb(s), s.length * 8));
}
/**
* Calculate the HMAC-sha256 of a key and some data (raw strings)
*/
function rstr_hmac(key, data) {
key = (utf8) ? utf8Encode(key) : key;
data = (utf8) ? utf8Encode(data) : data;
var hash, i = 0,
bkey = rstr2binb(key),
ipad = Array(16),
opad = Array(16);
if (bkey.length > 16) {
bkey = binb(bkey, key.length * 8);
}
for (; i < 16; i += 1) {
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
hash = binb(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
return binb2rstr(binb(opad.concat(hash), 512 + 256));
}
/*
* Main sha256 function, with its support functions
*/
function sha256_S(X, n) {
return (X >>> n) | (X << (32 - n));
}
function sha256_R(X, n) {
return (X >>> n);
}
function sha256_Ch(x, y, z) {
return ((x & y) ^ ((~x) & z));
}