blob: be5c2ea0cd2ec3829abd276455e463c8e7b7d393 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001;(function (root, factory) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory();
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define([], factory);
9 }
10 else {
11 // Global (browser)
12 root.CryptoJS = factory();
13 }
14}(this, function () {
15
16 /*globals window, global, require*/
17
18 /**
19 * CryptoJS core components.
20 */
21 var CryptoJS = CryptoJS || (function (Math, undefined) {
22
23 var crypto;
24
25 // Native crypto from window (Browser)
26 if (typeof window !== 'undefined' && window.crypto) {
27 crypto = window.crypto;
28 }
29
30 // Native (experimental IE 11) crypto from window (Browser)
31 if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
32 crypto = window.msCrypto;
33 }
34
35 // Native crypto from global (NodeJS)
36 if (!crypto && typeof global !== 'undefined' && global.crypto) {
37 crypto = global.crypto;
38 }
39
40 // Native crypto import via require (NodeJS)
41 if (!crypto && typeof require === 'function') {
42 try {
43 crypto = require('crypto');
44 } catch (err) {}
45 }
46
47 /*
48 * Cryptographically secure pseudorandom number generator
49 *
50 * As Math.random() is cryptographically not safe to use
51 */
52 var cryptoSecureRandomInt = function () {
53 if (crypto) {
54 // Use getRandomValues method (Browser)
55 if (typeof crypto.getRandomValues === 'function') {
56 try {
57 return crypto.getRandomValues(new Uint32Array(1))[0];
58 } catch (err) {}
59 }
60
61 // Use randomBytes method (NodeJS)
62 if (typeof crypto.randomBytes === 'function') {
63 try {
64 return crypto.randomBytes(4).readInt32LE();
65 } catch (err) {}
66 }
67 }
68
69 throw new Error('Native crypto module could not be used to get secure random number.');
70 };
71
72 /*
73 * Local polyfill of Object.create
74
75 */
76 var create = Object.create || (function () {
77 function F() {}
78
79 return function (obj) {
80 var subtype;
81
82 F.prototype = obj;
83
84 subtype = new F();
85
86 F.prototype = null;
87
88 return subtype;
89 };
90 }())
91
92 /**
93 * CryptoJS namespace.
94 */
95 var C = {};
96
97 /**
98 * Library namespace.
99 */
100 var C_lib = C.lib = {};
101
102 /**
103 * Base object for prototypal inheritance.
104 */
105 var Base = C_lib.Base = (function () {
106
107
108 return {
109 /**
110 * Creates a new object that inherits from this object.
111 *
112 * @param {Object} overrides Properties to copy into the new object.
113 *
114 * @return {Object} The new object.
115 *
116 * @static
117 *
118 * @example
119 *
120 * var MyType = CryptoJS.lib.Base.extend({
121 * field: 'value',
122 *
123 * method: function () {
124 * }
125 * });
126 */
127 extend: function (overrides) {
128 // Spawn
129 var subtype = create(this);
130
131 // Augment
132 if (overrides) {
133 subtype.mixIn(overrides);
134 }
135
136 // Create default initializer
137 if (!subtype.hasOwnProperty('init') || this.init === subtype.init) {
138 subtype.init = function () {
139 subtype.$super.init.apply(this, arguments);
140 };
141 }
142
143 // Initializer's prototype is the subtype object
144 subtype.init.prototype = subtype;
145
146 // Reference supertype
147 subtype.$super = this;
148
149 return subtype;
150 },
151
152 /**
153 * Extends this object and runs the init method.
154 * Arguments to create() will be passed to init().
155 *
156 * @return {Object} The new object.
157 *
158 * @static
159 *
160 * @example
161 *
162 * var instance = MyType.create();
163 */
164 create: function () {
165 var instance = this.extend();
166 instance.init.apply(instance, arguments);
167
168 return instance;
169 },
170
171 /**
172 * Initializes a newly created object.
173 * Override this method to add some logic when your objects are created.
174 *
175 * @example
176 *
177 * var MyType = CryptoJS.lib.Base.extend({
178 * init: function () {
179 * // ...
180 * }
181 * });
182 */
183 init: function () {
184 },
185
186 /**
187 * Copies properties into this object.
188 *
189 * @param {Object} properties The properties to mix in.
190 *
191 * @example
192 *
193 * MyType.mixIn({
194 * field: 'value'
195 * });
196 */
197 mixIn: function (properties) {
198 for (var propertyName in properties) {
199 if (properties.hasOwnProperty(propertyName)) {
200 this[propertyName] = properties[propertyName];
201 }
202 }
203
204 // IE won't copy toString using the loop above
205 if (properties.hasOwnProperty('toString')) {
206 this.toString = properties.toString;
207 }
208 },
209
210 /**
211 * Creates a copy of this object.
212 *
213 * @return {Object} The clone.
214 *
215 * @example
216 *
217 * var clone = instance.clone();
218 */
219 clone: function () {
220 return this.init.prototype.extend(this);
221 }
222 };
223 }());
224
225 /**
226 * An array of 32-bit words.
227 *
228 * @property {Array} words The array of 32-bit words.
229 * @property {number} sigBytes The number of significant bytes in this word array.
230 */
231 var WordArray = C_lib.WordArray = Base.extend({
232 /**
233 * Initializes a newly created word array.
234 *
235 * @param {Array} words (Optional) An array of 32-bit words.
236 * @param {number} sigBytes (Optional) The number of significant bytes in the words.
237 *
238 * @example
239 *
240 * var wordArray = CryptoJS.lib.WordArray.create();
241 * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
242 * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
243 */
244 init: function (words, sigBytes) {
245 words = this.words = words || [];
246
247 if (sigBytes != undefined) {
248 this.sigBytes = sigBytes;
249 } else {
250 this.sigBytes = words.length * 4;
251 }
252 },
253
254 /**
255 * Converts this word array to a string.
256 *
257 * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
258 *
259 * @return {string} The stringified word array.
260 *
261 * @example
262 *
263 * var string = wordArray + '';
264 * var string = wordArray.toString();
265 * var string = wordArray.toString(CryptoJS.enc.Utf8);
266 */
267 toString: function (encoder) {
268 return (encoder || Hex).stringify(this);
269 },
270
271 /**
272 * Concatenates a word array to this word array.
273 *
274 * @param {WordArray} wordArray The word array to append.
275 *
276 * @return {WordArray} This word array.
277 *
278 * @example
279 *
280 * wordArray1.concat(wordArray2);
281 */
282 concat: function (wordArray) {
283 // Shortcuts
284 var thisWords = this.words;
285 var thatWords = wordArray.words;
286 var thisSigBytes = this.sigBytes;
287 var thatSigBytes = wordArray.sigBytes;
288
289 // Clamp excess bits
290 this.clamp();
291
292 // Concat
293 if (thisSigBytes % 4) {
294 // Copy one byte at a time
295 for (var i = 0; i < thatSigBytes; i++) {
296 var thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
297 thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8);
298 }
299 } else {
300 // Copy one word at a time
301 for (var i = 0; i < thatSigBytes; i += 4) {
302 thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2];
303 }
304 }
305 this.sigBytes += thatSigBytes;
306
307 // Chainable
308 return this;
309 },
310
311 /**
312 * Removes insignificant bits.
313 *
314 * @example
315 *
316 * wordArray.clamp();
317 */
318 clamp: function () {
319 // Shortcuts
320 var words = this.words;
321 var sigBytes = this.sigBytes;
322
323 // Clamp
324 words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8);
325 words.length = Math.ceil(sigBytes / 4);
326 },
327
328 /**
329 * Creates a copy of this word array.
330 *
331 * @return {WordArray} The clone.
332 *
333 * @example
334 *
335 * var clone = wordArray.clone();
336 */
337 clone: function () {
338 var clone = Base.clone.call(this);
339 clone.words = this.words.slice(0);
340
341 return clone;
342 },
343
344 /**
345 * Creates a word array filled with random bytes.
346 *
347 * @param {number} nBytes The number of random bytes to generate.
348 *
349 * @return {WordArray} The random word array.
350 *
351 * @static
352 *
353 * @example
354 *
355 * var wordArray = CryptoJS.lib.WordArray.random(16);
356 */
357 random: function (nBytes) {
358 var words = [];
359
360 for (var i = 0; i < nBytes; i += 4) {
361 words.push(cryptoSecureRandomInt());
362 }
363
364 return new WordArray.init(words, nBytes);
365 }
366 });
367
368 /**
369 * Encoder namespace.
370 */
371 var C_enc = C.enc = {};
372
373 /**
374 * Hex encoding strategy.
375 */
376 var Hex = C_enc.Hex = {
377 /**
378 * Converts a word array to a hex string.
379 *
380 * @param {WordArray} wordArray The word array.
381 *
382 * @return {string} The hex string.
383 *
384 * @static
385 *
386 * @example
387 *
388 * var hexString = CryptoJS.enc.Hex.stringify(wordArray);
389 */
390 stringify: function (wordArray) {
391 // Shortcuts
392 var words = wordArray.words;
393 var sigBytes = wordArray.sigBytes;
394
395 // Convert
396 var hexChars = [];
397 for (var i = 0; i < sigBytes; i++) {
398 var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
399 hexChars.push((bite >>> 4).toString(16));
400 hexChars.push((bite & 0x0f).toString(16));
401 }
402
403 return hexChars.join('');
404 },
405
406 /**
407 * Converts a hex string to a word array.
408 *
409 * @param {string} hexStr The hex string.
410 *
411 * @return {WordArray} The word array.
412 *
413 * @static
414 *
415 * @example
416 *
417 * var wordArray = CryptoJS.enc.Hex.parse(hexString);
418 */
419 parse: function (hexStr) {
420 // Shortcut
421 var hexStrLength = hexStr.length;
422
423 // Convert
424 var words = [];
425 for (var i = 0; i < hexStrLength; i += 2) {
426 words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
427 }
428
429 return new WordArray.init(words, hexStrLength / 2);
430 }
431 };
432
433 /**
434 * Latin1 encoding strategy.
435 */
436 var Latin1 = C_enc.Latin1 = {
437 /**
438 * Converts a word array to a Latin1 string.
439 *
440 * @param {WordArray} wordArray The word array.
441 *
442 * @return {string} The Latin1 string.
443 *
444 * @static
445 *
446 * @example
447 *
448 * var latin1String = CryptoJS.enc.Latin1.stringify(wordArray);
449 */
450 stringify: function (wordArray) {
451 // Shortcuts
452 var words = wordArray.words;
453 var sigBytes = wordArray.sigBytes;
454
455 // Convert
456 var latin1Chars = [];
457 for (var i = 0; i < sigBytes; i++) {
458 var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
459 latin1Chars.push(String.fromCharCode(bite));
460 }
461
462 return latin1Chars.join('');
463 },
464
465 /**
466 * Converts a Latin1 string to a word array.
467 *
468 * @param {string} latin1Str The Latin1 string.
469 *
470 * @return {WordArray} The word array.
471 *
472 * @static
473 *
474 * @example
475 *
476 * var wordArray = CryptoJS.enc.Latin1.parse(latin1String);
477 */
478 parse: function (latin1Str) {
479 // Shortcut
480 var latin1StrLength = latin1Str.length;
481
482 // Convert
483 var words = [];
484 for (var i = 0; i < latin1StrLength; i++) {
485 words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
486 }
487
488 return new WordArray.init(words, latin1StrLength);
489 }
490 };
491
492 /**
493 * UTF-8 encoding strategy.
494 */
495 var Utf8 = C_enc.Utf8 = {
496 /**
497 * Converts a word array to a UTF-8 string.
498 *
499 * @param {WordArray} wordArray The word array.
500 *
501 * @return {string} The UTF-8 string.
502 *
503 * @static
504 *
505 * @example
506 *
507 * var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
508 */
509 stringify: function (wordArray) {
510 try {
511 return decodeURIComponent(escape(Latin1.stringify(wordArray)));
512 } catch (e) {
513 throw new Error('Malformed UTF-8 data');
514 }
515 },
516
517 /**
518 * Converts a UTF-8 string to a word array.
519 *
520 * @param {string} utf8Str The UTF-8 string.
521 *
522 * @return {WordArray} The word array.
523 *
524 * @static
525 *
526 * @example
527 *
528 * var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
529 */
530 parse: function (utf8Str) {
531 return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
532 }
533 };
534
535 /**
536 * Abstract buffered block algorithm template.
537 *
538 * The property blockSize must be implemented in a concrete subtype.
539 *
540 * @property {number} _minBufferSize The number of blocks that should be kept unprocessed in the buffer. Default: 0
541 */
542 var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm = Base.extend({
543 /**
544 * Resets this block algorithm's data buffer to its initial state.
545 *
546 * @example
547 *
548 * bufferedBlockAlgorithm.reset();
549 */
550 reset: function () {
551 // Initial values
552 this._data = new WordArray.init();
553 this._nDataBytes = 0;
554 },
555
556 /**
557 * Adds new data to this block algorithm's buffer.
558 *
559 * @param {WordArray|string} data The data to append. Strings are converted to a WordArray using UTF-8.
560 *
561 * @example
562 *
563 * bufferedBlockAlgorithm._append('data');
564 * bufferedBlockAlgorithm._append(wordArray);
565 */
566 _append: function (data) {
567 // Convert string to WordArray, else assume WordArray already
568 if (typeof data == 'string') {
569 data = Utf8.parse(data);
570 }
571
572 // Append
573 this._data.concat(data);
574 this._nDataBytes += data.sigBytes;
575 },
576
577 /**
578 * Processes available data blocks.
579 *
580 * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
581 *
582 * @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
583 *
584 * @return {WordArray} The processed data.
585 *
586 * @example
587 *
588 * var processedData = bufferedBlockAlgorithm._process();
589 * var processedData = bufferedBlockAlgorithm._process(!!'flush');
590 */
591 _process: function (doFlush) {
592 var processedWords;
593
594 // Shortcuts
595 var data = this._data;
596 var dataWords = data.words;
597 var dataSigBytes = data.sigBytes;
598 var blockSize = this.blockSize;
599 var blockSizeBytes = blockSize * 4;
600
601 // Count blocks ready
602 var nBlocksReady = dataSigBytes / blockSizeBytes;
603 if (doFlush) {
604 // Round up to include partial blocks
605 nBlocksReady = Math.ceil(nBlocksReady);
606 } else {
607 // Round down to include only full blocks,
608 // less the number of blocks that must remain in the buffer
609 nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
610 }
611
612 // Count words ready
613 var nWordsReady = nBlocksReady * blockSize;
614
615 // Count bytes ready
616 var nBytesReady = Math.min(nWordsReady * 4, dataSigBytes);
617
618 // Process blocks
619 if (nWordsReady) {
620 for (var offset = 0; offset < nWordsReady; offset += blockSize) {
621 // Perform concrete-algorithm logic
622 this._doProcessBlock(dataWords, offset);
623 }
624
625 // Remove processed words
626 processedWords = dataWords.splice(0, nWordsReady);
627 data.sigBytes -= nBytesReady;
628 }
629
630 // Return processed words
631 return new WordArray.init(processedWords, nBytesReady);
632 },
633
634 /**
635 * Creates a copy of this object.
636 *
637 * @return {Object} The clone.
638 *
639 * @example
640 *
641 * var clone = bufferedBlockAlgorithm.clone();
642 */
643 clone: function () {
644 var clone = Base.clone.call(this);
645 clone._data = this._data.clone();
646
647 return clone;
648 },
649
650 _minBufferSize: 0
651 });
652
653 /**
654 * Abstract hasher template.
655 *
656 * @property {number} blockSize The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
657 */
658 var Hasher = C_lib.Hasher = BufferedBlockAlgorithm.extend({
659 /**
660 * Configuration options.
661 */
662 cfg: Base.extend(),
663
664 /**
665 * Initializes a newly created hasher.
666 *
667 * @param {Object} cfg (Optional) The configuration options to use for this hash computation.
668 *
669 * @example
670 *
671 * var hasher = CryptoJS.algo.SHA256.create();
672 */
673 init: function (cfg) {
674 // Apply config defaults
675 this.cfg = this.cfg.extend(cfg);
676
677 // Set initial values
678 this.reset();
679 },
680
681 /**
682 * Resets this hasher to its initial state.
683 *
684 * @example
685 *
686 * hasher.reset();
687 */
688 reset: function () {
689 // Reset data buffer
690 BufferedBlockAlgorithm.reset.call(this);
691
692 // Perform concrete-hasher logic
693 this._doReset();
694 },
695
696 /**
697 * Updates this hasher with a message.
698 *
699 * @param {WordArray|string} messageUpdate The message to append.
700 *
701 * @return {Hasher} This hasher.
702 *
703 * @example
704 *
705 * hasher.update('message');
706 * hasher.update(wordArray);
707 */
708 update: function (messageUpdate) {
709 // Append
710 this._append(messageUpdate);
711
712 // Update the hash
713 this._process();
714
715 // Chainable
716 return this;
717 },
718
719 /**
720 * Finalizes the hash computation.
721 * Note that the finalize operation is effectively a destructive, read-once operation.
722 *
723 * @param {WordArray|string} messageUpdate (Optional) A final message update.
724 *
725 * @return {WordArray} The hash.
726 *
727 * @example
728 *
729 * var hash = hasher.finalize();
730 * var hash = hasher.finalize('message');
731 * var hash = hasher.finalize(wordArray);
732 */
733 finalize: function (messageUpdate) {
734 // Final message update
735 if (messageUpdate) {
736 this._append(messageUpdate);
737 }
738
739 // Perform concrete-hasher logic
740 var hash = this._doFinalize();
741
742 return hash;
743 },
744
745 blockSize: 512/32,
746
747 /**
748 * Creates a shortcut function to a hasher's object interface.
749 *
750 * @param {Hasher} hasher The hasher to create a helper for.
751 *
752 * @return {Function} The shortcut function.
753 *
754 * @static
755 *
756 * @example
757 *
758 * var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
759 */
760 _createHelper: function (hasher) {
761 return function (message, cfg) {
762 return new hasher.init(cfg).finalize(message);
763 };
764 },
765
766 /**
767 * Creates a shortcut function to the HMAC's object interface.
768 *
769 * @param {Hasher} hasher The hasher to use in this HMAC helper.
770 *
771 * @return {Function} The shortcut function.
772 *
773 * @static
774 *
775 * @example
776 *
777 * var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
778 */
779 _createHmacHelper: function (hasher) {
780 return function (message, key) {
781 return new C_algo.HMAC.init(hasher, key).finalize(message);
782 };
783 }
784 });
785
786 /**
787 * Algorithm namespace.
788 */
789 var C_algo = C.algo = {};
790
791 return C;
792 }(Math));
793
794
795 (function () {
796 // Shortcuts
797 var C = CryptoJS;
798 var C_lib = C.lib;
799 var WordArray = C_lib.WordArray;
800 var C_enc = C.enc;
801
802 /**
803 * Base64 encoding strategy.
804 */
805 var Base64 = C_enc.Base64 = {
806 /**
807 * Converts a word array to a Base64 string.
808 *
809 * @param {WordArray} wordArray The word array.
810 *
811 * @return {string} The Base64 string.
812 *
813 * @static
814 *
815 * @example
816 *
817 * var base64String = CryptoJS.enc.Base64.stringify(wordArray);
818 */
819 stringify: function (wordArray) {
820 // Shortcuts
821 var words = wordArray.words;
822 var sigBytes = wordArray.sigBytes;
823 var map = this._map;
824
825 // Clamp excess bits
826 wordArray.clamp();
827
828 // Convert
829 var base64Chars = [];
830 for (var i = 0; i < sigBytes; i += 3) {
831 var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
832 var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
833 var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
834
835 var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
836
837 for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
838 base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
839 }
840 }
841
842 // Add padding
843 var paddingChar = map.charAt(64);
844 if (paddingChar) {
845 while (base64Chars.length % 4) {
846 base64Chars.push(paddingChar);
847 }
848 }
849
850 return base64Chars.join('');
851 },
852
853 /**
854 * Converts a Base64 string to a word array.
855 *
856 * @param {string} base64Str The Base64 string.
857 *
858 * @return {WordArray} The word array.
859 *
860 * @static
861 *
862 * @example
863 *
864 * var wordArray = CryptoJS.enc.Base64.parse(base64String);
865 */
866 parse: function (base64Str) {
867 // Shortcuts
868 var base64StrLength = base64Str.length;
869 var map = this._map;
870 var reverseMap = this._reverseMap;
871
872 if (!reverseMap) {
873 reverseMap = this._reverseMap = [];
874 for (var j = 0; j < map.length; j++) {
875 reverseMap[map.charCodeAt(j)] = j;
876 }
877 }
878
879 // Ignore padding
880 var paddingChar = map.charAt(64);
881 if (paddingChar) {
882 var paddingIndex = base64Str.indexOf(paddingChar);
883 if (paddingIndex !== -1) {
884 base64StrLength = paddingIndex;
885 }
886 }
887
888 // Convert
889 return parseLoop(base64Str, base64StrLength, reverseMap);
890
891 },
892
893 _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
894 };
895
896 function parseLoop(base64Str, base64StrLength, reverseMap) {
897 var words = [];
898 var nBytes = 0;
899 for (var i = 0; i < base64StrLength; i++) {
900 if (i % 4) {
901 var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);
902 var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);
903 var bitsCombined = bits1 | bits2;
904 words[nBytes >>> 2] |= bitsCombined << (24 - (nBytes % 4) * 8);
905 nBytes++;
906 }
907 }
908 return WordArray.create(words, nBytes);
909 }
910 }());
911
912
913 (function (Math) {
914 // Shortcuts
915 var C = CryptoJS;
916 var C_lib = C.lib;
917 var WordArray = C_lib.WordArray;
918 var Hasher = C_lib.Hasher;
919 var C_algo = C.algo;
920
921 // Constants table
922 var T = [];
923
924 // Compute constants
925 (function () {
926 for (var i = 0; i < 64; i++) {
927 T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0;
928 }
929 }());
930
931 /**
932 * MD5 hash algorithm.
933 */
934 var MD5 = C_algo.MD5 = Hasher.extend({
935 _doReset: function () {
936 this._hash = new WordArray.init([
937 0x67452301, 0xefcdab89,
938 0x98badcfe, 0x10325476
939 ]);
940 },
941
942 _doProcessBlock: function (M, offset) {
943 // Swap endian
944 for (var i = 0; i < 16; i++) {
945 // Shortcuts
946 var offset_i = offset + i;
947 var M_offset_i = M[offset_i];
948
949 M[offset_i] = (
950 (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
951 (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
952 );
953 }
954
955 // Shortcuts
956 var H = this._hash.words;
957
958 var M_offset_0 = M[offset + 0];
959 var M_offset_1 = M[offset + 1];
960 var M_offset_2 = M[offset + 2];
961 var M_offset_3 = M[offset + 3];
962 var M_offset_4 = M[offset + 4];
963 var M_offset_5 = M[offset + 5];
964 var M_offset_6 = M[offset + 6];
965 var M_offset_7 = M[offset + 7];
966 var M_offset_8 = M[offset + 8];
967 var M_offset_9 = M[offset + 9];
968 var M_offset_10 = M[offset + 10];
969 var M_offset_11 = M[offset + 11];
970 var M_offset_12 = M[offset + 12];
971 var M_offset_13 = M[offset + 13];
972 var M_offset_14 = M[offset + 14];
973 var M_offset_15 = M[offset + 15];
974
975 // Working varialbes
976 var a = H[0];
977 var b = H[1];
978 var c = H[2];
979 var d = H[3];
980
981 // Computation
982 a = FF(a, b, c, d, M_offset_0, 7, T[0]);
983 d = FF(d, a, b, c, M_offset_1, 12, T[1]);
984 c = FF(c, d, a, b, M_offset_2, 17, T[2]);
985 b = FF(b, c, d, a, M_offset_3, 22, T[3]);
986 a = FF(a, b, c, d, M_offset_4, 7, T[4]);
987 d = FF(d, a, b, c, M_offset_5, 12, T[5]);
988 c = FF(c, d, a, b, M_offset_6, 17, T[6]);
989 b = FF(b, c, d, a, M_offset_7, 22, T[7]);
990 a = FF(a, b, c, d, M_offset_8, 7, T[8]);
991 d = FF(d, a, b, c, M_offset_9, 12, T[9]);
992 c = FF(c, d, a, b, M_offset_10, 17, T[10]);
993 b = FF(b, c, d, a, M_offset_11, 22, T[11]);
994 a = FF(a, b, c, d, M_offset_12, 7, T[12]);
995 d = FF(d, a, b, c, M_offset_13, 12, T[13]);
996 c = FF(c, d, a, b, M_offset_14, 17, T[14]);
997 b = FF(b, c, d, a, M_offset_15, 22, T[15]);
998
999 a = GG(a, b, c, d, M_offset_1, 5, T[16]);
1000 d = GG(d, a, b, c, M_offset_6, 9, T[17]);
1001 c = GG(c, d, a, b, M_offset_11, 14, T[18]);
1002 b = GG(b, c, d, a, M_offset_0, 20, T[19]);
1003 a = GG(a, b, c, d, M_offset_5, 5, T[20]);
1004 d = GG(d, a, b, c, M_offset_10, 9, T[21]);
1005 c = GG(c, d, a, b, M_offset_15, 14, T[22]);
1006 b = GG(b, c, d, a, M_offset_4, 20, T[23]);
1007 a = GG(a, b, c, d, M_offset_9, 5, T[24]);
1008 d = GG(d, a, b, c, M_offset_14, 9, T[25]);
1009 c = GG(c, d, a, b, M_offset_3, 14, T[26]);
1010 b = GG(b, c, d, a, M_offset_8, 20, T[27]);
1011 a = GG(a, b, c, d, M_offset_13, 5, T[28]);
1012 d = GG(d, a, b, c, M_offset_2, 9, T[29]);
1013 c = GG(c, d, a, b, M_offset_7, 14, T[30]);
1014 b = GG(b, c, d, a, M_offset_12, 20, T[31]);
1015
1016 a = HH(a, b, c, d, M_offset_5, 4, T[32]);
1017 d = HH(d, a, b, c, M_offset_8, 11, T[33]);
1018 c = HH(c, d, a, b, M_offset_11, 16, T[34]);
1019 b = HH(b, c, d, a, M_offset_14, 23, T[35]);
1020 a = HH(a, b, c, d, M_offset_1, 4, T[36]);
1021 d = HH(d, a, b, c, M_offset_4, 11, T[37]);
1022 c = HH(c, d, a, b, M_offset_7, 16, T[38]);
1023 b = HH(b, c, d, a, M_offset_10, 23, T[39]);
1024 a = HH(a, b, c, d, M_offset_13, 4, T[40]);
1025 d = HH(d, a, b, c, M_offset_0, 11, T[41]);
1026 c = HH(c, d, a, b, M_offset_3, 16, T[42]);
1027 b = HH(b, c, d, a, M_offset_6, 23, T[43]);
1028 a = HH(a, b, c, d, M_offset_9, 4, T[44]);
1029 d = HH(d, a, b, c, M_offset_12, 11, T[45]);
1030 c = HH(c, d, a, b, M_offset_15, 16, T[46]);
1031 b = HH(b, c, d, a, M_offset_2, 23, T[47]);
1032
1033 a = II(a, b, c, d, M_offset_0, 6, T[48]);
1034 d = II(d, a, b, c, M_offset_7, 10, T[49]);
1035 c = II(c, d, a, b, M_offset_14, 15, T[50]);
1036 b = II(b, c, d, a, M_offset_5, 21, T[51]);
1037 a = II(a, b, c, d, M_offset_12, 6, T[52]);
1038 d = II(d, a, b, c, M_offset_3, 10, T[53]);
1039 c = II(c, d, a, b, M_offset_10, 15, T[54]);
1040 b = II(b, c, d, a, M_offset_1, 21, T[55]);
1041 a = II(a, b, c, d, M_offset_8, 6, T[56]);
1042 d = II(d, a, b, c, M_offset_15, 10, T[57]);
1043 c = II(c, d, a, b, M_offset_6, 15, T[58]);
1044 b = II(b, c, d, a, M_offset_13, 21, T[59]);
1045 a = II(a, b, c, d, M_offset_4, 6, T[60]);
1046 d = II(d, a, b, c, M_offset_11, 10, T[61]);
1047 c = II(c, d, a, b, M_offset_2, 15, T[62]);
1048 b = II(b, c, d, a, M_offset_9, 21, T[63]);
1049
1050 // Intermediate hash value
1051 H[0] = (H[0] + a) | 0;
1052 H[1] = (H[1] + b) | 0;
1053 H[2] = (H[2] + c) | 0;
1054 H[3] = (H[3] + d) | 0;
1055 },
1056
1057 _doFinalize: function () {
1058 // Shortcuts
1059 var data = this._data;
1060 var dataWords = data.words;
1061
1062 var nBitsTotal = this._nDataBytes * 8;
1063 var nBitsLeft = data.sigBytes * 8;
1064
1065 // Add padding
1066 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
1067
1068 var nBitsTotalH = Math.floor(nBitsTotal / 0x100000000);
1069 var nBitsTotalL = nBitsTotal;
1070 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = (
1071 (((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff) |
1072 (((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00)
1073 );
1074 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
1075 (((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff) |
1076 (((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00)
1077 );
1078
1079 data.sigBytes = (dataWords.length + 1) * 4;
1080
1081 // Hash final blocks
1082 this._process();
1083
1084 // Shortcuts
1085 var hash = this._hash;
1086 var H = hash.words;
1087
1088 // Swap endian
1089 for (var i = 0; i < 4; i++) {
1090 // Shortcut
1091 var H_i = H[i];
1092
1093 H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
1094 (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
1095 }
1096
1097 // Return final computed hash
1098 return hash;
1099 },
1100
1101 clone: function () {
1102 var clone = Hasher.clone.call(this);
1103 clone._hash = this._hash.clone();
1104
1105 return clone;
1106 }
1107 });
1108
1109 function FF(a, b, c, d, x, s, t) {
1110 var n = a + ((b & c) | (~b & d)) + x + t;
1111 return ((n << s) | (n >>> (32 - s))) + b;
1112 }
1113
1114 function GG(a, b, c, d, x, s, t) {
1115 var n = a + ((b & d) | (c & ~d)) + x + t;
1116 return ((n << s) | (n >>> (32 - s))) + b;
1117 }
1118
1119 function HH(a, b, c, d, x, s, t) {
1120 var n = a + (b ^ c ^ d) + x + t;
1121 return ((n << s) | (n >>> (32 - s))) + b;
1122 }
1123
1124 function II(a, b, c, d, x, s, t) {
1125 var n = a + (c ^ (b | ~d)) + x + t;
1126 return ((n << s) | (n >>> (32 - s))) + b;
1127 }
1128
1129 /**
1130 * Shortcut function to the hasher's object interface.
1131 *
1132 * @param {WordArray|string} message The message to hash.
1133 *
1134 * @return {WordArray} The hash.
1135 *
1136 * @static
1137 *
1138 * @example
1139 *
1140 * var hash = CryptoJS.MD5('message');
1141 * var hash = CryptoJS.MD5(wordArray);
1142 */
1143 C.MD5 = Hasher._createHelper(MD5);
1144
1145 /**
1146 * Shortcut function to the HMAC's object interface.
1147 *
1148 * @param {WordArray|string} message The message to hash.
1149 * @param {WordArray|string} key The secret key.
1150 *
1151 * @return {WordArray} The HMAC.
1152 *
1153 * @static
1154 *
1155 * @example
1156 *
1157 * var hmac = CryptoJS.HmacMD5(message, key);
1158 */
1159 C.HmacMD5 = Hasher._createHmacHelper(MD5);
1160 }(Math));
1161
1162
1163 (function () {
1164 // Shortcuts
1165 var C = CryptoJS;
1166 var C_lib = C.lib;
1167 var WordArray = C_lib.WordArray;
1168 var Hasher = C_lib.Hasher;
1169 var C_algo = C.algo;
1170
1171 // Reusable object
1172 var W = [];
1173
1174 /**
1175 * SHA-1 hash algorithm.
1176 */
1177 var SHA1 = C_algo.SHA1 = Hasher.extend({
1178 _doReset: function () {
1179 this._hash = new WordArray.init([
1180 0x67452301, 0xefcdab89,
1181 0x98badcfe, 0x10325476,
1182 0xc3d2e1f0
1183 ]);
1184 },
1185
1186 _doProcessBlock: function (M, offset) {
1187 // Shortcut
1188 var H = this._hash.words;
1189
1190 // Working variables
1191 var a = H[0];
1192 var b = H[1];
1193 var c = H[2];
1194 var d = H[3];
1195 var e = H[4];
1196
1197 // Computation
1198 for (var i = 0; i < 80; i++) {
1199 if (i < 16) {
1200 W[i] = M[offset + i] | 0;
1201 } else {
1202 var n = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
1203 W[i] = (n << 1) | (n >>> 31);
1204 }
1205
1206 var t = ((a << 5) | (a >>> 27)) + e + W[i];
1207 if (i < 20) {
1208 t += ((b & c) | (~b & d)) + 0x5a827999;
1209 } else if (i < 40) {
1210 t += (b ^ c ^ d) + 0x6ed9eba1;
1211 } else if (i < 60) {
1212 t += ((b & c) | (b & d) | (c & d)) - 0x70e44324;
1213 } else /* if (i < 80) */ {
1214 t += (b ^ c ^ d) - 0x359d3e2a;
1215 }
1216
1217 e = d;
1218 d = c;
1219 c = (b << 30) | (b >>> 2);
1220 b = a;
1221 a = t;
1222 }
1223
1224 // Intermediate hash value
1225 H[0] = (H[0] + a) | 0;
1226 H[1] = (H[1] + b) | 0;
1227 H[2] = (H[2] + c) | 0;
1228 H[3] = (H[3] + d) | 0;
1229 H[4] = (H[4] + e) | 0;
1230 },
1231
1232 _doFinalize: function () {
1233 // Shortcuts
1234 var data = this._data;
1235 var dataWords = data.words;
1236
1237 var nBitsTotal = this._nDataBytes * 8;
1238 var nBitsLeft = data.sigBytes * 8;
1239
1240 // Add padding
1241 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
1242 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
1243 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
1244 data.sigBytes = dataWords.length * 4;
1245
1246 // Hash final blocks
1247 this._process();
1248
1249 // Return final computed hash
1250 return this._hash;
1251 },
1252
1253 clone: function () {
1254 var clone = Hasher.clone.call(this);
1255 clone._hash = this._hash.clone();
1256
1257 return clone;
1258 }
1259 });
1260
1261 /**
1262 * Shortcut function to the hasher's object interface.
1263 *
1264 * @param {WordArray|string} message The message to hash.
1265 *
1266 * @return {WordArray} The hash.
1267 *
1268 * @static
1269 *
1270 * @example
1271 *
1272 * var hash = CryptoJS.SHA1('message');
1273 * var hash = CryptoJS.SHA1(wordArray);
1274 */
1275 C.SHA1 = Hasher._createHelper(SHA1);
1276
1277 /**
1278 * Shortcut function to the HMAC's object interface.
1279 *
1280 * @param {WordArray|string} message The message to hash.
1281 * @param {WordArray|string} key The secret key.
1282 *
1283 * @return {WordArray} The HMAC.
1284 *
1285 * @static
1286 *
1287 * @example
1288 *
1289 * var hmac = CryptoJS.HmacSHA1(message, key);
1290 */
1291 C.HmacSHA1 = Hasher._createHmacHelper(SHA1);
1292 }());
1293
1294
1295 (function (Math) {
1296 // Shortcuts
1297 var C = CryptoJS;
1298 var C_lib = C.lib;
1299 var WordArray = C_lib.WordArray;
1300 var Hasher = C_lib.Hasher;
1301 var C_algo = C.algo;
1302
1303 // Initialization and round constants tables
1304 var H = [];
1305 var K = [];
1306
1307 // Compute constants
1308 (function () {
1309 function isPrime(n) {
1310 var sqrtN = Math.sqrt(n);
1311 for (var factor = 2; factor <= sqrtN; factor++) {
1312 if (!(n % factor)) {
1313 return false;
1314 }
1315 }
1316
1317 return true;
1318 }
1319
1320 function getFractionalBits(n) {
1321 return ((n - (n | 0)) * 0x100000000) | 0;
1322 }
1323
1324 var n = 2;
1325 var nPrime = 0;
1326 while (nPrime < 64) {
1327 if (isPrime(n)) {
1328 if (nPrime < 8) {
1329 H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2));
1330 }
1331 K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3));
1332
1333 nPrime++;
1334 }
1335
1336 n++;
1337 }
1338 }());
1339
1340 // Reusable object
1341 var W = [];
1342
1343 /**
1344 * SHA-256 hash algorithm.
1345 */
1346 var SHA256 = C_algo.SHA256 = Hasher.extend({
1347 _doReset: function () {
1348 this._hash = new WordArray.init(H.slice(0));
1349 },
1350
1351 _doProcessBlock: function (M, offset) {
1352 // Shortcut
1353 var H = this._hash.words;
1354
1355 // Working variables
1356 var a = H[0];
1357 var b = H[1];
1358 var c = H[2];
1359 var d = H[3];
1360 var e = H[4];
1361 var f = H[5];
1362 var g = H[6];
1363 var h = H[7];
1364
1365 // Computation
1366 for (var i = 0; i < 64; i++) {
1367 if (i < 16) {
1368 W[i] = M[offset + i] | 0;
1369 } else {
1370 var gamma0x = W[i - 15];
1371 var gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
1372 ((gamma0x << 14) | (gamma0x >>> 18)) ^
1373 (gamma0x >>> 3);
1374
1375 var gamma1x = W[i - 2];
1376 var gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
1377 ((gamma1x << 13) | (gamma1x >>> 19)) ^
1378 (gamma1x >>> 10);
1379
1380 W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
1381 }
1382
1383 var ch = (e & f) ^ (~e & g);
1384 var maj = (a & b) ^ (a & c) ^ (b & c);
1385
1386 var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
1387 var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
1388
1389 var t1 = h + sigma1 + ch + K[i] + W[i];
1390 var t2 = sigma0 + maj;
1391
1392 h = g;
1393 g = f;
1394 f = e;
1395 e = (d + t1) | 0;
1396 d = c;
1397 c = b;
1398 b = a;
1399 a = (t1 + t2) | 0;
1400 }
1401
1402 // Intermediate hash value
1403 H[0] = (H[0] + a) | 0;
1404 H[1] = (H[1] + b) | 0;
1405 H[2] = (H[2] + c) | 0;
1406 H[3] = (H[3] + d) | 0;
1407 H[4] = (H[4] + e) | 0;
1408 H[5] = (H[5] + f) | 0;
1409 H[6] = (H[6] + g) | 0;
1410 H[7] = (H[7] + h) | 0;
1411 },
1412
1413 _doFinalize: function () {
1414 // Shortcuts
1415 var data = this._data;
1416 var dataWords = data.words;
1417
1418 var nBitsTotal = this._nDataBytes * 8;
1419 var nBitsLeft = data.sigBytes * 8;
1420
1421 // Add padding
1422 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
1423 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
1424 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
1425 data.sigBytes = dataWords.length * 4;
1426
1427 // Hash final blocks
1428 this._process();
1429
1430 // Return final computed hash
1431 return this._hash;
1432 },
1433
1434 clone: function () {
1435 var clone = Hasher.clone.call(this);
1436 clone._hash = this._hash.clone();
1437
1438 return clone;
1439 }
1440 });
1441
1442 /**
1443 * Shortcut function to the hasher's object interface.
1444 *
1445 * @param {WordArray|string} message The message to hash.
1446 *
1447 * @return {WordArray} The hash.
1448 *
1449 * @static
1450 *
1451 * @example
1452 *
1453 * var hash = CryptoJS.SHA256('message');
1454 * var hash = CryptoJS.SHA256(wordArray);
1455 */
1456 C.SHA256 = Hasher._createHelper(SHA256);
1457
1458 /**
1459 * Shortcut function to the HMAC's object interface.
1460 *
1461 * @param {WordArray|string} message The message to hash.
1462 * @param {WordArray|string} key The secret key.
1463 *
1464 * @return {WordArray} The HMAC.
1465 *
1466 * @static
1467 *
1468 * @example
1469 *
1470 * var hmac = CryptoJS.HmacSHA256(message, key);
1471 */
1472 C.HmacSHA256 = Hasher._createHmacHelper(SHA256);
1473 }(Math));
1474
1475
1476 (function () {
1477 // Shortcuts
1478 var C = CryptoJS;
1479 var C_lib = C.lib;
1480 var WordArray = C_lib.WordArray;
1481 var C_enc = C.enc;
1482
1483 /**
1484 * UTF-16 BE encoding strategy.
1485 */
1486 var Utf16BE = C_enc.Utf16 = C_enc.Utf16BE = {
1487 /**
1488 * Converts a word array to a UTF-16 BE string.
1489 *
1490 * @param {WordArray} wordArray The word array.
1491 *
1492 * @return {string} The UTF-16 BE string.
1493 *
1494 * @static
1495 *
1496 * @example
1497 *
1498 * var utf16String = CryptoJS.enc.Utf16.stringify(wordArray);
1499 */
1500 stringify: function (wordArray) {
1501 // Shortcuts
1502 var words = wordArray.words;
1503 var sigBytes = wordArray.sigBytes;
1504
1505 // Convert
1506 var utf16Chars = [];
1507 for (var i = 0; i < sigBytes; i += 2) {
1508 var codePoint = (words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff;
1509 utf16Chars.push(String.fromCharCode(codePoint));
1510 }
1511
1512 return utf16Chars.join('');
1513 },
1514
1515 /**
1516 * Converts a UTF-16 BE string to a word array.
1517 *
1518 * @param {string} utf16Str The UTF-16 BE string.
1519 *
1520 * @return {WordArray} The word array.
1521 *
1522 * @static
1523 *
1524 * @example
1525 *
1526 * var wordArray = CryptoJS.enc.Utf16.parse(utf16String);
1527 */
1528 parse: function (utf16Str) {
1529 // Shortcut
1530 var utf16StrLength = utf16Str.length;
1531
1532 // Convert
1533 var words = [];
1534 for (var i = 0; i < utf16StrLength; i++) {
1535 words[i >>> 1] |= utf16Str.charCodeAt(i) << (16 - (i % 2) * 16);
1536 }
1537
1538 return WordArray.create(words, utf16StrLength * 2);
1539 }
1540 };
1541
1542 /**
1543 * UTF-16 LE encoding strategy.
1544 */
1545 C_enc.Utf16LE = {
1546 /**
1547 * Converts a word array to a UTF-16 LE string.
1548 *
1549 * @param {WordArray} wordArray The word array.
1550 *
1551 * @return {string} The UTF-16 LE string.
1552 *
1553 * @static
1554 *
1555 * @example
1556 *
1557 * var utf16Str = CryptoJS.enc.Utf16LE.stringify(wordArray);
1558 */
1559 stringify: function (wordArray) {
1560 // Shortcuts
1561 var words = wordArray.words;
1562 var sigBytes = wordArray.sigBytes;
1563
1564 // Convert
1565 var utf16Chars = [];
1566 for (var i = 0; i < sigBytes; i += 2) {
1567 var codePoint = swapEndian((words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff);
1568 utf16Chars.push(String.fromCharCode(codePoint));
1569 }
1570
1571 return utf16Chars.join('');
1572 },
1573
1574 /**
1575 * Converts a UTF-16 LE string to a word array.
1576 *
1577 * @param {string} utf16Str The UTF-16 LE string.
1578 *
1579 * @return {WordArray} The word array.
1580 *
1581 * @static
1582 *
1583 * @example
1584 *
1585 * var wordArray = CryptoJS.enc.Utf16LE.parse(utf16Str);
1586 */
1587 parse: function (utf16Str) {
1588 // Shortcut
1589 var utf16StrLength = utf16Str.length;
1590
1591 // Convert
1592 var words = [];
1593 for (var i = 0; i < utf16StrLength; i++) {
1594 words[i >>> 1] |= swapEndian(utf16Str.charCodeAt(i) << (16 - (i % 2) * 16));
1595 }
1596
1597 return WordArray.create(words, utf16StrLength * 2);
1598 }
1599 };
1600
1601 function swapEndian(word) {
1602 return ((word << 8) & 0xff00ff00) | ((word >>> 8) & 0x00ff00ff);
1603 }
1604 }());
1605
1606
1607 (function () {
1608 // Check if typed arrays are supported
1609 if (typeof ArrayBuffer != 'function') {
1610 return;
1611 }
1612
1613 // Shortcuts
1614 var C = CryptoJS;
1615 var C_lib = C.lib;
1616 var WordArray = C_lib.WordArray;
1617
1618 // Reference original init
1619 var superInit = WordArray.init;
1620
1621 // Augment WordArray.init to handle typed arrays
1622 var subInit = WordArray.init = function (typedArray) {
1623 // Convert buffers to uint8
1624 if (typedArray instanceof ArrayBuffer) {
1625 typedArray = new Uint8Array(typedArray);
1626 }
1627
1628 // Convert other array views to uint8
1629 if (
1630 typedArray instanceof Int8Array ||
1631 (typeof Uint8ClampedArray !== "undefined" && typedArray instanceof Uint8ClampedArray) ||
1632 typedArray instanceof Int16Array ||
1633 typedArray instanceof Uint16Array ||
1634 typedArray instanceof Int32Array ||
1635 typedArray instanceof Uint32Array ||
1636 typedArray instanceof Float32Array ||
1637 typedArray instanceof Float64Array
1638 ) {
1639 typedArray = new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength);
1640 }
1641
1642 // Handle Uint8Array
1643 if (typedArray instanceof Uint8Array) {
1644 // Shortcut
1645 var typedArrayByteLength = typedArray.byteLength;
1646
1647 // Extract bytes
1648 var words = [];
1649 for (var i = 0; i < typedArrayByteLength; i++) {
1650 words[i >>> 2] |= typedArray[i] << (24 - (i % 4) * 8);
1651 }
1652
1653 // Initialize this word array
1654 superInit.call(this, words, typedArrayByteLength);
1655 } else {
1656 // Else call normal init
1657 superInit.apply(this, arguments);
1658 }
1659 };
1660
1661 subInit.prototype = WordArray;
1662 }());
1663
1664
1665 /** @preserve
1666 (c) 2012 by Cédric Mesnil. All rights reserved.
1667
1668 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1669
1670 - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
1671 - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
1672
1673 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1674 */
1675
1676 (function (Math) {
1677 // Shortcuts
1678 var C = CryptoJS;
1679 var C_lib = C.lib;
1680 var WordArray = C_lib.WordArray;
1681 var Hasher = C_lib.Hasher;
1682 var C_algo = C.algo;
1683
1684 // Constants table
1685 var _zl = WordArray.create([
1686 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1687 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
1688 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1689 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
1690 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13]);
1691 var _zr = WordArray.create([
1692 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
1693 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
1694 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
1695 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
1696 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11]);
1697 var _sl = WordArray.create([
1698 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
1699 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
1700 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
1701 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
1702 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 ]);
1703 var _sr = WordArray.create([
1704 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
1705 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
1706 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
1707 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
1708 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 ]);
1709
1710 var _hl = WordArray.create([ 0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]);
1711 var _hr = WordArray.create([ 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000]);
1712
1713 /**
1714 * RIPEMD160 hash algorithm.
1715 */
1716 var RIPEMD160 = C_algo.RIPEMD160 = Hasher.extend({
1717 _doReset: function () {
1718 this._hash = WordArray.create([0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]);
1719 },
1720
1721 _doProcessBlock: function (M, offset) {
1722
1723 // Swap endian
1724 for (var i = 0; i < 16; i++) {
1725 // Shortcuts
1726 var offset_i = offset + i;
1727 var M_offset_i = M[offset_i];
1728
1729 // Swap
1730 M[offset_i] = (
1731 (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
1732 (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
1733 );
1734 }
1735 // Shortcut
1736 var H = this._hash.words;
1737 var hl = _hl.words;
1738 var hr = _hr.words;
1739 var zl = _zl.words;
1740 var zr = _zr.words;
1741 var sl = _sl.words;
1742 var sr = _sr.words;
1743
1744 // Working variables
1745 var al, bl, cl, dl, el;
1746 var ar, br, cr, dr, er;
1747
1748 ar = al = H[0];
1749 br = bl = H[1];
1750 cr = cl = H[2];
1751 dr = dl = H[3];
1752 er = el = H[4];
1753 // Computation
1754 var t;
1755 for (var i = 0; i < 80; i += 1) {
1756 t = (al + M[offset+zl[i]])|0;
1757 if (i<16){
1758 t += f1(bl,cl,dl) + hl[0];
1759 } else if (i<32) {
1760 t += f2(bl,cl,dl) + hl[1];
1761 } else if (i<48) {
1762 t += f3(bl,cl,dl) + hl[2];
1763 } else if (i<64) {
1764 t += f4(bl,cl,dl) + hl[3];
1765 } else {// if (i<80) {
1766 t += f5(bl,cl,dl) + hl[4];
1767 }
1768 t = t|0;
1769 t = rotl(t,sl[i]);
1770 t = (t+el)|0;
1771 al = el;
1772 el = dl;
1773 dl = rotl(cl, 10);
1774 cl = bl;
1775 bl = t;
1776
1777 t = (ar + M[offset+zr[i]])|0;
1778 if (i<16){
1779 t += f5(br,cr,dr) + hr[0];
1780 } else if (i<32) {
1781 t += f4(br,cr,dr) + hr[1];
1782 } else if (i<48) {
1783 t += f3(br,cr,dr) + hr[2];
1784 } else if (i<64) {
1785 t += f2(br,cr,dr) + hr[3];
1786 } else {// if (i<80) {
1787 t += f1(br,cr,dr) + hr[4];
1788 }
1789 t = t|0;
1790 t = rotl(t,sr[i]) ;
1791 t = (t+er)|0;
1792 ar = er;
1793 er = dr;
1794 dr = rotl(cr, 10);
1795 cr = br;
1796 br = t;
1797 }
1798 // Intermediate hash value
1799 t = (H[1] + cl + dr)|0;
1800 H[1] = (H[2] + dl + er)|0;
1801 H[2] = (H[3] + el + ar)|0;
1802 H[3] = (H[4] + al + br)|0;
1803 H[4] = (H[0] + bl + cr)|0;
1804 H[0] = t;
1805 },
1806
1807 _doFinalize: function () {
1808 // Shortcuts
1809 var data = this._data;
1810 var dataWords = data.words;
1811
1812 var nBitsTotal = this._nDataBytes * 8;
1813 var nBitsLeft = data.sigBytes * 8;
1814
1815 // Add padding
1816 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
1817 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
1818 (((nBitsTotal << 8) | (nBitsTotal >>> 24)) & 0x00ff00ff) |
1819 (((nBitsTotal << 24) | (nBitsTotal >>> 8)) & 0xff00ff00)
1820 );
1821 data.sigBytes = (dataWords.length + 1) * 4;
1822
1823 // Hash final blocks
1824 this._process();
1825
1826 // Shortcuts
1827 var hash = this._hash;
1828 var H = hash.words;
1829
1830 // Swap endian
1831 for (var i = 0; i < 5; i++) {
1832 // Shortcut
1833 var H_i = H[i];
1834
1835 // Swap
1836 H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
1837 (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
1838 }
1839
1840 // Return final computed hash
1841 return hash;
1842 },
1843
1844 clone: function () {
1845 var clone = Hasher.clone.call(this);
1846 clone._hash = this._hash.clone();
1847
1848 return clone;
1849 }
1850 });
1851
1852
1853 function f1(x, y, z) {
1854 return ((x) ^ (y) ^ (z));
1855
1856 }
1857
1858 function f2(x, y, z) {
1859 return (((x)&(y)) | ((~x)&(z)));
1860 }
1861
1862 function f3(x, y, z) {
1863 return (((x) | (~(y))) ^ (z));
1864 }
1865
1866 function f4(x, y, z) {
1867 return (((x) & (z)) | ((y)&(~(z))));
1868 }
1869
1870 function f5(x, y, z) {
1871 return ((x) ^ ((y) |(~(z))));
1872
1873 }
1874
1875 function rotl(x,n) {
1876 return (x<<n) | (x>>>(32-n));
1877 }
1878
1879
1880 /**
1881 * Shortcut function to the hasher's object interface.
1882 *
1883 * @param {WordArray|string} message The message to hash.
1884 *
1885 * @return {WordArray} The hash.
1886 *
1887 * @static
1888 *
1889 * @example
1890 *
1891 * var hash = CryptoJS.RIPEMD160('message');
1892 * var hash = CryptoJS.RIPEMD160(wordArray);
1893 */
1894 C.RIPEMD160 = Hasher._createHelper(RIPEMD160);
1895
1896 /**
1897 * Shortcut function to the HMAC's object interface.
1898 *
1899 * @param {WordArray|string} message The message to hash.
1900 * @param {WordArray|string} key The secret key.
1901 *
1902 * @return {WordArray} The HMAC.
1903 *
1904 * @static
1905 *
1906 * @example
1907 *
1908 * var hmac = CryptoJS.HmacRIPEMD160(message, key);
1909 */
1910 C.HmacRIPEMD160 = Hasher._createHmacHelper(RIPEMD160);
1911 }(Math));
1912
1913
1914 (function () {
1915 // Shortcuts
1916 var C = CryptoJS;
1917 var C_lib = C.lib;
1918 var Base = C_lib.Base;
1919 var C_enc = C.enc;
1920 var Utf8 = C_enc.Utf8;
1921 var C_algo = C.algo;
1922
1923 /**
1924 * HMAC algorithm.
1925 */
1926 var HMAC = C_algo.HMAC = Base.extend({
1927 /**
1928 * Initializes a newly created HMAC.
1929 *
1930 * @param {Hasher} hasher The hash algorithm to use.
1931 * @param {WordArray|string} key The secret key.
1932 *
1933 * @example
1934 *
1935 * var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
1936 */
1937 init: function (hasher, key) {
1938 // Init hasher
1939 hasher = this._hasher = new hasher.init();
1940
1941 // Convert string to WordArray, else assume WordArray already
1942 if (typeof key == 'string') {
1943 key = Utf8.parse(key);
1944 }
1945
1946 // Shortcuts
1947 var hasherBlockSize = hasher.blockSize;
1948 var hasherBlockSizeBytes = hasherBlockSize * 4;
1949
1950 // Allow arbitrary length keys
1951 if (key.sigBytes > hasherBlockSizeBytes) {
1952 key = hasher.finalize(key);
1953 }
1954
1955 // Clamp excess bits
1956 key.clamp();
1957
1958 // Clone key for inner and outer pads
1959 var oKey = this._oKey = key.clone();
1960 var iKey = this._iKey = key.clone();
1961
1962 // Shortcuts
1963 var oKeyWords = oKey.words;
1964 var iKeyWords = iKey.words;
1965
1966 // XOR keys with pad constants
1967 for (var i = 0; i < hasherBlockSize; i++) {
1968 oKeyWords[i] ^= 0x5c5c5c5c;
1969 iKeyWords[i] ^= 0x36363636;
1970 }
1971 oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
1972
1973 // Set initial values
1974 this.reset();
1975 },
1976
1977 /**
1978 * Resets this HMAC to its initial state.
1979 *
1980 * @example
1981 *
1982 * hmacHasher.reset();
1983 */
1984 reset: function () {
1985 // Shortcut
1986 var hasher = this._hasher;
1987
1988 // Reset
1989 hasher.reset();
1990 hasher.update(this._iKey);
1991 },
1992
1993 /**
1994 * Updates this HMAC with a message.
1995 *
1996 * @param {WordArray|string} messageUpdate The message to append.
1997 *
1998 * @return {HMAC} This HMAC instance.
1999 *
2000 * @example
2001 *
2002 * hmacHasher.update('message');
2003 * hmacHasher.update(wordArray);
2004 */
2005 update: function (messageUpdate) {
2006 this._hasher.update(messageUpdate);
2007
2008 // Chainable
2009 return this;
2010 },
2011
2012 /**
2013 * Finalizes the HMAC computation.
2014 * Note that the finalize operation is effectively a destructive, read-once operation.
2015 *
2016 * @param {WordArray|string} messageUpdate (Optional) A final message update.
2017 *
2018 * @return {WordArray} The HMAC.
2019 *
2020 * @example
2021 *
2022 * var hmac = hmacHasher.finalize();
2023 * var hmac = hmacHasher.finalize('message');
2024 * var hmac = hmacHasher.finalize(wordArray);
2025 */
2026 finalize: function (messageUpdate) {
2027 // Shortcut
2028 var hasher = this._hasher;
2029
2030 // Compute HMAC
2031 var innerHash = hasher.finalize(messageUpdate);
2032 hasher.reset();
2033 var hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
2034
2035 return hmac;
2036 }
2037 });
2038 }());
2039
2040
2041 (function () {
2042 // Shortcuts
2043 var C = CryptoJS;
2044 var C_lib = C.lib;
2045 var Base = C_lib.Base;
2046 var WordArray = C_lib.WordArray;
2047 var C_algo = C.algo;
2048 var SHA1 = C_algo.SHA1;
2049 var HMAC = C_algo.HMAC;
2050
2051 /**
2052 * Password-Based Key Derivation Function 2 algorithm.
2053 */
2054 var PBKDF2 = C_algo.PBKDF2 = Base.extend({
2055 /**
2056 * Configuration options.
2057 *
2058 * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
2059 * @property {Hasher} hasher The hasher to use. Default: SHA1
2060 * @property {number} iterations The number of iterations to perform. Default: 1
2061 */
2062 cfg: Base.extend({
2063 keySize: 128/32,
2064 hasher: SHA1,
2065 iterations: 1
2066 }),
2067
2068 /**
2069 * Initializes a newly created key derivation function.
2070 *
2071 * @param {Object} cfg (Optional) The configuration options to use for the derivation.
2072 *
2073 * @example
2074 *
2075 * var kdf = CryptoJS.algo.PBKDF2.create();
2076 * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8 });
2077 * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8, iterations: 1000 });
2078 */
2079 init: function (cfg) {
2080 this.cfg = this.cfg.extend(cfg);
2081 },
2082
2083 /**
2084 * Computes the Password-Based Key Derivation Function 2.
2085 *
2086 * @param {WordArray|string} password The password.
2087 * @param {WordArray|string} salt A salt.
2088 *
2089 * @return {WordArray} The derived key.
2090 *
2091 * @example
2092 *
2093 * var key = kdf.compute(password, salt);
2094 */
2095 compute: function (password, salt) {
2096 // Shortcut
2097 var cfg = this.cfg;
2098
2099 // Init HMAC
2100 var hmac = HMAC.create(cfg.hasher, password);
2101
2102 // Initial values
2103 var derivedKey = WordArray.create();
2104 var blockIndex = WordArray.create([0x00000001]);
2105
2106 // Shortcuts
2107 var derivedKeyWords = derivedKey.words;
2108 var blockIndexWords = blockIndex.words;
2109 var keySize = cfg.keySize;
2110 var iterations = cfg.iterations;
2111
2112 // Generate key
2113 while (derivedKeyWords.length < keySize) {
2114 var block = hmac.update(salt).finalize(blockIndex);
2115 hmac.reset();
2116
2117 // Shortcuts
2118 var blockWords = block.words;
2119 var blockWordsLength = blockWords.length;
2120
2121 // Iterations
2122 var intermediate = block;
2123 for (var i = 1; i < iterations; i++) {
2124 intermediate = hmac.finalize(intermediate);
2125 hmac.reset();
2126
2127 // Shortcut
2128 var intermediateWords = intermediate.words;
2129
2130 // XOR intermediate with block
2131 for (var j = 0; j < blockWordsLength; j++) {
2132 blockWords[j] ^= intermediateWords[j];
2133 }
2134 }
2135
2136 derivedKey.concat(block);
2137 blockIndexWords[0]++;
2138 }
2139 derivedKey.sigBytes = keySize * 4;
2140
2141 return derivedKey;
2142 }
2143 });
2144
2145 /**
2146 * Computes the Password-Based Key Derivation Function 2.
2147 *
2148 * @param {WordArray|string} password The password.
2149 * @param {WordArray|string} salt A salt.
2150 * @param {Object} cfg (Optional) The configuration options to use for this computation.
2151 *
2152 * @return {WordArray} The derived key.
2153 *
2154 * @static
2155 *
2156 * @example
2157 *
2158 * var key = CryptoJS.PBKDF2(password, salt);
2159 * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8 });
2160 * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8, iterations: 1000 });
2161 */
2162 C.PBKDF2 = function (password, salt, cfg) {
2163 return PBKDF2.create(cfg).compute(password, salt);
2164 };
2165 }());
2166
2167
2168 (function () {
2169 // Shortcuts
2170 var C = CryptoJS;
2171 var C_lib = C.lib;
2172 var Base = C_lib.Base;
2173 var WordArray = C_lib.WordArray;
2174 var C_algo = C.algo;
2175 var MD5 = C_algo.MD5;
2176
2177 /**
2178 * This key derivation function is meant to conform with EVP_BytesToKey.
2179 * www.openssl.org/docs/crypto/EVP_BytesToKey.html
2180 */
2181 var EvpKDF = C_algo.EvpKDF = Base.extend({
2182 /**
2183 * Configuration options.
2184 *
2185 * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
2186 * @property {Hasher} hasher The hash algorithm to use. Default: MD5
2187 * @property {number} iterations The number of iterations to perform. Default: 1
2188 */
2189 cfg: Base.extend({
2190 keySize: 128/32,
2191 hasher: MD5,
2192 iterations: 1
2193 }),
2194
2195 /**
2196 * Initializes a newly created key derivation function.
2197 *
2198 * @param {Object} cfg (Optional) The configuration options to use for the derivation.
2199 *
2200 * @example
2201 *
2202 * var kdf = CryptoJS.algo.EvpKDF.create();
2203 * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8 });
2204 * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8, iterations: 1000 });
2205 */
2206 init: function (cfg) {
2207 this.cfg = this.cfg.extend(cfg);
2208 },
2209
2210 /**
2211 * Derives a key from a password.
2212 *
2213 * @param {WordArray|string} password The password.
2214 * @param {WordArray|string} salt A salt.
2215 *
2216 * @return {WordArray} The derived key.
2217 *
2218 * @example
2219 *
2220 * var key = kdf.compute(password, salt);
2221 */
2222 compute: function (password, salt) {
2223 var block;
2224
2225 // Shortcut
2226 var cfg = this.cfg;
2227
2228 // Init hasher
2229 var hasher = cfg.hasher.create();
2230
2231 // Initial values
2232 var derivedKey = WordArray.create();
2233
2234 // Shortcuts
2235 var derivedKeyWords = derivedKey.words;
2236 var keySize = cfg.keySize;
2237 var iterations = cfg.iterations;
2238
2239 // Generate key
2240 while (derivedKeyWords.length < keySize) {
2241 if (block) {
2242 hasher.update(block);
2243 }
2244 block = hasher.update(password).finalize(salt);
2245 hasher.reset();
2246
2247 // Iterations
2248 for (var i = 1; i < iterations; i++) {
2249 block = hasher.finalize(block);
2250 hasher.reset();
2251 }
2252
2253 derivedKey.concat(block);
2254 }
2255 derivedKey.sigBytes = keySize * 4;
2256
2257 return derivedKey;
2258 }
2259 });
2260
2261 /**
2262 * Derives a key from a password.
2263 *
2264 * @param {WordArray|string} password The password.
2265 * @param {WordArray|string} salt A salt.
2266 * @param {Object} cfg (Optional) The configuration options to use for this computation.
2267 *
2268 * @return {WordArray} The derived key.
2269 *
2270 * @static
2271 *
2272 * @example
2273 *
2274 * var key = CryptoJS.EvpKDF(password, salt);
2275 * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8 });
2276 * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8, iterations: 1000 });
2277 */
2278 C.EvpKDF = function (password, salt, cfg) {
2279 return EvpKDF.create(cfg).compute(password, salt);
2280 };
2281 }());
2282
2283
2284 (function () {
2285 // Shortcuts
2286 var C = CryptoJS;
2287 var C_lib = C.lib;
2288 var WordArray = C_lib.WordArray;
2289 var C_algo = C.algo;
2290 var SHA256 = C_algo.SHA256;
2291
2292 /**
2293 * SHA-224 hash algorithm.
2294 */
2295 var SHA224 = C_algo.SHA224 = SHA256.extend({
2296 _doReset: function () {
2297 this._hash = new WordArray.init([
2298 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
2299 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
2300 ]);
2301 },
2302
2303 _doFinalize: function () {
2304 var hash = SHA256._doFinalize.call(this);
2305
2306 hash.sigBytes -= 4;
2307
2308 return hash;
2309 }
2310 });
2311
2312 /**
2313 * Shortcut function to the hasher's object interface.
2314 *
2315 * @param {WordArray|string} message The message to hash.
2316 *
2317 * @return {WordArray} The hash.
2318 *
2319 * @static
2320 *
2321 * @example
2322 *
2323 * var hash = CryptoJS.SHA224('message');
2324 * var hash = CryptoJS.SHA224(wordArray);
2325 */
2326 C.SHA224 = SHA256._createHelper(SHA224);
2327
2328 /**
2329 * Shortcut function to the HMAC's object interface.
2330 *
2331 * @param {WordArray|string} message The message to hash.
2332 * @param {WordArray|string} key The secret key.
2333 *
2334 * @return {WordArray} The HMAC.
2335 *
2336 * @static
2337 *
2338 * @example
2339 *
2340 * var hmac = CryptoJS.HmacSHA224(message, key);
2341 */
2342 C.HmacSHA224 = SHA256._createHmacHelper(SHA224);
2343 }());
2344
2345
2346 (function (undefined) {
2347 // Shortcuts
2348 var C = CryptoJS;
2349 var C_lib = C.lib;
2350 var Base = C_lib.Base;
2351 var X32WordArray = C_lib.WordArray;
2352
2353 /**
2354 * x64 namespace.
2355 */
2356 var C_x64 = C.x64 = {};
2357
2358 /**
2359 * A 64-bit word.
2360 */
2361 var X64Word = C_x64.Word = Base.extend({
2362 /**
2363 * Initializes a newly created 64-bit word.
2364 *
2365 * @param {number} high The high 32 bits.
2366 * @param {number} low The low 32 bits.
2367 *
2368 * @example
2369 *
2370 * var x64Word = CryptoJS.x64.Word.create(0x00010203, 0x04050607);
2371 */
2372 init: function (high, low) {
2373 this.high = high;
2374 this.low = low;
2375 }
2376
2377 /**
2378 * Bitwise NOTs this word.
2379 *
2380 * @return {X64Word} A new x64-Word object after negating.
2381 *
2382 * @example
2383 *
2384 * var negated = x64Word.not();
2385 */
2386 // not: function () {
2387 // var high = ~this.high;
2388 // var low = ~this.low;
2389
2390 // return X64Word.create(high, low);
2391 // },
2392
2393 /**
2394 * Bitwise ANDs this word with the passed word.
2395 *
2396 * @param {X64Word} word The x64-Word to AND with this word.
2397 *
2398 * @return {X64Word} A new x64-Word object after ANDing.
2399 *
2400 * @example
2401 *
2402 * var anded = x64Word.and(anotherX64Word);
2403 */
2404 // and: function (word) {
2405 // var high = this.high & word.high;
2406 // var low = this.low & word.low;
2407
2408 // return X64Word.create(high, low);
2409 // },
2410
2411 /**
2412 * Bitwise ORs this word with the passed word.
2413 *
2414 * @param {X64Word} word The x64-Word to OR with this word.
2415 *
2416 * @return {X64Word} A new x64-Word object after ORing.
2417 *
2418 * @example
2419 *
2420 * var ored = x64Word.or(anotherX64Word);
2421 */
2422 // or: function (word) {
2423 // var high = this.high | word.high;
2424 // var low = this.low | word.low;
2425
2426 // return X64Word.create(high, low);
2427 // },
2428
2429 /**
2430 * Bitwise XORs this word with the passed word.
2431 *
2432 * @param {X64Word} word The x64-Word to XOR with this word.
2433 *
2434 * @return {X64Word} A new x64-Word object after XORing.
2435 *
2436 * @example
2437 *
2438 * var xored = x64Word.xor(anotherX64Word);
2439 */
2440 // xor: function (word) {
2441 // var high = this.high ^ word.high;
2442 // var low = this.low ^ word.low;
2443
2444 // return X64Word.create(high, low);
2445 // },
2446
2447 /**
2448 * Shifts this word n bits to the left.
2449 *
2450 * @param {number} n The number of bits to shift.
2451 *
2452 * @return {X64Word} A new x64-Word object after shifting.
2453 *
2454 * @example
2455 *
2456 * var shifted = x64Word.shiftL(25);
2457 */
2458 // shiftL: function (n) {
2459 // if (n < 32) {
2460 // var high = (this.high << n) | (this.low >>> (32 - n));
2461 // var low = this.low << n;
2462 // } else {
2463 // var high = this.low << (n - 32);
2464 // var low = 0;
2465 // }
2466
2467 // return X64Word.create(high, low);
2468 // },
2469
2470 /**
2471 * Shifts this word n bits to the right.
2472 *
2473 * @param {number} n The number of bits to shift.
2474 *
2475 * @return {X64Word} A new x64-Word object after shifting.
2476 *
2477 * @example
2478 *
2479 * var shifted = x64Word.shiftR(7);
2480 */
2481 // shiftR: function (n) {
2482 // if (n < 32) {
2483 // var low = (this.low >>> n) | (this.high << (32 - n));
2484 // var high = this.high >>> n;
2485 // } else {
2486 // var low = this.high >>> (n - 32);
2487 // var high = 0;
2488 // }
2489
2490 // return X64Word.create(high, low);
2491 // },
2492
2493 /**
2494 * Rotates this word n bits to the left.
2495 *
2496 * @param {number} n The number of bits to rotate.
2497 *
2498 * @return {X64Word} A new x64-Word object after rotating.
2499 *
2500 * @example
2501 *
2502 * var rotated = x64Word.rotL(25);
2503 */
2504 // rotL: function (n) {
2505 // return this.shiftL(n).or(this.shiftR(64 - n));
2506 // },
2507
2508 /**
2509 * Rotates this word n bits to the right.
2510 *
2511 * @param {number} n The number of bits to rotate.
2512 *
2513 * @return {X64Word} A new x64-Word object after rotating.
2514 *
2515 * @example
2516 *
2517 * var rotated = x64Word.rotR(7);
2518 */
2519 // rotR: function (n) {
2520 // return this.shiftR(n).or(this.shiftL(64 - n));
2521 // },
2522
2523 /**
2524 * Adds this word with the passed word.
2525 *
2526 * @param {X64Word} word The x64-Word to add with this word.
2527 *
2528 * @return {X64Word} A new x64-Word object after adding.
2529 *
2530 * @example
2531 *
2532 * var added = x64Word.add(anotherX64Word);
2533 */
2534 // add: function (word) {
2535 // var low = (this.low + word.low) | 0;
2536 // var carry = (low >>> 0) < (this.low >>> 0) ? 1 : 0;
2537 // var high = (this.high + word.high + carry) | 0;
2538
2539 // return X64Word.create(high, low);
2540 // }
2541 });
2542
2543 /**
2544 * An array of 64-bit words.
2545 *
2546 * @property {Array} words The array of CryptoJS.x64.Word objects.
2547 * @property {number} sigBytes The number of significant bytes in this word array.
2548 */
2549 var X64WordArray = C_x64.WordArray = Base.extend({
2550 /**
2551 * Initializes a newly created word array.
2552 *
2553 * @param {Array} words (Optional) An array of CryptoJS.x64.Word objects.
2554 * @param {number} sigBytes (Optional) The number of significant bytes in the words.
2555 *
2556 * @example
2557 *
2558 * var wordArray = CryptoJS.x64.WordArray.create();
2559 *
2560 * var wordArray = CryptoJS.x64.WordArray.create([
2561 * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
2562 * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
2563 * ]);
2564 *
2565 * var wordArray = CryptoJS.x64.WordArray.create([
2566 * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
2567 * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
2568 * ], 10);
2569 */
2570 init: function (words, sigBytes) {
2571 words = this.words = words || [];
2572
2573 if (sigBytes != undefined) {
2574 this.sigBytes = sigBytes;
2575 } else {
2576 this.sigBytes = words.length * 8;
2577 }
2578 },
2579
2580 /**
2581 * Converts this 64-bit word array to a 32-bit word array.
2582 *
2583 * @return {CryptoJS.lib.WordArray} This word array's data as a 32-bit word array.
2584 *
2585 * @example
2586 *
2587 * var x32WordArray = x64WordArray.toX32();
2588 */
2589 toX32: function () {
2590 // Shortcuts
2591 var x64Words = this.words;
2592 var x64WordsLength = x64Words.length;
2593
2594 // Convert
2595 var x32Words = [];
2596 for (var i = 0; i < x64WordsLength; i++) {
2597 var x64Word = x64Words[i];
2598 x32Words.push(x64Word.high);
2599 x32Words.push(x64Word.low);
2600 }
2601
2602 return X32WordArray.create(x32Words, this.sigBytes);
2603 },
2604
2605 /**
2606 * Creates a copy of this word array.
2607 *
2608 * @return {X64WordArray} The clone.
2609 *
2610 * @example
2611 *
2612 * var clone = x64WordArray.clone();
2613 */
2614 clone: function () {
2615 var clone = Base.clone.call(this);
2616
2617 // Clone "words" array
2618 var words = clone.words = this.words.slice(0);
2619
2620 // Clone each X64Word object
2621 var wordsLength = words.length;
2622 for (var i = 0; i < wordsLength; i++) {
2623 words[i] = words[i].clone();
2624 }
2625
2626 return clone;
2627 }
2628 });
2629 }());
2630
2631
2632 (function (Math) {
2633 // Shortcuts
2634 var C = CryptoJS;
2635 var C_lib = C.lib;
2636 var WordArray = C_lib.WordArray;
2637 var Hasher = C_lib.Hasher;
2638 var C_x64 = C.x64;
2639 var X64Word = C_x64.Word;
2640 var C_algo = C.algo;
2641
2642 // Constants tables
2643 var RHO_OFFSETS = [];
2644 var PI_INDEXES = [];
2645 var ROUND_CONSTANTS = [];
2646
2647 // Compute Constants
2648 (function () {
2649 // Compute rho offset constants
2650 var x = 1, y = 0;
2651 for (var t = 0; t < 24; t++) {
2652 RHO_OFFSETS[x + 5 * y] = ((t + 1) * (t + 2) / 2) % 64;
2653
2654 var newX = y % 5;
2655 var newY = (2 * x + 3 * y) % 5;
2656 x = newX;
2657 y = newY;
2658 }
2659
2660 // Compute pi index constants
2661 for (var x = 0; x < 5; x++) {
2662 for (var y = 0; y < 5; y++) {
2663 PI_INDEXES[x + 5 * y] = y + ((2 * x + 3 * y) % 5) * 5;
2664 }
2665 }
2666
2667 // Compute round constants
2668 var LFSR = 0x01;
2669 for (var i = 0; i < 24; i++) {
2670 var roundConstantMsw = 0;
2671 var roundConstantLsw = 0;
2672
2673 for (var j = 0; j < 7; j++) {
2674 if (LFSR & 0x01) {
2675 var bitPosition = (1 << j) - 1;
2676 if (bitPosition < 32) {
2677 roundConstantLsw ^= 1 << bitPosition;
2678 } else /* if (bitPosition >= 32) */ {
2679 roundConstantMsw ^= 1 << (bitPosition - 32);
2680 }
2681 }
2682
2683 // Compute next LFSR
2684 if (LFSR & 0x80) {
2685 // Primitive polynomial over GF(2): x^8 + x^6 + x^5 + x^4 + 1
2686 LFSR = (LFSR << 1) ^ 0x71;
2687 } else {
2688 LFSR <<= 1;
2689 }
2690 }
2691
2692 ROUND_CONSTANTS[i] = X64Word.create(roundConstantMsw, roundConstantLsw);
2693 }
2694 }());
2695
2696 // Reusable objects for temporary values
2697 var T = [];
2698 (function () {
2699 for (var i = 0; i < 25; i++) {
2700 T[i] = X64Word.create();
2701 }
2702 }());
2703
2704 /**
2705 * SHA-3 hash algorithm.
2706 */
2707 var SHA3 = C_algo.SHA3 = Hasher.extend({
2708 /**
2709 * Configuration options.
2710 *
2711 * @property {number} outputLength
2712 * The desired number of bits in the output hash.
2713 * Only values permitted are: 224, 256, 384, 512.
2714 * Default: 512
2715 */
2716 cfg: Hasher.cfg.extend({
2717 outputLength: 512
2718 }),
2719
2720 _doReset: function () {
2721 var state = this._state = []
2722 for (var i = 0; i < 25; i++) {
2723 state[i] = new X64Word.init();
2724 }
2725
2726 this.blockSize = (1600 - 2 * this.cfg.outputLength) / 32;
2727 },
2728
2729 _doProcessBlock: function (M, offset) {
2730 // Shortcuts
2731 var state = this._state;
2732 var nBlockSizeLanes = this.blockSize / 2;
2733
2734 // Absorb
2735 for (var i = 0; i < nBlockSizeLanes; i++) {
2736 // Shortcuts
2737 var M2i = M[offset + 2 * i];
2738 var M2i1 = M[offset + 2 * i + 1];
2739
2740 // Swap endian
2741 M2i = (
2742 (((M2i << 8) | (M2i >>> 24)) & 0x00ff00ff) |
2743 (((M2i << 24) | (M2i >>> 8)) & 0xff00ff00)
2744 );
2745 M2i1 = (
2746 (((M2i1 << 8) | (M2i1 >>> 24)) & 0x00ff00ff) |
2747 (((M2i1 << 24) | (M2i1 >>> 8)) & 0xff00ff00)
2748 );
2749
2750 // Absorb message into state
2751 var lane = state[i];
2752 lane.high ^= M2i1;
2753 lane.low ^= M2i;
2754 }
2755
2756 // Rounds
2757 for (var round = 0; round < 24; round++) {
2758 // Theta
2759 for (var x = 0; x < 5; x++) {
2760 // Mix column lanes
2761 var tMsw = 0, tLsw = 0;
2762 for (var y = 0; y < 5; y++) {
2763 var lane = state[x + 5 * y];
2764 tMsw ^= lane.high;
2765 tLsw ^= lane.low;
2766 }
2767
2768 // Temporary values
2769 var Tx = T[x];
2770 Tx.high = tMsw;
2771 Tx.low = tLsw;
2772 }
2773 for (var x = 0; x < 5; x++) {
2774 // Shortcuts
2775 var Tx4 = T[(x + 4) % 5];
2776 var Tx1 = T[(x + 1) % 5];
2777 var Tx1Msw = Tx1.high;
2778 var Tx1Lsw = Tx1.low;
2779
2780 // Mix surrounding columns
2781 var tMsw = Tx4.high ^ ((Tx1Msw << 1) | (Tx1Lsw >>> 31));
2782 var tLsw = Tx4.low ^ ((Tx1Lsw << 1) | (Tx1Msw >>> 31));
2783 for (var y = 0; y < 5; y++) {
2784 var lane = state[x + 5 * y];
2785 lane.high ^= tMsw;
2786 lane.low ^= tLsw;
2787 }
2788 }
2789
2790 // Rho Pi
2791 for (var laneIndex = 1; laneIndex < 25; laneIndex++) {
2792 var tMsw;
2793 var tLsw;
2794
2795 // Shortcuts
2796 var lane = state[laneIndex];
2797 var laneMsw = lane.high;
2798 var laneLsw = lane.low;
2799 var rhoOffset = RHO_OFFSETS[laneIndex];
2800
2801 // Rotate lanes
2802 if (rhoOffset < 32) {
2803 tMsw = (laneMsw << rhoOffset) | (laneLsw >>> (32 - rhoOffset));
2804 tLsw = (laneLsw << rhoOffset) | (laneMsw >>> (32 - rhoOffset));
2805 } else /* if (rhoOffset >= 32) */ {
2806 tMsw = (laneLsw << (rhoOffset - 32)) | (laneMsw >>> (64 - rhoOffset));
2807 tLsw = (laneMsw << (rhoOffset - 32)) | (laneLsw >>> (64 - rhoOffset));
2808 }
2809
2810 // Transpose lanes
2811 var TPiLane = T[PI_INDEXES[laneIndex]];
2812 TPiLane.high = tMsw;
2813 TPiLane.low = tLsw;
2814 }
2815
2816 // Rho pi at x = y = 0
2817 var T0 = T[0];
2818 var state0 = state[0];
2819 T0.high = state0.high;
2820 T0.low = state0.low;
2821
2822 // Chi
2823 for (var x = 0; x < 5; x++) {
2824 for (var y = 0; y < 5; y++) {
2825 // Shortcuts
2826 var laneIndex = x + 5 * y;
2827 var lane = state[laneIndex];
2828 var TLane = T[laneIndex];
2829 var Tx1Lane = T[((x + 1) % 5) + 5 * y];
2830 var Tx2Lane = T[((x + 2) % 5) + 5 * y];
2831
2832 // Mix rows
2833 lane.high = TLane.high ^ (~Tx1Lane.high & Tx2Lane.high);
2834 lane.low = TLane.low ^ (~Tx1Lane.low & Tx2Lane.low);
2835 }
2836 }
2837
2838 // Iota
2839 var lane = state[0];
2840 var roundConstant = ROUND_CONSTANTS[round];
2841 lane.high ^= roundConstant.high;
2842 lane.low ^= roundConstant.low;
2843 }
2844 },
2845
2846 _doFinalize: function () {
2847 // Shortcuts
2848 var data = this._data;
2849 var dataWords = data.words;
2850 var nBitsTotal = this._nDataBytes * 8;
2851 var nBitsLeft = data.sigBytes * 8;
2852 var blockSizeBits = this.blockSize * 32;
2853
2854 // Add padding
2855 dataWords[nBitsLeft >>> 5] |= 0x1 << (24 - nBitsLeft % 32);
2856 dataWords[((Math.ceil((nBitsLeft + 1) / blockSizeBits) * blockSizeBits) >>> 5) - 1] |= 0x80;
2857 data.sigBytes = dataWords.length * 4;
2858
2859 // Hash final blocks
2860 this._process();
2861
2862 // Shortcuts
2863 var state = this._state;
2864 var outputLengthBytes = this.cfg.outputLength / 8;
2865 var outputLengthLanes = outputLengthBytes / 8;
2866
2867 // Squeeze
2868 var hashWords = [];
2869 for (var i = 0; i < outputLengthLanes; i++) {
2870 // Shortcuts
2871 var lane = state[i];
2872 var laneMsw = lane.high;
2873 var laneLsw = lane.low;
2874
2875 // Swap endian
2876 laneMsw = (
2877 (((laneMsw << 8) | (laneMsw >>> 24)) & 0x00ff00ff) |
2878 (((laneMsw << 24) | (laneMsw >>> 8)) & 0xff00ff00)
2879 );
2880 laneLsw = (
2881 (((laneLsw << 8) | (laneLsw >>> 24)) & 0x00ff00ff) |
2882 (((laneLsw << 24) | (laneLsw >>> 8)) & 0xff00ff00)
2883 );
2884
2885 // Squeeze state to retrieve hash
2886 hashWords.push(laneLsw);
2887 hashWords.push(laneMsw);
2888 }
2889
2890 // Return final computed hash
2891 return new WordArray.init(hashWords, outputLengthBytes);
2892 },
2893
2894 clone: function () {
2895 var clone = Hasher.clone.call(this);
2896
2897 var state = clone._state = this._state.slice(0);
2898 for (var i = 0; i < 25; i++) {
2899 state[i] = state[i].clone();
2900 }
2901
2902 return clone;
2903 }
2904 });
2905
2906 /**
2907 * Shortcut function to the hasher's object interface.
2908 *
2909 * @param {WordArray|string} message The message to hash.
2910 *
2911 * @return {WordArray} The hash.
2912 *
2913 * @static
2914 *
2915 * @example
2916 *
2917 * var hash = CryptoJS.SHA3('message');
2918 * var hash = CryptoJS.SHA3(wordArray);
2919 */
2920 C.SHA3 = Hasher._createHelper(SHA3);
2921
2922 /**
2923 * Shortcut function to the HMAC's object interface.
2924 *
2925 * @param {WordArray|string} message The message to hash.
2926 * @param {WordArray|string} key The secret key.
2927 *
2928 * @return {WordArray} The HMAC.
2929 *
2930 * @static
2931 *
2932 * @example
2933 *
2934 * var hmac = CryptoJS.HmacSHA3(message, key);
2935 */
2936 C.HmacSHA3 = Hasher._createHmacHelper(SHA3);
2937 }(Math));
2938
2939
2940 (function () {
2941 // Shortcuts
2942 var C = CryptoJS;
2943 var C_lib = C.lib;
2944 var Hasher = C_lib.Hasher;
2945 var C_x64 = C.x64;
2946 var X64Word = C_x64.Word;
2947 var X64WordArray = C_x64.WordArray;
2948 var C_algo = C.algo;
2949
2950 function X64Word_create() {
2951 return X64Word.create.apply(X64Word, arguments);
2952 }
2953
2954 // Constants
2955 var K = [
2956 X64Word_create(0x428a2f98, 0xd728ae22), X64Word_create(0x71374491, 0x23ef65cd),
2957 X64Word_create(0xb5c0fbcf, 0xec4d3b2f), X64Word_create(0xe9b5dba5, 0x8189dbbc),
2958 X64Word_create(0x3956c25b, 0xf348b538), X64Word_create(0x59f111f1, 0xb605d019),
2959 X64Word_create(0x923f82a4, 0xaf194f9b), X64Word_create(0xab1c5ed5, 0xda6d8118),
2960 X64Word_create(0xd807aa98, 0xa3030242), X64Word_create(0x12835b01, 0x45706fbe),
2961 X64Word_create(0x243185be, 0x4ee4b28c), X64Word_create(0x550c7dc3, 0xd5ffb4e2),
2962 X64Word_create(0x72be5d74, 0xf27b896f), X64Word_create(0x80deb1fe, 0x3b1696b1),
2963 X64Word_create(0x9bdc06a7, 0x25c71235), X64Word_create(0xc19bf174, 0xcf692694),
2964 X64Word_create(0xe49b69c1, 0x9ef14ad2), X64Word_create(0xefbe4786, 0x384f25e3),
2965 X64Word_create(0x0fc19dc6, 0x8b8cd5b5), X64Word_create(0x240ca1cc, 0x77ac9c65),
2966 X64Word_create(0x2de92c6f, 0x592b0275), X64Word_create(0x4a7484aa, 0x6ea6e483),
2967 X64Word_create(0x5cb0a9dc, 0xbd41fbd4), X64Word_create(0x76f988da, 0x831153b5),
2968 X64Word_create(0x983e5152, 0xee66dfab), X64Word_create(0xa831c66d, 0x2db43210),
2969 X64Word_create(0xb00327c8, 0x98fb213f), X64Word_create(0xbf597fc7, 0xbeef0ee4),
2970 X64Word_create(0xc6e00bf3, 0x3da88fc2), X64Word_create(0xd5a79147, 0x930aa725),
2971 X64Word_create(0x06ca6351, 0xe003826f), X64Word_create(0x14292967, 0x0a0e6e70),
2972 X64Word_create(0x27b70a85, 0x46d22ffc), X64Word_create(0x2e1b2138, 0x5c26c926),
2973 X64Word_create(0x4d2c6dfc, 0x5ac42aed), X64Word_create(0x53380d13, 0x9d95b3df),
2974 X64Word_create(0x650a7354, 0x8baf63de), X64Word_create(0x766a0abb, 0x3c77b2a8),
2975 X64Word_create(0x81c2c92e, 0x47edaee6), X64Word_create(0x92722c85, 0x1482353b),
2976 X64Word_create(0xa2bfe8a1, 0x4cf10364), X64Word_create(0xa81a664b, 0xbc423001),
2977 X64Word_create(0xc24b8b70, 0xd0f89791), X64Word_create(0xc76c51a3, 0x0654be30),
2978 X64Word_create(0xd192e819, 0xd6ef5218), X64Word_create(0xd6990624, 0x5565a910),
2979 X64Word_create(0xf40e3585, 0x5771202a), X64Word_create(0x106aa070, 0x32bbd1b8),
2980 X64Word_create(0x19a4c116, 0xb8d2d0c8), X64Word_create(0x1e376c08, 0x5141ab53),
2981 X64Word_create(0x2748774c, 0xdf8eeb99), X64Word_create(0x34b0bcb5, 0xe19b48a8),
2982 X64Word_create(0x391c0cb3, 0xc5c95a63), X64Word_create(0x4ed8aa4a, 0xe3418acb),
2983 X64Word_create(0x5b9cca4f, 0x7763e373), X64Word_create(0x682e6ff3, 0xd6b2b8a3),
2984 X64Word_create(0x748f82ee, 0x5defb2fc), X64Word_create(0x78a5636f, 0x43172f60),
2985 X64Word_create(0x84c87814, 0xa1f0ab72), X64Word_create(0x8cc70208, 0x1a6439ec),
2986 X64Word_create(0x90befffa, 0x23631e28), X64Word_create(0xa4506ceb, 0xde82bde9),
2987 X64Word_create(0xbef9a3f7, 0xb2c67915), X64Word_create(0xc67178f2, 0xe372532b),
2988 X64Word_create(0xca273ece, 0xea26619c), X64Word_create(0xd186b8c7, 0x21c0c207),
2989 X64Word_create(0xeada7dd6, 0xcde0eb1e), X64Word_create(0xf57d4f7f, 0xee6ed178),
2990 X64Word_create(0x06f067aa, 0x72176fba), X64Word_create(0x0a637dc5, 0xa2c898a6),
2991 X64Word_create(0x113f9804, 0xbef90dae), X64Word_create(0x1b710b35, 0x131c471b),
2992 X64Word_create(0x28db77f5, 0x23047d84), X64Word_create(0x32caab7b, 0x40c72493),
2993 X64Word_create(0x3c9ebe0a, 0x15c9bebc), X64Word_create(0x431d67c4, 0x9c100d4c),
2994 X64Word_create(0x4cc5d4be, 0xcb3e42b6), X64Word_create(0x597f299c, 0xfc657e2a),
2995 X64Word_create(0x5fcb6fab, 0x3ad6faec), X64Word_create(0x6c44198c, 0x4a475817)
2996 ];
2997
2998 // Reusable objects
2999 var W = [];
3000 (function () {
3001 for (var i = 0; i < 80; i++) {
3002 W[i] = X64Word_create();
3003 }
3004 }());
3005
3006 /**
3007 * SHA-512 hash algorithm.
3008 */
3009 var SHA512 = C_algo.SHA512 = Hasher.extend({
3010 _doReset: function () {
3011 this._hash = new X64WordArray.init([
3012 new X64Word.init(0x6a09e667, 0xf3bcc908), new X64Word.init(0xbb67ae85, 0x84caa73b),
3013 new X64Word.init(0x3c6ef372, 0xfe94f82b), new X64Word.init(0xa54ff53a, 0x5f1d36f1),
3014 new X64Word.init(0x510e527f, 0xade682d1), new X64Word.init(0x9b05688c, 0x2b3e6c1f),
3015 new X64Word.init(0x1f83d9ab, 0xfb41bd6b), new X64Word.init(0x5be0cd19, 0x137e2179)
3016 ]);
3017 },
3018
3019 _doProcessBlock: function (M, offset) {
3020 // Shortcuts
3021 var H = this._hash.words;
3022
3023 var H0 = H[0];
3024 var H1 = H[1];
3025 var H2 = H[2];
3026 var H3 = H[3];
3027 var H4 = H[4];
3028 var H5 = H[5];
3029 var H6 = H[6];
3030 var H7 = H[7];
3031
3032 var H0h = H0.high;
3033 var H0l = H0.low;
3034 var H1h = H1.high;
3035 var H1l = H1.low;
3036 var H2h = H2.high;
3037 var H2l = H2.low;
3038 var H3h = H3.high;
3039 var H3l = H3.low;
3040 var H4h = H4.high;
3041 var H4l = H4.low;
3042 var H5h = H5.high;
3043 var H5l = H5.low;
3044 var H6h = H6.high;
3045 var H6l = H6.low;
3046 var H7h = H7.high;
3047 var H7l = H7.low;
3048
3049 // Working variables
3050 var ah = H0h;
3051 var al = H0l;
3052 var bh = H1h;
3053 var bl = H1l;
3054 var ch = H2h;
3055 var cl = H2l;
3056 var dh = H3h;
3057 var dl = H3l;
3058 var eh = H4h;
3059 var el = H4l;
3060 var fh = H5h;
3061 var fl = H5l;
3062 var gh = H6h;
3063 var gl = H6l;
3064 var hh = H7h;
3065 var hl = H7l;
3066
3067 // Rounds
3068 for (var i = 0; i < 80; i++) {
3069 var Wil;
3070 var Wih;
3071
3072 // Shortcut
3073 var Wi = W[i];
3074
3075 // Extend message
3076 if (i < 16) {
3077 Wih = Wi.high = M[offset + i * 2] | 0;
3078 Wil = Wi.low = M[offset + i * 2 + 1] | 0;
3079 } else {
3080 // Gamma0
3081 var gamma0x = W[i - 15];
3082 var gamma0xh = gamma0x.high;
3083 var gamma0xl = gamma0x.low;
3084 var gamma0h = ((gamma0xh >>> 1) | (gamma0xl << 31)) ^ ((gamma0xh >>> 8) | (gamma0xl << 24)) ^ (gamma0xh >>> 7);
3085 var gamma0l = ((gamma0xl >>> 1) | (gamma0xh << 31)) ^ ((gamma0xl >>> 8) | (gamma0xh << 24)) ^ ((gamma0xl >>> 7) | (gamma0xh << 25));
3086
3087 // Gamma1
3088 var gamma1x = W[i - 2];
3089 var gamma1xh = gamma1x.high;
3090 var gamma1xl = gamma1x.low;
3091 var gamma1h = ((gamma1xh >>> 19) | (gamma1xl << 13)) ^ ((gamma1xh << 3) | (gamma1xl >>> 29)) ^ (gamma1xh >>> 6);
3092 var gamma1l = ((gamma1xl >>> 19) | (gamma1xh << 13)) ^ ((gamma1xl << 3) | (gamma1xh >>> 29)) ^ ((gamma1xl >>> 6) | (gamma1xh << 26));
3093
3094 // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
3095 var Wi7 = W[i - 7];
3096 var Wi7h = Wi7.high;
3097 var Wi7l = Wi7.low;
3098
3099 var Wi16 = W[i - 16];
3100 var Wi16h = Wi16.high;
3101 var Wi16l = Wi16.low;
3102
3103 Wil = gamma0l + Wi7l;
3104 Wih = gamma0h + Wi7h + ((Wil >>> 0) < (gamma0l >>> 0) ? 1 : 0);
3105 Wil = Wil + gamma1l;
3106 Wih = Wih + gamma1h + ((Wil >>> 0) < (gamma1l >>> 0) ? 1 : 0);
3107 Wil = Wil + Wi16l;
3108 Wih = Wih + Wi16h + ((Wil >>> 0) < (Wi16l >>> 0) ? 1 : 0);
3109
3110 Wi.high = Wih;
3111 Wi.low = Wil;
3112 }
3113
3114 var chh = (eh & fh) ^ (~eh & gh);
3115 var chl = (el & fl) ^ (~el & gl);
3116 var majh = (ah & bh) ^ (ah & ch) ^ (bh & ch);
3117 var majl = (al & bl) ^ (al & cl) ^ (bl & cl);
3118
3119 var sigma0h = ((ah >>> 28) | (al << 4)) ^ ((ah << 30) | (al >>> 2)) ^ ((ah << 25) | (al >>> 7));
3120 var sigma0l = ((al >>> 28) | (ah << 4)) ^ ((al << 30) | (ah >>> 2)) ^ ((al << 25) | (ah >>> 7));
3121 var sigma1h = ((eh >>> 14) | (el << 18)) ^ ((eh >>> 18) | (el << 14)) ^ ((eh << 23) | (el >>> 9));
3122 var sigma1l = ((el >>> 14) | (eh << 18)) ^ ((el >>> 18) | (eh << 14)) ^ ((el << 23) | (eh >>> 9));
3123
3124 // t1 = h + sigma1 + ch + K[i] + W[i]
3125 var Ki = K[i];
3126 var Kih = Ki.high;
3127 var Kil = Ki.low;
3128
3129 var t1l = hl + sigma1l;
3130 var t1h = hh + sigma1h + ((t1l >>> 0) < (hl >>> 0) ? 1 : 0);
3131 var t1l = t1l + chl;
3132 var t1h = t1h + chh + ((t1l >>> 0) < (chl >>> 0) ? 1 : 0);
3133 var t1l = t1l + Kil;
3134 var t1h = t1h + Kih + ((t1l >>> 0) < (Kil >>> 0) ? 1 : 0);
3135 var t1l = t1l + Wil;
3136 var t1h = t1h + Wih + ((t1l >>> 0) < (Wil >>> 0) ? 1 : 0);
3137
3138 // t2 = sigma0 + maj
3139 var t2l = sigma0l + majl;
3140 var t2h = sigma0h + majh + ((t2l >>> 0) < (sigma0l >>> 0) ? 1 : 0);
3141
3142 // Update working variables
3143 hh = gh;
3144 hl = gl;
3145 gh = fh;
3146 gl = fl;
3147 fh = eh;
3148 fl = el;
3149 el = (dl + t1l) | 0;
3150 eh = (dh + t1h + ((el >>> 0) < (dl >>> 0) ? 1 : 0)) | 0;
3151 dh = ch;
3152 dl = cl;
3153 ch = bh;
3154 cl = bl;
3155 bh = ah;
3156 bl = al;
3157 al = (t1l + t2l) | 0;
3158 ah = (t1h + t2h + ((al >>> 0) < (t1l >>> 0) ? 1 : 0)) | 0;
3159 }
3160
3161 // Intermediate hash value
3162 H0l = H0.low = (H0l + al);
3163 H0.high = (H0h + ah + ((H0l >>> 0) < (al >>> 0) ? 1 : 0));
3164 H1l = H1.low = (H1l + bl);
3165 H1.high = (H1h + bh + ((H1l >>> 0) < (bl >>> 0) ? 1 : 0));
3166 H2l = H2.low = (H2l + cl);
3167 H2.high = (H2h + ch + ((H2l >>> 0) < (cl >>> 0) ? 1 : 0));
3168 H3l = H3.low = (H3l + dl);
3169 H3.high = (H3h + dh + ((H3l >>> 0) < (dl >>> 0) ? 1 : 0));
3170 H4l = H4.low = (H4l + el);
3171 H4.high = (H4h + eh + ((H4l >>> 0) < (el >>> 0) ? 1 : 0));
3172 H5l = H5.low = (H5l + fl);
3173 H5.high = (H5h + fh + ((H5l >>> 0) < (fl >>> 0) ? 1 : 0));
3174 H6l = H6.low = (H6l + gl);
3175 H6.high = (H6h + gh + ((H6l >>> 0) < (gl >>> 0) ? 1 : 0));
3176 H7l = H7.low = (H7l + hl);
3177 H7.high = (H7h + hh + ((H7l >>> 0) < (hl >>> 0) ? 1 : 0));
3178 },
3179
3180 _doFinalize: function () {
3181 // Shortcuts
3182 var data = this._data;
3183 var dataWords = data.words;
3184
3185 var nBitsTotal = this._nDataBytes * 8;
3186 var nBitsLeft = data.sigBytes * 8;
3187
3188 // Add padding
3189 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
3190 dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 30] = Math.floor(nBitsTotal / 0x100000000);
3191 dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 31] = nBitsTotal;
3192 data.sigBytes = dataWords.length * 4;
3193
3194 // Hash final blocks
3195 this._process();
3196
3197 // Convert hash to 32-bit word array before returning
3198 var hash = this._hash.toX32();
3199
3200 // Return final computed hash
3201 return hash;
3202 },
3203
3204 clone: function () {
3205 var clone = Hasher.clone.call(this);
3206 clone._hash = this._hash.clone();
3207
3208 return clone;
3209 },
3210
3211 blockSize: 1024/32
3212 });
3213
3214 /**
3215 * Shortcut function to the hasher's object interface.
3216 *
3217 * @param {WordArray|string} message The message to hash.
3218 *
3219 * @return {WordArray} The hash.
3220 *
3221 * @static
3222 *
3223 * @example
3224 *
3225 * var hash = CryptoJS.SHA512('message');
3226 * var hash = CryptoJS.SHA512(wordArray);
3227 */
3228 C.SHA512 = Hasher._createHelper(SHA512);
3229
3230 /**
3231 * Shortcut function to the HMAC's object interface.
3232 *
3233 * @param {WordArray|string} message The message to hash.
3234 * @param {WordArray|string} key The secret key.
3235 *
3236 * @return {WordArray} The HMAC.
3237 *
3238 * @static
3239 *
3240 * @example
3241 *
3242 * var hmac = CryptoJS.HmacSHA512(message, key);
3243 */
3244 C.HmacSHA512 = Hasher._createHmacHelper(SHA512);
3245 }());
3246
3247
3248 (function () {
3249 // Shortcuts
3250 var C = CryptoJS;
3251 var C_x64 = C.x64;
3252 var X64Word = C_x64.Word;
3253 var X64WordArray = C_x64.WordArray;
3254 var C_algo = C.algo;
3255 var SHA512 = C_algo.SHA512;
3256
3257 /**
3258 * SHA-384 hash algorithm.
3259 */
3260 var SHA384 = C_algo.SHA384 = SHA512.extend({
3261 _doReset: function () {
3262 this._hash = new X64WordArray.init([
3263 new X64Word.init(0xcbbb9d5d, 0xc1059ed8), new X64Word.init(0x629a292a, 0x367cd507),
3264 new X64Word.init(0x9159015a, 0x3070dd17), new X64Word.init(0x152fecd8, 0xf70e5939),
3265 new X64Word.init(0x67332667, 0xffc00b31), new X64Word.init(0x8eb44a87, 0x68581511),
3266 new X64Word.init(0xdb0c2e0d, 0x64f98fa7), new X64Word.init(0x47b5481d, 0xbefa4fa4)
3267 ]);
3268 },
3269
3270 _doFinalize: function () {
3271 var hash = SHA512._doFinalize.call(this);
3272
3273 hash.sigBytes -= 16;
3274
3275 return hash;
3276 }
3277 });
3278
3279 /**
3280 * Shortcut function to the hasher's object interface.
3281 *
3282 * @param {WordArray|string} message The message to hash.
3283 *
3284 * @return {WordArray} The hash.
3285 *
3286 * @static
3287 *
3288 * @example
3289 *
3290 * var hash = CryptoJS.SHA384('message');
3291 * var hash = CryptoJS.SHA384(wordArray);
3292 */
3293 C.SHA384 = SHA512._createHelper(SHA384);
3294
3295 /**
3296 * Shortcut function to the HMAC's object interface.
3297 *
3298 * @param {WordArray|string} message The message to hash.
3299 * @param {WordArray|string} key The secret key.
3300 *
3301 * @return {WordArray} The HMAC.
3302 *
3303 * @static
3304 *
3305 * @example
3306 *
3307 * var hmac = CryptoJS.HmacSHA384(message, key);
3308 */
3309 C.HmacSHA384 = SHA512._createHmacHelper(SHA384);
3310 }());
3311
3312
3313 /**
3314 * Cipher core components.
3315 */
3316 CryptoJS.lib.Cipher || (function (undefined) {
3317 // Shortcuts
3318 var C = CryptoJS;
3319 var C_lib = C.lib;
3320 var Base = C_lib.Base;
3321 var WordArray = C_lib.WordArray;
3322 var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm;
3323 var C_enc = C.enc;
3324 var Utf8 = C_enc.Utf8;
3325 var Base64 = C_enc.Base64;
3326 var C_algo = C.algo;
3327 var EvpKDF = C_algo.EvpKDF;
3328
3329 /**
3330 * Abstract base cipher template.
3331 *
3332 * @property {number} keySize This cipher's key size. Default: 4 (128 bits)
3333 * @property {number} ivSize This cipher's IV size. Default: 4 (128 bits)
3334 * @property {number} _ENC_XFORM_MODE A constant representing encryption mode.
3335 * @property {number} _DEC_XFORM_MODE A constant representing decryption mode.
3336 */
3337 var Cipher = C_lib.Cipher = BufferedBlockAlgorithm.extend({
3338 /**
3339 * Configuration options.
3340 *
3341 * @property {WordArray} iv The IV to use for this operation.
3342 */
3343 cfg: Base.extend(),
3344
3345 /**
3346 * Creates this cipher in encryption mode.
3347 *
3348 * @param {WordArray} key The key.
3349 * @param {Object} cfg (Optional) The configuration options to use for this operation.
3350 *
3351 * @return {Cipher} A cipher instance.
3352 *
3353 * @static
3354 *
3355 * @example
3356 *
3357 * var cipher = CryptoJS.algo.AES.createEncryptor(keyWordArray, { iv: ivWordArray });
3358 */
3359 createEncryptor: function (key, cfg) {
3360 return this.create(this._ENC_XFORM_MODE, key, cfg);
3361 },
3362
3363 /**
3364 * Creates this cipher in decryption mode.
3365 *
3366 * @param {WordArray} key The key.
3367 * @param {Object} cfg (Optional) The configuration options to use for this operation.
3368 *
3369 * @return {Cipher} A cipher instance.
3370 *
3371 * @static
3372 *
3373 * @example
3374 *
3375 * var cipher = CryptoJS.algo.AES.createDecryptor(keyWordArray, { iv: ivWordArray });
3376 */
3377 createDecryptor: function (key, cfg) {
3378 return this.create(this._DEC_XFORM_MODE, key, cfg);
3379 },
3380
3381 /**
3382 * Initializes a newly created cipher.
3383 *
3384 * @param {number} xformMode Either the encryption or decryption transormation mode constant.
3385 * @param {WordArray} key The key.
3386 * @param {Object} cfg (Optional) The configuration options to use for this operation.
3387 *
3388 * @example
3389 *
3390 * var cipher = CryptoJS.algo.AES.create(CryptoJS.algo.AES._ENC_XFORM_MODE, keyWordArray, { iv: ivWordArray });
3391 */
3392 init: function (xformMode, key, cfg) {
3393 // Apply config defaults
3394 this.cfg = this.cfg.extend(cfg);
3395
3396 // Store transform mode and key
3397 this._xformMode = xformMode;
3398 this._key = key;
3399
3400 // Set initial values
3401 this.reset();
3402 },
3403
3404 /**
3405 * Resets this cipher to its initial state.
3406 *
3407 * @example
3408 *
3409 * cipher.reset();
3410 */
3411 reset: function () {
3412 // Reset data buffer
3413 BufferedBlockAlgorithm.reset.call(this);
3414
3415 // Perform concrete-cipher logic
3416 this._doReset();
3417 },
3418
3419 /**
3420 * Adds data to be encrypted or decrypted.
3421 *
3422 * @param {WordArray|string} dataUpdate The data to encrypt or decrypt.
3423 *
3424 * @return {WordArray} The data after processing.
3425 *
3426 * @example
3427 *
3428 * var encrypted = cipher.process('data');
3429 * var encrypted = cipher.process(wordArray);
3430 */
3431 process: function (dataUpdate) {
3432 // Append
3433 this._append(dataUpdate);
3434
3435 // Process available blocks
3436 return this._process();
3437 },
3438
3439 /**
3440 * Finalizes the encryption or decryption process.
3441 * Note that the finalize operation is effectively a destructive, read-once operation.
3442 *
3443 * @param {WordArray|string} dataUpdate The final data to encrypt or decrypt.
3444 *
3445 * @return {WordArray} The data after final processing.
3446 *
3447 * @example
3448 *
3449 * var encrypted = cipher.finalize();
3450 * var encrypted = cipher.finalize('data');
3451 * var encrypted = cipher.finalize(wordArray);
3452 */
3453 finalize: function (dataUpdate) {
3454 // Final data update
3455 if (dataUpdate) {
3456 this._append(dataUpdate);
3457 }
3458
3459 // Perform concrete-cipher logic
3460 var finalProcessedData = this._doFinalize();
3461
3462 return finalProcessedData;
3463 },
3464
3465 keySize: 128/32,
3466
3467 ivSize: 128/32,
3468
3469 _ENC_XFORM_MODE: 1,
3470
3471 _DEC_XFORM_MODE: 2,
3472
3473 /**
3474 * Creates shortcut functions to a cipher's object interface.
3475 *
3476 * @param {Cipher} cipher The cipher to create a helper for.
3477 *
3478 * @return {Object} An object with encrypt and decrypt shortcut functions.
3479 *
3480 * @static
3481 *
3482 * @example
3483 *
3484 * var AES = CryptoJS.lib.Cipher._createHelper(CryptoJS.algo.AES);
3485 */
3486 _createHelper: (function () {
3487 function selectCipherStrategy(key) {
3488 if (typeof key == 'string') {
3489 return PasswordBasedCipher;
3490 } else {
3491 return SerializableCipher;
3492 }
3493 }
3494
3495 return function (cipher) {
3496 return {
3497 encrypt: function (message, key, cfg) {
3498 return selectCipherStrategy(key).encrypt(cipher, message, key, cfg);
3499 },
3500
3501 decrypt: function (ciphertext, key, cfg) {
3502 return selectCipherStrategy(key).decrypt(cipher, ciphertext, key, cfg);
3503 }
3504 };
3505 };
3506 }())
3507 });
3508
3509 /**
3510 * Abstract base stream cipher template.
3511 *
3512 * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 1 (32 bits)
3513 */
3514 var StreamCipher = C_lib.StreamCipher = Cipher.extend({
3515 _doFinalize: function () {
3516 // Process partial blocks
3517 var finalProcessedBlocks = this._process(!!'flush');
3518
3519 return finalProcessedBlocks;
3520 },
3521
3522 blockSize: 1
3523 });
3524
3525 /**
3526 * Mode namespace.
3527 */
3528 var C_mode = C.mode = {};
3529
3530 /**
3531 * Abstract base block cipher mode template.
3532 */
3533 var BlockCipherMode = C_lib.BlockCipherMode = Base.extend({
3534 /**
3535 * Creates this mode for encryption.
3536 *
3537 * @param {Cipher} cipher A block cipher instance.
3538 * @param {Array} iv The IV words.
3539 *
3540 * @static
3541 *
3542 * @example
3543 *
3544 * var mode = CryptoJS.mode.CBC.createEncryptor(cipher, iv.words);
3545 */
3546 createEncryptor: function (cipher, iv) {
3547 return this.Encryptor.create(cipher, iv);
3548 },
3549
3550 /**
3551 * Creates this mode for decryption.
3552 *
3553 * @param {Cipher} cipher A block cipher instance.
3554 * @param {Array} iv The IV words.
3555 *
3556 * @static
3557 *
3558 * @example
3559 *
3560 * var mode = CryptoJS.mode.CBC.createDecryptor(cipher, iv.words);
3561 */
3562 createDecryptor: function (cipher, iv) {
3563 return this.Decryptor.create(cipher, iv);
3564 },
3565
3566 /**
3567 * Initializes a newly created mode.
3568 *
3569 * @param {Cipher} cipher A block cipher instance.
3570 * @param {Array} iv The IV words.
3571 *
3572 * @example
3573 *
3574 * var mode = CryptoJS.mode.CBC.Encryptor.create(cipher, iv.words);
3575 */
3576 init: function (cipher, iv) {
3577 this._cipher = cipher;
3578 this._iv = iv;
3579 }
3580 });
3581
3582 /**
3583 * Cipher Block Chaining mode.
3584 */
3585 var CBC = C_mode.CBC = (function () {
3586 /**
3587 * Abstract base CBC mode.
3588 */
3589 var CBC = BlockCipherMode.extend();
3590
3591 /**
3592 * CBC encryptor.
3593 */
3594 CBC.Encryptor = CBC.extend({
3595 /**
3596 * Processes the data block at offset.
3597 *
3598 * @param {Array} words The data words to operate on.
3599 * @param {number} offset The offset where the block starts.
3600 *
3601 * @example
3602 *
3603 * mode.processBlock(data.words, offset);
3604 */
3605 processBlock: function (words, offset) {
3606 // Shortcuts
3607 var cipher = this._cipher;
3608 var blockSize = cipher.blockSize;
3609
3610 // XOR and encrypt
3611 xorBlock.call(this, words, offset, blockSize);
3612 cipher.encryptBlock(words, offset);
3613
3614 // Remember this block to use with next block
3615 this._prevBlock = words.slice(offset, offset + blockSize);
3616 }
3617 });
3618
3619 /**
3620 * CBC decryptor.
3621 */
3622 CBC.Decryptor = CBC.extend({
3623 /**
3624 * Processes the data block at offset.
3625 *
3626 * @param {Array} words The data words to operate on.
3627 * @param {number} offset The offset where the block starts.
3628 *
3629 * @example
3630 *
3631 * mode.processBlock(data.words, offset);
3632 */
3633 processBlock: function (words, offset) {
3634 // Shortcuts
3635 var cipher = this._cipher;
3636 var blockSize = cipher.blockSize;
3637
3638 // Remember this block to use with next block
3639 var thisBlock = words.slice(offset, offset + blockSize);
3640
3641 // Decrypt and XOR
3642 cipher.decryptBlock(words, offset);
3643 xorBlock.call(this, words, offset, blockSize);
3644
3645 // This block becomes the previous block
3646 this._prevBlock = thisBlock;
3647 }
3648 });
3649
3650 function xorBlock(words, offset, blockSize) {
3651 var block;
3652
3653 // Shortcut
3654 var iv = this._iv;
3655
3656 // Choose mixing block
3657 if (iv) {
3658 block = iv;
3659
3660 // Remove IV for subsequent blocks
3661 this._iv = undefined;
3662 } else {
3663 block = this._prevBlock;
3664 }
3665
3666 // XOR blocks
3667 for (var i = 0; i < blockSize; i++) {
3668 words[offset + i] ^= block[i];
3669 }
3670 }
3671
3672 return CBC;
3673 }());
3674
3675 /**
3676 * Padding namespace.
3677 */
3678 var C_pad = C.pad = {};
3679
3680 /**
3681 * PKCS #5/7 padding strategy.
3682 */
3683 var Pkcs7 = C_pad.Pkcs7 = {
3684 /**
3685 * Pads data using the algorithm defined in PKCS #5/7.
3686 *
3687 * @param {WordArray} data The data to pad.
3688 * @param {number} blockSize The multiple that the data should be padded to.
3689 *
3690 * @static
3691 *
3692 * @example
3693 *
3694 * CryptoJS.pad.Pkcs7.pad(wordArray, 4);
3695 */
3696 pad: function (data, blockSize) {
3697 // Shortcut
3698 var blockSizeBytes = blockSize * 4;
3699
3700 // Count padding bytes
3701 var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
3702
3703 // Create padding word
3704 var paddingWord = (nPaddingBytes << 24) | (nPaddingBytes << 16) | (nPaddingBytes << 8) | nPaddingBytes;
3705
3706 // Create padding
3707 var paddingWords = [];
3708 for (var i = 0; i < nPaddingBytes; i += 4) {
3709 paddingWords.push(paddingWord);
3710 }
3711 var padding = WordArray.create(paddingWords, nPaddingBytes);
3712
3713 // Add padding
3714 data.concat(padding);
3715 },
3716
3717 /**
3718 * Unpads data that had been padded using the algorithm defined in PKCS #5/7.
3719 *
3720 * @param {WordArray} data The data to unpad.
3721 *
3722 * @static
3723 *
3724 * @example
3725 *
3726 * CryptoJS.pad.Pkcs7.unpad(wordArray);
3727 */
3728 unpad: function (data) {
3729 // Get number of padding bytes from last byte
3730 var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
3731
3732 // Remove padding
3733 data.sigBytes -= nPaddingBytes;
3734 }
3735 };
3736
3737 /**
3738 * Abstract base block cipher template.
3739 *
3740 * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 4 (128 bits)
3741 */
3742 var BlockCipher = C_lib.BlockCipher = Cipher.extend({
3743 /**
3744 * Configuration options.
3745 *
3746 * @property {Mode} mode The block mode to use. Default: CBC
3747 * @property {Padding} padding The padding strategy to use. Default: Pkcs7
3748 */
3749 cfg: Cipher.cfg.extend({
3750 mode: CBC,
3751 padding: Pkcs7
3752 }),
3753
3754 reset: function () {
3755 var modeCreator;
3756
3757 // Reset cipher
3758 Cipher.reset.call(this);
3759
3760 // Shortcuts
3761 var cfg = this.cfg;
3762 var iv = cfg.iv;
3763 var mode = cfg.mode;
3764
3765 // Reset block mode
3766 if (this._xformMode == this._ENC_XFORM_MODE) {
3767 modeCreator = mode.createEncryptor;
3768 } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
3769 modeCreator = mode.createDecryptor;
3770 // Keep at least one block in the buffer for unpadding
3771 this._minBufferSize = 1;
3772 }
3773
3774 if (this._mode && this._mode.__creator == modeCreator) {
3775 this._mode.init(this, iv && iv.words);
3776 } else {
3777 this._mode = modeCreator.call(mode, this, iv && iv.words);
3778 this._mode.__creator = modeCreator;
3779 }
3780 },
3781
3782 _doProcessBlock: function (words, offset) {
3783 this._mode.processBlock(words, offset);
3784 },
3785
3786 _doFinalize: function () {
3787 var finalProcessedBlocks;
3788
3789 // Shortcut
3790 var padding = this.cfg.padding;
3791
3792 // Finalize
3793 if (this._xformMode == this._ENC_XFORM_MODE) {
3794 // Pad data
3795 padding.pad(this._data, this.blockSize);
3796
3797 // Process final blocks
3798 finalProcessedBlocks = this._process(!!'flush');
3799 } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
3800 // Process final blocks
3801 finalProcessedBlocks = this._process(!!'flush');
3802
3803 // Unpad data
3804 padding.unpad(finalProcessedBlocks);
3805 }
3806
3807 return finalProcessedBlocks;
3808 },
3809
3810 blockSize: 128/32
3811 });
3812
3813 /**
3814 * A collection of cipher parameters.
3815 *
3816 * @property {WordArray} ciphertext The raw ciphertext.
3817 * @property {WordArray} key The key to this ciphertext.
3818 * @property {WordArray} iv The IV used in the ciphering operation.
3819 * @property {WordArray} salt The salt used with a key derivation function.
3820 * @property {Cipher} algorithm The cipher algorithm.
3821 * @property {Mode} mode The block mode used in the ciphering operation.
3822 * @property {Padding} padding The padding scheme used in the ciphering operation.
3823 * @property {number} blockSize The block size of the cipher.
3824 * @property {Format} formatter The default formatting strategy to convert this cipher params object to a string.
3825 */
3826 var CipherParams = C_lib.CipherParams = Base.extend({
3827 /**
3828 * Initializes a newly created cipher params object.
3829 *
3830 * @param {Object} cipherParams An object with any of the possible cipher parameters.
3831 *
3832 * @example
3833 *
3834 * var cipherParams = CryptoJS.lib.CipherParams.create({
3835 * ciphertext: ciphertextWordArray,
3836 * key: keyWordArray,
3837 * iv: ivWordArray,
3838 * salt: saltWordArray,
3839 * algorithm: CryptoJS.algo.AES,
3840 * mode: CryptoJS.mode.CBC,
3841 * padding: CryptoJS.pad.PKCS7,
3842 * blockSize: 4,
3843 * formatter: CryptoJS.format.OpenSSL
3844 * });
3845 */
3846 init: function (cipherParams) {
3847 this.mixIn(cipherParams);
3848 },
3849
3850 /**
3851 * Converts this cipher params object to a string.
3852 *
3853 * @param {Format} formatter (Optional) The formatting strategy to use.
3854 *
3855 * @return {string} The stringified cipher params.
3856 *
3857 * @throws Error If neither the formatter nor the default formatter is set.
3858 *
3859 * @example
3860 *
3861 * var string = cipherParams + '';
3862 * var string = cipherParams.toString();
3863 * var string = cipherParams.toString(CryptoJS.format.OpenSSL);
3864 */
3865 toString: function (formatter) {
3866 return (formatter || this.formatter).stringify(this);
3867 }
3868 });
3869
3870 /**
3871 * Format namespace.
3872 */
3873 var C_format = C.format = {};
3874
3875 /**
3876 * OpenSSL formatting strategy.
3877 */
3878 var OpenSSLFormatter = C_format.OpenSSL = {
3879 /**
3880 * Converts a cipher params object to an OpenSSL-compatible string.
3881 *
3882 * @param {CipherParams} cipherParams The cipher params object.
3883 *
3884 * @return {string} The OpenSSL-compatible string.
3885 *
3886 * @static
3887 *
3888 * @example
3889 *
3890 * var openSSLString = CryptoJS.format.OpenSSL.stringify(cipherParams);
3891 */
3892 stringify: function (cipherParams) {
3893 var wordArray;
3894
3895 // Shortcuts
3896 var ciphertext = cipherParams.ciphertext;
3897 var salt = cipherParams.salt;
3898
3899 // Format
3900 if (salt) {
3901 wordArray = WordArray.create([0x53616c74, 0x65645f5f]).concat(salt).concat(ciphertext);
3902 } else {
3903 wordArray = ciphertext;
3904 }
3905
3906 return wordArray.toString(Base64);
3907 },
3908
3909 /**
3910 * Converts an OpenSSL-compatible string to a cipher params object.
3911 *
3912 * @param {string} openSSLStr The OpenSSL-compatible string.
3913 *
3914 * @return {CipherParams} The cipher params object.
3915 *
3916 * @static
3917 *
3918 * @example
3919 *
3920 * var cipherParams = CryptoJS.format.OpenSSL.parse(openSSLString);
3921 */
3922 parse: function (openSSLStr) {
3923 var salt;
3924
3925 // Parse base64
3926 var ciphertext = Base64.parse(openSSLStr);
3927
3928 // Shortcut
3929 var ciphertextWords = ciphertext.words;
3930
3931 // Test for salt
3932 if (ciphertextWords[0] == 0x53616c74 && ciphertextWords[1] == 0x65645f5f) {
3933 // Extract salt
3934 salt = WordArray.create(ciphertextWords.slice(2, 4));
3935
3936 // Remove salt from ciphertext
3937 ciphertextWords.splice(0, 4);
3938 ciphertext.sigBytes -= 16;
3939 }
3940
3941 return CipherParams.create({ ciphertext: ciphertext, salt: salt });
3942 }
3943 };
3944
3945 /**
3946 * A cipher wrapper that returns ciphertext as a serializable cipher params object.
3947 */
3948 var SerializableCipher = C_lib.SerializableCipher = Base.extend({
3949 /**
3950 * Configuration options.
3951 *
3952 * @property {Formatter} format The formatting strategy to convert cipher param objects to and from a string. Default: OpenSSL
3953 */
3954 cfg: Base.extend({
3955 format: OpenSSLFormatter
3956 }),
3957
3958 /**
3959 * Encrypts a message.
3960 *
3961 * @param {Cipher} cipher The cipher algorithm to use.
3962 * @param {WordArray|string} message The message to encrypt.
3963 * @param {WordArray} key The key.
3964 * @param {Object} cfg (Optional) The configuration options to use for this operation.
3965 *
3966 * @return {CipherParams} A cipher params object.
3967 *
3968 * @static
3969 *
3970 * @example
3971 *
3972 * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key);
3973 * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv });
3974 * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv, format: CryptoJS.format.OpenSSL });
3975 */
3976 encrypt: function (cipher, message, key, cfg) {
3977 // Apply config defaults
3978 cfg = this.cfg.extend(cfg);
3979
3980 // Encrypt
3981 var encryptor = cipher.createEncryptor(key, cfg);
3982 var ciphertext = encryptor.finalize(message);
3983
3984 // Shortcut
3985 var cipherCfg = encryptor.cfg;
3986
3987 // Create and return serializable cipher params
3988 return CipherParams.create({
3989 ciphertext: ciphertext,
3990 key: key,
3991 iv: cipherCfg.iv,
3992 algorithm: cipher,
3993 mode: cipherCfg.mode,
3994 padding: cipherCfg.padding,
3995 blockSize: cipher.blockSize,
3996 formatter: cfg.format
3997 });
3998 },
3999
4000 /**
4001 * Decrypts serialized ciphertext.
4002 *
4003 * @param {Cipher} cipher The cipher algorithm to use.
4004 * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
4005 * @param {WordArray} key The key.
4006 * @param {Object} cfg (Optional) The configuration options to use for this operation.
4007 *
4008 * @return {WordArray} The plaintext.
4009 *
4010 * @static
4011 *
4012 * @example
4013 *
4014 * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, key, { iv: iv, format: CryptoJS.format.OpenSSL });
4015 * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, key, { iv: iv, format: CryptoJS.format.OpenSSL });
4016 */
4017 decrypt: function (cipher, ciphertext, key, cfg) {
4018 // Apply config defaults
4019 cfg = this.cfg.extend(cfg);
4020
4021 // Convert string to CipherParams
4022 ciphertext = this._parse(ciphertext, cfg.format);
4023
4024 // Decrypt
4025 var plaintext = cipher.createDecryptor(key, cfg).finalize(ciphertext.ciphertext);
4026
4027 return plaintext;
4028 },
4029
4030 /**
4031 * Converts serialized ciphertext to CipherParams,
4032 * else assumed CipherParams already and returns ciphertext unchanged.
4033 *
4034 * @param {CipherParams|string} ciphertext The ciphertext.
4035 * @param {Formatter} format The formatting strategy to use to parse serialized ciphertext.
4036 *
4037 * @return {CipherParams} The unserialized ciphertext.
4038 *
4039 * @static
4040 *
4041 * @example
4042 *
4043 * var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
4044 */
4045 _parse: function (ciphertext, format) {
4046 if (typeof ciphertext == 'string') {
4047 return format.parse(ciphertext, this);
4048 } else {
4049 return ciphertext;
4050 }
4051 }
4052 });
4053
4054 /**
4055 * Key derivation function namespace.
4056 */
4057 var C_kdf = C.kdf = {};
4058
4059 /**
4060 * OpenSSL key derivation function.
4061 */
4062 var OpenSSLKdf = C_kdf.OpenSSL = {
4063 /**
4064 * Derives a key and IV from a password.
4065 *
4066 * @param {string} password The password to derive from.
4067 * @param {number} keySize The size in words of the key to generate.
4068 * @param {number} ivSize The size in words of the IV to generate.
4069 * @param {WordArray|string} salt (Optional) A 64-bit salt to use. If omitted, a salt will be generated randomly.
4070 *
4071 * @return {CipherParams} A cipher params object with the key, IV, and salt.
4072 *
4073 * @static
4074 *
4075 * @example
4076 *
4077 * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32);
4078 * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32, 'saltsalt');
4079 */
4080 execute: function (password, keySize, ivSize, salt) {
4081 // Generate random salt
4082 if (!salt) {
4083 salt = WordArray.random(64/8);
4084 }
4085
4086 // Derive key and IV
4087 var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);
4088
4089 // Separate key and IV
4090 var iv = WordArray.create(key.words.slice(keySize), ivSize * 4);
4091 key.sigBytes = keySize * 4;
4092
4093 // Return params
4094 return CipherParams.create({ key: key, iv: iv, salt: salt });
4095 }
4096 };
4097
4098 /**
4099 * A serializable cipher wrapper that derives the key from a password,
4100 * and returns ciphertext as a serializable cipher params object.
4101 */
4102 var PasswordBasedCipher = C_lib.PasswordBasedCipher = SerializableCipher.extend({
4103 /**
4104 * Configuration options.
4105 *
4106 * @property {KDF} kdf The key derivation function to use to generate a key and IV from a password. Default: OpenSSL
4107 */
4108 cfg: SerializableCipher.cfg.extend({
4109 kdf: OpenSSLKdf
4110 }),
4111
4112 /**
4113 * Encrypts a message using a password.
4114 *
4115 * @param {Cipher} cipher The cipher algorithm to use.
4116 * @param {WordArray|string} message The message to encrypt.
4117 * @param {string} password The password.
4118 * @param {Object} cfg (Optional) The configuration options to use for this operation.
4119 *
4120 * @return {CipherParams} A cipher params object.
4121 *
4122 * @static
4123 *
4124 * @example
4125 *
4126 * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password');
4127 * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password', { format: CryptoJS.format.OpenSSL });
4128 */
4129 encrypt: function (cipher, message, password, cfg) {
4130 // Apply config defaults
4131 cfg = this.cfg.extend(cfg);
4132
4133 // Derive key and other params
4134 var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize);
4135
4136 // Add IV to config
4137 cfg.iv = derivedParams.iv;
4138
4139 // Encrypt
4140 var ciphertext = SerializableCipher.encrypt.call(this, cipher, message, derivedParams.key, cfg);
4141
4142 // Mix in derived params
4143 ciphertext.mixIn(derivedParams);
4144
4145 return ciphertext;
4146 },
4147
4148 /**
4149 * Decrypts serialized ciphertext using a password.
4150 *
4151 * @param {Cipher} cipher The cipher algorithm to use.
4152 * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
4153 * @param {string} password The password.
4154 * @param {Object} cfg (Optional) The configuration options to use for this operation.
4155 *
4156 * @return {WordArray} The plaintext.
4157 *
4158 * @static
4159 *
4160 * @example
4161 *
4162 * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, 'password', { format: CryptoJS.format.OpenSSL });
4163 * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, 'password', { format: CryptoJS.format.OpenSSL });
4164 */
4165 decrypt: function (cipher, ciphertext, password, cfg) {
4166 // Apply config defaults
4167 cfg = this.cfg.extend(cfg);
4168
4169 // Convert string to CipherParams
4170 ciphertext = this._parse(ciphertext, cfg.format);
4171
4172 // Derive key and other params
4173 var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt);
4174
4175 // Add IV to config
4176 cfg.iv = derivedParams.iv;
4177
4178 // Decrypt
4179 var plaintext = SerializableCipher.decrypt.call(this, cipher, ciphertext, derivedParams.key, cfg);
4180
4181 return plaintext;
4182 }
4183 });
4184 }());
4185
4186
4187 /**
4188 * Cipher Feedback block mode.
4189 */
4190 CryptoJS.mode.CFB = (function () {
4191 var CFB = CryptoJS.lib.BlockCipherMode.extend();
4192
4193 CFB.Encryptor = CFB.extend({
4194 processBlock: function (words, offset) {
4195 // Shortcuts
4196 var cipher = this._cipher;
4197 var blockSize = cipher.blockSize;
4198
4199 generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
4200
4201 // Remember this block to use with next block
4202 this._prevBlock = words.slice(offset, offset + blockSize);
4203 }
4204 });
4205
4206 CFB.Decryptor = CFB.extend({
4207 processBlock: function (words, offset) {
4208 // Shortcuts
4209 var cipher = this._cipher;
4210 var blockSize = cipher.blockSize;
4211
4212 // Remember this block to use with next block
4213 var thisBlock = words.slice(offset, offset + blockSize);
4214
4215 generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
4216
4217 // This block becomes the previous block
4218 this._prevBlock = thisBlock;
4219 }
4220 });
4221
4222 function generateKeystreamAndEncrypt(words, offset, blockSize, cipher) {
4223 var keystream;
4224
4225 // Shortcut
4226 var iv = this._iv;
4227
4228 // Generate keystream
4229 if (iv) {
4230 keystream = iv.slice(0);
4231
4232 // Remove IV for subsequent blocks
4233 this._iv = undefined;
4234 } else {
4235 keystream = this._prevBlock;
4236 }
4237 cipher.encryptBlock(keystream, 0);
4238
4239 // Encrypt
4240 for (var i = 0; i < blockSize; i++) {
4241 words[offset + i] ^= keystream[i];
4242 }
4243 }
4244
4245 return CFB;
4246 }());
4247
4248
4249 /**
4250 * Electronic Codebook block mode.
4251 */
4252 CryptoJS.mode.ECB = (function () {
4253 var ECB = CryptoJS.lib.BlockCipherMode.extend();
4254
4255 ECB.Encryptor = ECB.extend({
4256 processBlock: function (words, offset) {
4257 this._cipher.encryptBlock(words, offset);
4258 }
4259 });
4260
4261 ECB.Decryptor = ECB.extend({
4262 processBlock: function (words, offset) {
4263 this._cipher.decryptBlock(words, offset);
4264 }
4265 });
4266
4267 return ECB;
4268 }());
4269
4270
4271 /**
4272 * ANSI X.923 padding strategy.
4273 */
4274 CryptoJS.pad.AnsiX923 = {
4275 pad: function (data, blockSize) {
4276 // Shortcuts
4277 var dataSigBytes = data.sigBytes;
4278 var blockSizeBytes = blockSize * 4;
4279
4280 // Count padding bytes
4281 var nPaddingBytes = blockSizeBytes - dataSigBytes % blockSizeBytes;
4282
4283 // Compute last byte position
4284 var lastBytePos = dataSigBytes + nPaddingBytes - 1;
4285
4286 // Pad
4287 data.clamp();
4288 data.words[lastBytePos >>> 2] |= nPaddingBytes << (24 - (lastBytePos % 4) * 8);
4289 data.sigBytes += nPaddingBytes;
4290 },
4291
4292 unpad: function (data) {
4293 // Get number of padding bytes from last byte
4294 var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
4295
4296 // Remove padding
4297 data.sigBytes -= nPaddingBytes;
4298 }
4299 };
4300
4301
4302 /**
4303 * ISO 10126 padding strategy.
4304 */
4305 CryptoJS.pad.Iso10126 = {
4306 pad: function (data, blockSize) {
4307 // Shortcut
4308 var blockSizeBytes = blockSize * 4;
4309
4310 // Count padding bytes
4311 var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
4312
4313 // Pad
4314 data.concat(CryptoJS.lib.WordArray.random(nPaddingBytes - 1)).
4315 concat(CryptoJS.lib.WordArray.create([nPaddingBytes << 24], 1));
4316 },
4317
4318 unpad: function (data) {
4319 // Get number of padding bytes from last byte
4320 var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
4321
4322 // Remove padding
4323 data.sigBytes -= nPaddingBytes;
4324 }
4325 };
4326
4327
4328 /**
4329 * ISO/IEC 9797-1 Padding Method 2.
4330 */
4331 CryptoJS.pad.Iso97971 = {
4332 pad: function (data, blockSize) {
4333 // Add 0x80 byte
4334 data.concat(CryptoJS.lib.WordArray.create([0x80000000], 1));
4335
4336 // Zero pad the rest
4337 CryptoJS.pad.ZeroPadding.pad(data, blockSize);
4338 },
4339
4340 unpad: function (data) {
4341 // Remove zero padding
4342 CryptoJS.pad.ZeroPadding.unpad(data);
4343
4344 // Remove one more byte -- the 0x80 byte
4345 data.sigBytes--;
4346 }
4347 };
4348
4349
4350 /**
4351 * Output Feedback block mode.
4352 */
4353 CryptoJS.mode.OFB = (function () {
4354 var OFB = CryptoJS.lib.BlockCipherMode.extend();
4355
4356 var Encryptor = OFB.Encryptor = OFB.extend({
4357 processBlock: function (words, offset) {
4358 // Shortcuts
4359 var cipher = this._cipher
4360 var blockSize = cipher.blockSize;
4361 var iv = this._iv;
4362 var keystream = this._keystream;
4363
4364 // Generate keystream
4365 if (iv) {
4366 keystream = this._keystream = iv.slice(0);
4367
4368 // Remove IV for subsequent blocks
4369 this._iv = undefined;
4370 }
4371 cipher.encryptBlock(keystream, 0);
4372
4373 // Encrypt
4374 for (var i = 0; i < blockSize; i++) {
4375 words[offset + i] ^= keystream[i];
4376 }
4377 }
4378 });
4379
4380 OFB.Decryptor = Encryptor;
4381
4382 return OFB;
4383 }());
4384
4385
4386 /**
4387 * A noop padding strategy.
4388 */
4389 CryptoJS.pad.NoPadding = {
4390 pad: function () {
4391 },
4392
4393 unpad: function () {
4394 }
4395 };
4396
4397
4398 (function (undefined) {
4399 // Shortcuts
4400 var C = CryptoJS;
4401 var C_lib = C.lib;
4402 var CipherParams = C_lib.CipherParams;
4403 var C_enc = C.enc;
4404 var Hex = C_enc.Hex;
4405 var C_format = C.format;
4406
4407 var HexFormatter = C_format.Hex = {
4408 /**
4409 * Converts the ciphertext of a cipher params object to a hexadecimally encoded string.
4410 *
4411 * @param {CipherParams} cipherParams The cipher params object.
4412 *
4413 * @return {string} The hexadecimally encoded string.
4414 *
4415 * @static
4416 *
4417 * @example
4418 *
4419 * var hexString = CryptoJS.format.Hex.stringify(cipherParams);
4420 */
4421 stringify: function (cipherParams) {
4422 return cipherParams.ciphertext.toString(Hex);
4423 },
4424
4425 /**
4426 * Converts a hexadecimally encoded ciphertext string to a cipher params object.
4427 *
4428 * @param {string} input The hexadecimally encoded string.
4429 *
4430 * @return {CipherParams} The cipher params object.
4431 *
4432 * @static
4433 *
4434 * @example
4435 *
4436 * var cipherParams = CryptoJS.format.Hex.parse(hexString);
4437 */
4438 parse: function (input) {
4439 var ciphertext = Hex.parse(input);
4440 return CipherParams.create({ ciphertext: ciphertext });
4441 }
4442 };
4443 }());
4444
4445
4446 (function () {
4447 // Shortcuts
4448 var C = CryptoJS;
4449 var C_lib = C.lib;
4450 var BlockCipher = C_lib.BlockCipher;
4451 var C_algo = C.algo;
4452
4453 // Lookup tables
4454 var SBOX = [];
4455 var INV_SBOX = [];
4456 var SUB_MIX_0 = [];
4457 var SUB_MIX_1 = [];
4458 var SUB_MIX_2 = [];
4459 var SUB_MIX_3 = [];
4460 var INV_SUB_MIX_0 = [];
4461 var INV_SUB_MIX_1 = [];
4462 var INV_SUB_MIX_2 = [];
4463 var INV_SUB_MIX_3 = [];
4464
4465 // Compute lookup tables
4466 (function () {
4467 // Compute double table
4468 var d = [];
4469 for (var i = 0; i < 256; i++) {
4470 if (i < 128) {
4471 d[i] = i << 1;
4472 } else {
4473 d[i] = (i << 1) ^ 0x11b;
4474 }
4475 }
4476
4477 // Walk GF(2^8)
4478 var x = 0;
4479 var xi = 0;
4480 for (var i = 0; i < 256; i++) {
4481 // Compute sbox
4482 var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
4483 sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
4484 SBOX[x] = sx;
4485 INV_SBOX[sx] = x;
4486
4487 // Compute multiplication
4488 var x2 = d[x];
4489 var x4 = d[x2];
4490 var x8 = d[x4];
4491
4492 // Compute sub bytes, mix columns tables
4493 var t = (d[sx] * 0x101) ^ (sx * 0x1010100);
4494 SUB_MIX_0[x] = (t << 24) | (t >>> 8);
4495 SUB_MIX_1[x] = (t << 16) | (t >>> 16);
4496 SUB_MIX_2[x] = (t << 8) | (t >>> 24);
4497 SUB_MIX_3[x] = t;
4498
4499 // Compute inv sub bytes, inv mix columns tables
4500 var t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
4501 INV_SUB_MIX_0[sx] = (t << 24) | (t >>> 8);
4502 INV_SUB_MIX_1[sx] = (t << 16) | (t >>> 16);
4503 INV_SUB_MIX_2[sx] = (t << 8) | (t >>> 24);
4504 INV_SUB_MIX_3[sx] = t;
4505
4506 // Compute next counter
4507 if (!x) {
4508 x = xi = 1;
4509 } else {
4510 x = x2 ^ d[d[d[x8 ^ x2]]];
4511 xi ^= d[d[xi]];
4512 }
4513 }
4514 }());
4515
4516 // Precomputed Rcon lookup
4517 var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
4518
4519 /**
4520 * AES block cipher algorithm.
4521 */
4522 var AES = C_algo.AES = BlockCipher.extend({
4523 _doReset: function () {
4524 var t;
4525
4526 // Skip reset of nRounds has been set before and key did not change
4527 if (this._nRounds && this._keyPriorReset === this._key) {
4528 return;
4529 }
4530
4531 // Shortcuts
4532 var key = this._keyPriorReset = this._key;
4533 var keyWords = key.words;
4534 var keySize = key.sigBytes / 4;
4535
4536 // Compute number of rounds
4537 var nRounds = this._nRounds = keySize + 6;
4538
4539 // Compute number of key schedule rows
4540 var ksRows = (nRounds + 1) * 4;
4541
4542 // Compute key schedule
4543 var keySchedule = this._keySchedule = [];
4544 for (var ksRow = 0; ksRow < ksRows; ksRow++) {
4545 if (ksRow < keySize) {
4546 keySchedule[ksRow] = keyWords[ksRow];
4547 } else {
4548 t = keySchedule[ksRow - 1];
4549
4550 if (!(ksRow % keySize)) {
4551 // Rot word
4552 t = (t << 8) | (t >>> 24);
4553
4554 // Sub word
4555 t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
4556
4557 // Mix Rcon
4558 t ^= RCON[(ksRow / keySize) | 0] << 24;
4559 } else if (keySize > 6 && ksRow % keySize == 4) {
4560 // Sub word
4561 t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
4562 }
4563
4564 keySchedule[ksRow] = keySchedule[ksRow - keySize] ^ t;
4565 }
4566 }
4567
4568 // Compute inv key schedule
4569 var invKeySchedule = this._invKeySchedule = [];
4570 for (var invKsRow = 0; invKsRow < ksRows; invKsRow++) {
4571 var ksRow = ksRows - invKsRow;
4572
4573 if (invKsRow % 4) {
4574 var t = keySchedule[ksRow];
4575 } else {
4576 var t = keySchedule[ksRow - 4];
4577 }
4578
4579 if (invKsRow < 4 || ksRow <= 4) {
4580 invKeySchedule[invKsRow] = t;
4581 } else {
4582 invKeySchedule[invKsRow] = INV_SUB_MIX_0[SBOX[t >>> 24]] ^ INV_SUB_MIX_1[SBOX[(t >>> 16) & 0xff]] ^
4583 INV_SUB_MIX_2[SBOX[(t >>> 8) & 0xff]] ^ INV_SUB_MIX_3[SBOX[t & 0xff]];
4584 }
4585 }
4586 },
4587
4588 encryptBlock: function (M, offset) {
4589 this._doCryptBlock(M, offset, this._keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX);
4590 },
4591
4592 decryptBlock: function (M, offset) {
4593 // Swap 2nd and 4th rows
4594 var t = M[offset + 1];
4595 M[offset + 1] = M[offset + 3];
4596 M[offset + 3] = t;
4597
4598 this._doCryptBlock(M, offset, this._invKeySchedule, INV_SUB_MIX_0, INV_SUB_MIX_1, INV_SUB_MIX_2, INV_SUB_MIX_3, INV_SBOX);
4599
4600 // Inv swap 2nd and 4th rows
4601 var t = M[offset + 1];
4602 M[offset + 1] = M[offset + 3];
4603 M[offset + 3] = t;
4604 },
4605
4606 _doCryptBlock: function (M, offset, keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX) {
4607 // Shortcut
4608 var nRounds = this._nRounds;
4609
4610 // Get input, add round key
4611 var s0 = M[offset] ^ keySchedule[0];
4612 var s1 = M[offset + 1] ^ keySchedule[1];
4613 var s2 = M[offset + 2] ^ keySchedule[2];
4614 var s3 = M[offset + 3] ^ keySchedule[3];
4615
4616 // Key schedule row counter
4617 var ksRow = 4;
4618
4619 // Rounds
4620 for (var round = 1; round < nRounds; round++) {
4621 // Shift rows, sub bytes, mix columns, add round key
4622 var t0 = SUB_MIX_0[s0 >>> 24] ^ SUB_MIX_1[(s1 >>> 16) & 0xff] ^ SUB_MIX_2[(s2 >>> 8) & 0xff] ^ SUB_MIX_3[s3 & 0xff] ^ keySchedule[ksRow++];
4623 var t1 = SUB_MIX_0[s1 >>> 24] ^ SUB_MIX_1[(s2 >>> 16) & 0xff] ^ SUB_MIX_2[(s3 >>> 8) & 0xff] ^ SUB_MIX_3[s0 & 0xff] ^ keySchedule[ksRow++];
4624 var t2 = SUB_MIX_0[s2 >>> 24] ^ SUB_MIX_1[(s3 >>> 16) & 0xff] ^ SUB_MIX_2[(s0 >>> 8) & 0xff] ^ SUB_MIX_3[s1 & 0xff] ^ keySchedule[ksRow++];
4625 var t3 = SUB_MIX_0[s3 >>> 24] ^ SUB_MIX_1[(s0 >>> 16) & 0xff] ^ SUB_MIX_2[(s1 >>> 8) & 0xff] ^ SUB_MIX_3[s2 & 0xff] ^ keySchedule[ksRow++];
4626
4627 // Update state
4628 s0 = t0;
4629 s1 = t1;
4630 s2 = t2;
4631 s3 = t3;
4632 }
4633
4634 // Shift rows, sub bytes, add round key
4635 var t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
4636 var t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
4637 var t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
4638 var t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
4639
4640 // Set output
4641 M[offset] = t0;
4642 M[offset + 1] = t1;
4643 M[offset + 2] = t2;
4644 M[offset + 3] = t3;
4645 },
4646
4647 keySize: 256/32
4648 });
4649
4650 /**
4651 * Shortcut functions to the cipher's object interface.
4652 *
4653 * @example
4654 *
4655 * var ciphertext = CryptoJS.AES.encrypt(message, key, cfg);
4656 * var plaintext = CryptoJS.AES.decrypt(ciphertext, key, cfg);
4657 */
4658 C.AES = BlockCipher._createHelper(AES);
4659 }());
4660
4661
4662 (function () {
4663 // Shortcuts
4664 var C = CryptoJS;
4665 var C_lib = C.lib;
4666 var WordArray = C_lib.WordArray;
4667 var BlockCipher = C_lib.BlockCipher;
4668 var C_algo = C.algo;
4669
4670 // Permuted Choice 1 constants
4671 var PC1 = [
4672 57, 49, 41, 33, 25, 17, 9, 1,
4673 58, 50, 42, 34, 26, 18, 10, 2,
4674 59, 51, 43, 35, 27, 19, 11, 3,
4675 60, 52, 44, 36, 63, 55, 47, 39,
4676 31, 23, 15, 7, 62, 54, 46, 38,
4677 30, 22, 14, 6, 61, 53, 45, 37,
4678 29, 21, 13, 5, 28, 20, 12, 4
4679 ];
4680
4681 // Permuted Choice 2 constants
4682 var PC2 = [
4683 14, 17, 11, 24, 1, 5,
4684 3, 28, 15, 6, 21, 10,
4685 23, 19, 12, 4, 26, 8,
4686 16, 7, 27, 20, 13, 2,
4687 41, 52, 31, 37, 47, 55,
4688 30, 40, 51, 45, 33, 48,
4689 44, 49, 39, 56, 34, 53,
4690 46, 42, 50, 36, 29, 32
4691 ];
4692
4693 // Cumulative bit shift constants
4694 var BIT_SHIFTS = [1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28];
4695
4696 // SBOXes and round permutation constants
4697 var SBOX_P = [
4698 {
4699 0x0: 0x808200,
4700 0x10000000: 0x8000,
4701 0x20000000: 0x808002,
4702 0x30000000: 0x2,
4703 0x40000000: 0x200,
4704 0x50000000: 0x808202,
4705 0x60000000: 0x800202,
4706 0x70000000: 0x800000,
4707 0x80000000: 0x202,
4708 0x90000000: 0x800200,
4709 0xa0000000: 0x8200,
4710 0xb0000000: 0x808000,
4711 0xc0000000: 0x8002,
4712 0xd0000000: 0x800002,
4713 0xe0000000: 0x0,
4714 0xf0000000: 0x8202,
4715 0x8000000: 0x0,
4716 0x18000000: 0x808202,
4717 0x28000000: 0x8202,
4718 0x38000000: 0x8000,
4719 0x48000000: 0x808200,
4720 0x58000000: 0x200,
4721 0x68000000: 0x808002,
4722 0x78000000: 0x2,
4723 0x88000000: 0x800200,
4724 0x98000000: 0x8200,
4725 0xa8000000: 0x808000,
4726 0xb8000000: 0x800202,
4727 0xc8000000: 0x800002,
4728 0xd8000000: 0x8002,
4729 0xe8000000: 0x202,
4730 0xf8000000: 0x800000,
4731 0x1: 0x8000,
4732 0x10000001: 0x2,
4733 0x20000001: 0x808200,
4734 0x30000001: 0x800000,
4735 0x40000001: 0x808002,
4736 0x50000001: 0x8200,
4737 0x60000001: 0x200,
4738 0x70000001: 0x800202,
4739 0x80000001: 0x808202,
4740 0x90000001: 0x808000,
4741 0xa0000001: 0x800002,
4742 0xb0000001: 0x8202,
4743 0xc0000001: 0x202,
4744 0xd0000001: 0x800200,
4745 0xe0000001: 0x8002,
4746 0xf0000001: 0x0,
4747 0x8000001: 0x808202,
4748 0x18000001: 0x808000,
4749 0x28000001: 0x800000,
4750 0x38000001: 0x200,
4751 0x48000001: 0x8000,
4752 0x58000001: 0x800002,
4753 0x68000001: 0x2,
4754 0x78000001: 0x8202,
4755 0x88000001: 0x8002,
4756 0x98000001: 0x800202,
4757 0xa8000001: 0x202,
4758 0xb8000001: 0x808200,
4759 0xc8000001: 0x800200,
4760 0xd8000001: 0x0,
4761 0xe8000001: 0x8200,
4762 0xf8000001: 0x808002
4763 },
4764 {
4765 0x0: 0x40084010,
4766 0x1000000: 0x4000,
4767 0x2000000: 0x80000,
4768 0x3000000: 0x40080010,
4769 0x4000000: 0x40000010,
4770 0x5000000: 0x40084000,
4771 0x6000000: 0x40004000,
4772 0x7000000: 0x10,
4773 0x8000000: 0x84000,
4774 0x9000000: 0x40004010,
4775 0xa000000: 0x40000000,
4776 0xb000000: 0x84010,
4777 0xc000000: 0x80010,
4778 0xd000000: 0x0,
4779 0xe000000: 0x4010,
4780 0xf000000: 0x40080000,
4781 0x800000: 0x40004000,
4782 0x1800000: 0x84010,
4783 0x2800000: 0x10,
4784 0x3800000: 0x40004010,
4785 0x4800000: 0x40084010,
4786 0x5800000: 0x40000000,
4787 0x6800000: 0x80000,
4788 0x7800000: 0x40080010,
4789 0x8800000: 0x80010,
4790 0x9800000: 0x0,
4791 0xa800000: 0x4000,
4792 0xb800000: 0x40080000,
4793 0xc800000: 0x40000010,
4794 0xd800000: 0x84000,
4795 0xe800000: 0x40084000,
4796 0xf800000: 0x4010,
4797 0x10000000: 0x0,
4798 0x11000000: 0x40080010,
4799 0x12000000: 0x40004010,
4800 0x13000000: 0x40084000,
4801 0x14000000: 0x40080000,
4802 0x15000000: 0x10,
4803 0x16000000: 0x84010,
4804 0x17000000: 0x4000,
4805 0x18000000: 0x4010,
4806 0x19000000: 0x80000,
4807 0x1a000000: 0x80010,
4808 0x1b000000: 0x40000010,
4809 0x1c000000: 0x84000,
4810 0x1d000000: 0x40004000,
4811 0x1e000000: 0x40000000,
4812 0x1f000000: 0x40084010,
4813 0x10800000: 0x84010,
4814 0x11800000: 0x80000,
4815 0x12800000: 0x40080000,
4816 0x13800000: 0x4000,
4817 0x14800000: 0x40004000,
4818 0x15800000: 0x40084010,
4819 0x16800000: 0x10,
4820 0x17800000: 0x40000000,
4821 0x18800000: 0x40084000,
4822 0x19800000: 0x40000010,
4823 0x1a800000: 0x40004010,
4824 0x1b800000: 0x80010,
4825 0x1c800000: 0x0,
4826 0x1d800000: 0x4010,
4827 0x1e800000: 0x40080010,
4828 0x1f800000: 0x84000
4829 },
4830 {
4831 0x0: 0x104,
4832 0x100000: 0x0,
4833 0x200000: 0x4000100,
4834 0x300000: 0x10104,
4835 0x400000: 0x10004,
4836 0x500000: 0x4000004,
4837 0x600000: 0x4010104,
4838 0x700000: 0x4010000,
4839 0x800000: 0x4000000,
4840 0x900000: 0x4010100,
4841 0xa00000: 0x10100,
4842 0xb00000: 0x4010004,
4843 0xc00000: 0x4000104,
4844 0xd00000: 0x10000,
4845 0xe00000: 0x4,
4846 0xf00000: 0x100,
4847 0x80000: 0x4010100,
4848 0x180000: 0x4010004,
4849 0x280000: 0x0,
4850 0x380000: 0x4000100,
4851 0x480000: 0x4000004,
4852 0x580000: 0x10000,
4853 0x680000: 0x10004,
4854 0x780000: 0x104,
4855 0x880000: 0x4,
4856 0x980000: 0x100,
4857 0xa80000: 0x4010000,
4858 0xb80000: 0x10104,
4859 0xc80000: 0x10100,
4860 0xd80000: 0x4000104,
4861 0xe80000: 0x4010104,
4862 0xf80000: 0x4000000,
4863 0x1000000: 0x4010100,
4864 0x1100000: 0x10004,
4865 0x1200000: 0x10000,
4866 0x1300000: 0x4000100,
4867 0x1400000: 0x100,
4868 0x1500000: 0x4010104,
4869 0x1600000: 0x4000004,
4870 0x1700000: 0x0,
4871 0x1800000: 0x4000104,
4872 0x1900000: 0x4000000,
4873 0x1a00000: 0x4,
4874 0x1b00000: 0x10100,
4875 0x1c00000: 0x4010000,
4876 0x1d00000: 0x104,
4877 0x1e00000: 0x10104,
4878 0x1f00000: 0x4010004,
4879 0x1080000: 0x4000000,
4880 0x1180000: 0x104,
4881 0x1280000: 0x4010100,
4882 0x1380000: 0x0,
4883 0x1480000: 0x10004,
4884 0x1580000: 0x4000100,
4885 0x1680000: 0x100,
4886 0x1780000: 0x4010004,
4887 0x1880000: 0x10000,
4888 0x1980000: 0x4010104,
4889 0x1a80000: 0x10104,
4890 0x1b80000: 0x4000004,
4891 0x1c80000: 0x4000104,
4892 0x1d80000: 0x4010000,
4893 0x1e80000: 0x4,
4894 0x1f80000: 0x10100
4895 },
4896 {
4897 0x0: 0x80401000,
4898 0x10000: 0x80001040,
4899 0x20000: 0x401040,
4900 0x30000: 0x80400000,
4901 0x40000: 0x0,
4902 0x50000: 0x401000,
4903 0x60000: 0x80000040,
4904 0x70000: 0x400040,
4905 0x80000: 0x80000000,
4906 0x90000: 0x400000,
4907 0xa0000: 0x40,
4908 0xb0000: 0x80001000,
4909 0xc0000: 0x80400040,
4910 0xd0000: 0x1040,
4911 0xe0000: 0x1000,
4912 0xf0000: 0x80401040,
4913 0x8000: 0x80001040,
4914 0x18000: 0x40,
4915 0x28000: 0x80400040,
4916 0x38000: 0x80001000,
4917 0x48000: 0x401000,
4918 0x58000: 0x80401040,
4919 0x68000: 0x0,
4920 0x78000: 0x80400000,
4921 0x88000: 0x1000,
4922 0x98000: 0x80401000,
4923 0xa8000: 0x400000,
4924 0xb8000: 0x1040,
4925 0xc8000: 0x80000000,
4926 0xd8000: 0x400040,
4927 0xe8000: 0x401040,
4928 0xf8000: 0x80000040,
4929 0x100000: 0x400040,
4930 0x110000: 0x401000,
4931 0x120000: 0x80000040,
4932 0x130000: 0x0,
4933 0x140000: 0x1040,
4934 0x150000: 0x80400040,
4935 0x160000: 0x80401000,
4936 0x170000: 0x80001040,
4937 0x180000: 0x80401040,
4938 0x190000: 0x80000000,
4939 0x1a0000: 0x80400000,
4940 0x1b0000: 0x401040,
4941 0x1c0000: 0x80001000,
4942 0x1d0000: 0x400000,
4943 0x1e0000: 0x40,
4944 0x1f0000: 0x1000,
4945 0x108000: 0x80400000,
4946 0x118000: 0x80401040,
4947 0x128000: 0x0,
4948 0x138000: 0x401000,
4949 0x148000: 0x400040,
4950 0x158000: 0x80000000,
4951 0x168000: 0x80001040,
4952 0x178000: 0x40,
4953 0x188000: 0x80000040,
4954 0x198000: 0x1000,
4955 0x1a8000: 0x80001000,
4956 0x1b8000: 0x80400040,
4957 0x1c8000: 0x1040,
4958 0x1d8000: 0x80401000,
4959 0x1e8000: 0x400000,
4960 0x1f8000: 0x401040
4961 },
4962 {
4963 0x0: 0x80,
4964 0x1000: 0x1040000,
4965 0x2000: 0x40000,
4966 0x3000: 0x20000000,
4967 0x4000: 0x20040080,
4968 0x5000: 0x1000080,
4969 0x6000: 0x21000080,
4970 0x7000: 0x40080,
4971 0x8000: 0x1000000,
4972 0x9000: 0x20040000,
4973 0xa000: 0x20000080,
4974 0xb000: 0x21040080,
4975 0xc000: 0x21040000,
4976 0xd000: 0x0,
4977 0xe000: 0x1040080,
4978 0xf000: 0x21000000,
4979 0x800: 0x1040080,
4980 0x1800: 0x21000080,
4981 0x2800: 0x80,
4982 0x3800: 0x1040000,
4983 0x4800: 0x40000,
4984 0x5800: 0x20040080,
4985 0x6800: 0x21040000,
4986 0x7800: 0x20000000,
4987 0x8800: 0x20040000,
4988 0x9800: 0x0,
4989 0xa800: 0x21040080,
4990 0xb800: 0x1000080,
4991 0xc800: 0x20000080,
4992 0xd800: 0x21000000,
4993 0xe800: 0x1000000,
4994 0xf800: 0x40080,
4995 0x10000: 0x40000,
4996 0x11000: 0x80,
4997 0x12000: 0x20000000,
4998 0x13000: 0x21000080,
4999 0x14000: 0x1000080,
5000 0x15000: 0x21040000,
5001 0x16000: 0x20040080,
5002 0x17000: 0x1000000,
5003 0x18000: 0x21040080,
5004 0x19000: 0x21000000,
5005 0x1a000: 0x1040000,
5006 0x1b000: 0x20040000,
5007 0x1c000: 0x40080,
5008 0x1d000: 0x20000080,
5009 0x1e000: 0x0,
5010 0x1f000: 0x1040080,
5011 0x10800: 0x21000080,
5012 0x11800: 0x1000000,
5013 0x12800: 0x1040000,
5014 0x13800: 0x20040080,
5015 0x14800: 0x20000000,
5016 0x15800: 0x1040080,
5017 0x16800: 0x80,
5018 0x17800: 0x21040000,
5019 0x18800: 0x40080,
5020 0x19800: 0x21040080,
5021 0x1a800: 0x0,
5022 0x1b800: 0x21000000,
5023 0x1c800: 0x1000080,
5024 0x1d800: 0x40000,
5025 0x1e800: 0x20040000,
5026 0x1f800: 0x20000080
5027 },
5028 {
5029 0x0: 0x10000008,
5030 0x100: 0x2000,
5031 0x200: 0x10200000,
5032 0x300: 0x10202008,
5033 0x400: 0x10002000,
5034 0x500: 0x200000,
5035 0x600: 0x200008,
5036 0x700: 0x10000000,
5037 0x800: 0x0,
5038 0x900: 0x10002008,
5039 0xa00: 0x202000,
5040 0xb00: 0x8,
5041 0xc00: 0x10200008,
5042 0xd00: 0x202008,
5043 0xe00: 0x2008,
5044 0xf00: 0x10202000,
5045 0x80: 0x10200000,
5046 0x180: 0x10202008,
5047 0x280: 0x8,
5048 0x380: 0x200000,
5049 0x480: 0x202008,
5050 0x580: 0x10000008,
5051 0x680: 0x10002000,
5052 0x780: 0x2008,
5053 0x880: 0x200008,
5054 0x980: 0x2000,
5055 0xa80: 0x10002008,
5056 0xb80: 0x10200008,
5057 0xc80: 0x0,
5058 0xd80: 0x10202000,
5059 0xe80: 0x202000,
5060 0xf80: 0x10000000,
5061 0x1000: 0x10002000,
5062 0x1100: 0x10200008,
5063 0x1200: 0x10202008,
5064 0x1300: 0x2008,
5065 0x1400: 0x200000,
5066 0x1500: 0x10000000,
5067 0x1600: 0x10000008,
5068 0x1700: 0x202000,
5069 0x1800: 0x202008,
5070 0x1900: 0x0,
5071 0x1a00: 0x8,
5072 0x1b00: 0x10200000,
5073 0x1c00: 0x2000,
5074 0x1d00: 0x10002008,
5075 0x1e00: 0x10202000,
5076 0x1f00: 0x200008,
5077 0x1080: 0x8,
5078 0x1180: 0x202000,
5079 0x1280: 0x200000,
5080 0x1380: 0x10000008,
5081 0x1480: 0x10002000,
5082 0x1580: 0x2008,
5083 0x1680: 0x10202008,
5084 0x1780: 0x10200000,
5085 0x1880: 0x10202000,
5086 0x1980: 0x10200008,
5087 0x1a80: 0x2000,
5088 0x1b80: 0x202008,
5089 0x1c80: 0x200008,
5090 0x1d80: 0x0,
5091 0x1e80: 0x10000000,
5092 0x1f80: 0x10002008
5093 },
5094 {
5095 0x0: 0x100000,
5096 0x10: 0x2000401,
5097 0x20: 0x400,
5098 0x30: 0x100401,
5099 0x40: 0x2100401,
5100 0x50: 0x0,
5101 0x60: 0x1,
5102 0x70: 0x2100001,
5103 0x80: 0x2000400,
5104 0x90: 0x100001,
5105 0xa0: 0x2000001,
5106 0xb0: 0x2100400,
5107 0xc0: 0x2100000,
5108 0xd0: 0x401,
5109 0xe0: 0x100400,
5110 0xf0: 0x2000000,
5111 0x8: 0x2100001,
5112 0x18: 0x0,
5113 0x28: 0x2000401,
5114 0x38: 0x2100400,
5115 0x48: 0x100000,
5116 0x58: 0x2000001,
5117 0x68: 0x2000000,
5118 0x78: 0x401,
5119 0x88: 0x100401,
5120 0x98: 0x2000400,
5121 0xa8: 0x2100000,
5122 0xb8: 0x100001,
5123 0xc8: 0x400,
5124 0xd8: 0x2100401,
5125 0xe8: 0x1,
5126 0xf8: 0x100400,
5127 0x100: 0x2000000,
5128 0x110: 0x100000,
5129 0x120: 0x2000401,
5130 0x130: 0x2100001,
5131 0x140: 0x100001,
5132 0x150: 0x2000400,
5133 0x160: 0x2100400,
5134 0x170: 0x100401,
5135 0x180: 0x401,
5136 0x190: 0x2100401,
5137 0x1a0: 0x100400,
5138 0x1b0: 0x1,
5139 0x1c0: 0x0,
5140 0x1d0: 0x2100000,
5141 0x1e0: 0x2000001,
5142 0x1f0: 0x400,
5143 0x108: 0x100400,
5144 0x118: 0x2000401,
5145 0x128: 0x2100001,
5146 0x138: 0x1,
5147 0x148: 0x2000000,
5148 0x158: 0x100000,
5149 0x168: 0x401,
5150 0x178: 0x2100400,
5151 0x188: 0x2000001,
5152 0x198: 0x2100000,
5153 0x1a8: 0x0,
5154 0x1b8: 0x2100401,
5155 0x1c8: 0x100401,
5156 0x1d8: 0x400,
5157 0x1e8: 0x2000400,
5158 0x1f8: 0x100001
5159 },
5160 {
5161 0x0: 0x8000820,
5162 0x1: 0x20000,
5163 0x2: 0x8000000,
5164 0x3: 0x20,
5165 0x4: 0x20020,
5166 0x5: 0x8020820,
5167 0x6: 0x8020800,
5168 0x7: 0x800,
5169 0x8: 0x8020000,
5170 0x9: 0x8000800,
5171 0xa: 0x20800,
5172 0xb: 0x8020020,
5173 0xc: 0x820,
5174 0xd: 0x0,
5175 0xe: 0x8000020,
5176 0xf: 0x20820,
5177 0x80000000: 0x800,
5178 0x80000001: 0x8020820,
5179 0x80000002: 0x8000820,
5180 0x80000003: 0x8000000,
5181 0x80000004: 0x8020000,
5182 0x80000005: 0x20800,
5183 0x80000006: 0x20820,
5184 0x80000007: 0x20,
5185 0x80000008: 0x8000020,
5186 0x80000009: 0x820,
5187 0x8000000a: 0x20020,
5188 0x8000000b: 0x8020800,
5189 0x8000000c: 0x0,
5190 0x8000000d: 0x8020020,
5191 0x8000000e: 0x8000800,
5192 0x8000000f: 0x20000,
5193 0x10: 0x20820,
5194 0x11: 0x8020800,
5195 0x12: 0x20,
5196 0x13: 0x800,
5197 0x14: 0x8000800,
5198 0x15: 0x8000020,
5199 0x16: 0x8020020,
5200 0x17: 0x20000,
5201 0x18: 0x0,
5202 0x19: 0x20020,
5203 0x1a: 0x8020000,
5204 0x1b: 0x8000820,
5205 0x1c: 0x8020820,
5206 0x1d: 0x20800,
5207 0x1e: 0x820,
5208 0x1f: 0x8000000,
5209 0x80000010: 0x20000,
5210 0x80000011: 0x800,
5211 0x80000012: 0x8020020,
5212 0x80000013: 0x20820,
5213 0x80000014: 0x20,
5214 0x80000015: 0x8020000,
5215 0x80000016: 0x8000000,
5216 0x80000017: 0x8000820,
5217 0x80000018: 0x8020820,
5218 0x80000019: 0x8000020,
5219 0x8000001a: 0x8000800,
5220 0x8000001b: 0x0,
5221 0x8000001c: 0x20800,
5222 0x8000001d: 0x820,
5223 0x8000001e: 0x20020,
5224 0x8000001f: 0x8020800
5225 }
5226 ];
5227
5228 // Masks that select the SBOX input
5229 var SBOX_MASK = [
5230 0xf8000001, 0x1f800000, 0x01f80000, 0x001f8000,
5231 0x0001f800, 0x00001f80, 0x000001f8, 0x8000001f
5232 ];
5233
5234 /**
5235 * DES block cipher algorithm.
5236 */
5237 var DES = C_algo.DES = BlockCipher.extend({
5238 _doReset: function () {
5239 // Shortcuts
5240 var key = this._key;
5241 var keyWords = key.words;
5242
5243 // Select 56 bits according to PC1
5244 var keyBits = [];
5245 for (var i = 0; i < 56; i++) {
5246 var keyBitPos = PC1[i] - 1;
5247 keyBits[i] = (keyWords[keyBitPos >>> 5] >>> (31 - keyBitPos % 32)) & 1;
5248 }
5249
5250 // Assemble 16 subkeys
5251 var subKeys = this._subKeys = [];
5252 for (var nSubKey = 0; nSubKey < 16; nSubKey++) {
5253 // Create subkey
5254 var subKey = subKeys[nSubKey] = [];
5255
5256 // Shortcut
5257 var bitShift = BIT_SHIFTS[nSubKey];
5258
5259 // Select 48 bits according to PC2
5260 for (var i = 0; i < 24; i++) {
5261 // Select from the left 28 key bits
5262 subKey[(i / 6) | 0] |= keyBits[((PC2[i] - 1) + bitShift) % 28] << (31 - i % 6);
5263
5264 // Select from the right 28 key bits
5265 subKey[4 + ((i / 6) | 0)] |= keyBits[28 + (((PC2[i + 24] - 1) + bitShift) % 28)] << (31 - i % 6);
5266 }
5267
5268 // Since each subkey is applied to an expanded 32-bit input,
5269 // the subkey can be broken into 8 values scaled to 32-bits,
5270 // which allows the key to be used without expansion
5271 subKey[0] = (subKey[0] << 1) | (subKey[0] >>> 31);
5272 for (var i = 1; i < 7; i++) {
5273 subKey[i] = subKey[i] >>> ((i - 1) * 4 + 3);
5274 }
5275 subKey[7] = (subKey[7] << 5) | (subKey[7] >>> 27);
5276 }
5277
5278 // Compute inverse subkeys
5279 var invSubKeys = this._invSubKeys = [];
5280 for (var i = 0; i < 16; i++) {
5281 invSubKeys[i] = subKeys[15 - i];
5282 }
5283 },
5284
5285 encryptBlock: function (M, offset) {
5286 this._doCryptBlock(M, offset, this._subKeys);
5287 },
5288
5289 decryptBlock: function (M, offset) {
5290 this._doCryptBlock(M, offset, this._invSubKeys);
5291 },
5292
5293 _doCryptBlock: function (M, offset, subKeys) {
5294 // Get input
5295 this._lBlock = M[offset];
5296 this._rBlock = M[offset + 1];
5297
5298 // Initial permutation
5299 exchangeLR.call(this, 4, 0x0f0f0f0f);
5300 exchangeLR.call(this, 16, 0x0000ffff);
5301 exchangeRL.call(this, 2, 0x33333333);
5302 exchangeRL.call(this, 8, 0x00ff00ff);
5303 exchangeLR.call(this, 1, 0x55555555);
5304
5305 // Rounds
5306 for (var round = 0; round < 16; round++) {
5307 // Shortcuts
5308 var subKey = subKeys[round];
5309 var lBlock = this._lBlock;
5310 var rBlock = this._rBlock;
5311
5312 // Feistel function
5313 var f = 0;
5314 for (var i = 0; i < 8; i++) {
5315 f |= SBOX_P[i][((rBlock ^ subKey[i]) & SBOX_MASK[i]) >>> 0];
5316 }
5317 this._lBlock = rBlock;
5318 this._rBlock = lBlock ^ f;
5319 }
5320
5321 // Undo swap from last round
5322 var t = this._lBlock;
5323 this._lBlock = this._rBlock;
5324 this._rBlock = t;
5325
5326 // Final permutation
5327 exchangeLR.call(this, 1, 0x55555555);
5328 exchangeRL.call(this, 8, 0x00ff00ff);
5329 exchangeRL.call(this, 2, 0x33333333);
5330 exchangeLR.call(this, 16, 0x0000ffff);
5331 exchangeLR.call(this, 4, 0x0f0f0f0f);
5332
5333 // Set output
5334 M[offset] = this._lBlock;
5335 M[offset + 1] = this._rBlock;
5336 },
5337
5338 keySize: 64/32,
5339
5340 ivSize: 64/32,
5341
5342 blockSize: 64/32
5343 });
5344
5345 // Swap bits across the left and right words
5346 function exchangeLR(offset, mask) {
5347 var t = ((this._lBlock >>> offset) ^ this._rBlock) & mask;
5348 this._rBlock ^= t;
5349 this._lBlock ^= t << offset;
5350 }
5351
5352 function exchangeRL(offset, mask) {
5353 var t = ((this._rBlock >>> offset) ^ this._lBlock) & mask;
5354 this._lBlock ^= t;
5355 this._rBlock ^= t << offset;
5356 }
5357
5358 /**
5359 * Shortcut functions to the cipher's object interface.
5360 *
5361 * @example
5362 *
5363 * var ciphertext = CryptoJS.DES.encrypt(message, key, cfg);
5364 * var plaintext = CryptoJS.DES.decrypt(ciphertext, key, cfg);
5365 */
5366 C.DES = BlockCipher._createHelper(DES);
5367
5368 /**
5369 * Triple-DES block cipher algorithm.
5370 */
5371 var TripleDES = C_algo.TripleDES = BlockCipher.extend({
5372 _doReset: function () {
5373 // Shortcuts
5374 var key = this._key;
5375 var keyWords = key.words;
5376 // Make sure the key length is valid (64, 128 or >= 192 bit)
5377 if (keyWords.length !== 2 && keyWords.length !== 4 && keyWords.length < 6) {
5378 throw new Error('Invalid key length - 3DES requires the key length to be 64, 128, 192 or >192.');
5379 }
5380
5381 // Extend the key according to the keying options defined in 3DES standard
5382 var key1 = keyWords.slice(0, 2);
5383 var key2 = keyWords.length < 4 ? keyWords.slice(0, 2) : keyWords.slice(2, 4);
5384 var key3 = keyWords.length < 6 ? keyWords.slice(0, 2) : keyWords.slice(4, 6);
5385
5386 // Create DES instances
5387 this._des1 = DES.createEncryptor(WordArray.create(key1));
5388 this._des2 = DES.createEncryptor(WordArray.create(key2));
5389 this._des3 = DES.createEncryptor(WordArray.create(key3));
5390 },
5391
5392 encryptBlock: function (M, offset) {
5393 this._des1.encryptBlock(M, offset);
5394 this._des2.decryptBlock(M, offset);
5395 this._des3.encryptBlock(M, offset);
5396 },
5397
5398 decryptBlock: function (M, offset) {
5399 this._des3.decryptBlock(M, offset);
5400 this._des2.encryptBlock(M, offset);
5401 this._des1.decryptBlock(M, offset);
5402 },
5403
5404 keySize: 192/32,
5405
5406 ivSize: 64/32,
5407
5408 blockSize: 64/32
5409 });
5410
5411 /**
5412 * Shortcut functions to the cipher's object interface.
5413 *
5414 * @example
5415 *
5416 * var ciphertext = CryptoJS.TripleDES.encrypt(message, key, cfg);
5417 * var plaintext = CryptoJS.TripleDES.decrypt(ciphertext, key, cfg);
5418 */
5419 C.TripleDES = BlockCipher._createHelper(TripleDES);
5420 }());
5421
5422
5423 (function () {
5424 // Shortcuts
5425 var C = CryptoJS;
5426 var C_lib = C.lib;
5427 var StreamCipher = C_lib.StreamCipher;
5428 var C_algo = C.algo;
5429
5430 /**
5431 * RC4 stream cipher algorithm.
5432 */
5433 var RC4 = C_algo.RC4 = StreamCipher.extend({
5434 _doReset: function () {
5435 // Shortcuts
5436 var key = this._key;
5437 var keyWords = key.words;
5438 var keySigBytes = key.sigBytes;
5439
5440 // Init sbox
5441 var S = this._S = [];
5442 for (var i = 0; i < 256; i++) {
5443 S[i] = i;
5444 }
5445
5446 // Key setup
5447 for (var i = 0, j = 0; i < 256; i++) {
5448 var keyByteIndex = i % keySigBytes;
5449 var keyByte = (keyWords[keyByteIndex >>> 2] >>> (24 - (keyByteIndex % 4) * 8)) & 0xff;
5450
5451 j = (j + S[i] + keyByte) % 256;
5452
5453 // Swap
5454 var t = S[i];
5455 S[i] = S[j];
5456 S[j] = t;
5457 }
5458
5459 // Counters
5460 this._i = this._j = 0;
5461 },
5462
5463 _doProcessBlock: function (M, offset) {
5464 M[offset] ^= generateKeystreamWord.call(this);
5465 },
5466
5467 keySize: 256/32,
5468
5469 ivSize: 0
5470 });
5471
5472 function generateKeystreamWord() {
5473 // Shortcuts
5474 var S = this._S;
5475 var i = this._i;
5476 var j = this._j;
5477
5478 // Generate keystream word
5479 var keystreamWord = 0;
5480 for (var n = 0; n < 4; n++) {
5481 i = (i + 1) % 256;
5482 j = (j + S[i]) % 256;
5483
5484 // Swap
5485 var t = S[i];
5486 S[i] = S[j];
5487 S[j] = t;
5488
5489 keystreamWord |= S[(S[i] + S[j]) % 256] << (24 - n * 8);
5490 }
5491
5492 // Update counters
5493 this._i = i;
5494 this._j = j;
5495
5496 return keystreamWord;
5497 }
5498
5499 /**
5500 * Shortcut functions to the cipher's object interface.
5501 *
5502 * @example
5503 *
5504 * var ciphertext = CryptoJS.RC4.encrypt(message, key, cfg);
5505 * var plaintext = CryptoJS.RC4.decrypt(ciphertext, key, cfg);
5506 */
5507 C.RC4 = StreamCipher._createHelper(RC4);
5508
5509 /**
5510 * Modified RC4 stream cipher algorithm.
5511 */
5512 var RC4Drop = C_algo.RC4Drop = RC4.extend({
5513 /**
5514 * Configuration options.
5515 *
5516 * @property {number} drop The number of keystream words to drop. Default 192
5517 */
5518 cfg: RC4.cfg.extend({
5519 drop: 192
5520 }),
5521
5522 _doReset: function () {
5523 RC4._doReset.call(this);
5524
5525 // Drop
5526 for (var i = this.cfg.drop; i > 0; i--) {
5527 generateKeystreamWord.call(this);
5528 }
5529 }
5530 });
5531
5532 /**
5533 * Shortcut functions to the cipher's object interface.
5534 *
5535 * @example
5536 *
5537 * var ciphertext = CryptoJS.RC4Drop.encrypt(message, key, cfg);
5538 * var plaintext = CryptoJS.RC4Drop.decrypt(ciphertext, key, cfg);
5539 */
5540 C.RC4Drop = StreamCipher._createHelper(RC4Drop);
5541 }());
5542
5543
5544 /** @preserve
5545 * Counter block mode compatible with Dr Brian Gladman fileenc.c
5546 * derived from CryptoJS.mode.CTR
5547 * Jan Hruby jhruby.web@gmail.com
5548 */
5549 CryptoJS.mode.CTRGladman = (function () {
5550 var CTRGladman = CryptoJS.lib.BlockCipherMode.extend();
5551
5552 function incWord(word)
5553 {
5554 if (((word >> 24) & 0xff) === 0xff) { //overflow
5555 var b1 = (word >> 16)&0xff;
5556 var b2 = (word >> 8)&0xff;
5557 var b3 = word & 0xff;
5558
5559 if (b1 === 0xff) // overflow b1
5560 {
5561 b1 = 0;
5562 if (b2 === 0xff)
5563 {
5564 b2 = 0;
5565 if (b3 === 0xff)
5566 {
5567 b3 = 0;
5568 }
5569 else
5570 {
5571 ++b3;
5572 }
5573 }
5574 else
5575 {
5576 ++b2;
5577 }
5578 }
5579 else
5580 {
5581 ++b1;
5582 }
5583
5584 word = 0;
5585 word += (b1 << 16);
5586 word += (b2 << 8);
5587 word += b3;
5588 }
5589 else
5590 {
5591 word += (0x01 << 24);
5592 }
5593 return word;
5594 }
5595
5596 function incCounter(counter)
5597 {
5598 if ((counter[0] = incWord(counter[0])) === 0)
5599 {
5600 // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8
5601 counter[1] = incWord(counter[1]);
5602 }
5603 return counter;
5604 }
5605
5606 var Encryptor = CTRGladman.Encryptor = CTRGladman.extend({
5607 processBlock: function (words, offset) {
5608 // Shortcuts
5609 var cipher = this._cipher
5610 var blockSize = cipher.blockSize;
5611 var iv = this._iv;
5612 var counter = this._counter;
5613
5614 // Generate keystream
5615 if (iv) {
5616 counter = this._counter = iv.slice(0);
5617
5618 // Remove IV for subsequent blocks
5619 this._iv = undefined;
5620 }
5621
5622 incCounter(counter);
5623
5624 var keystream = counter.slice(0);
5625 cipher.encryptBlock(keystream, 0);
5626
5627 // Encrypt
5628 for (var i = 0; i < blockSize; i++) {
5629 words[offset + i] ^= keystream[i];
5630 }
5631 }
5632 });
5633
5634 CTRGladman.Decryptor = Encryptor;
5635
5636 return CTRGladman;
5637 }());
5638
5639
5640
5641
5642 (function () {
5643 // Shortcuts
5644 var C = CryptoJS;
5645 var C_lib = C.lib;
5646 var StreamCipher = C_lib.StreamCipher;
5647 var C_algo = C.algo;
5648
5649 // Reusable objects
5650 var S = [];
5651 var C_ = [];
5652 var G = [];
5653
5654 /**
5655 * Rabbit stream cipher algorithm
5656 */
5657 var Rabbit = C_algo.Rabbit = StreamCipher.extend({
5658 _doReset: function () {
5659 // Shortcuts
5660 var K = this._key.words;
5661 var iv = this.cfg.iv;
5662
5663 // Swap endian
5664 for (var i = 0; i < 4; i++) {
5665 K[i] = (((K[i] << 8) | (K[i] >>> 24)) & 0x00ff00ff) |
5666 (((K[i] << 24) | (K[i] >>> 8)) & 0xff00ff00);
5667 }
5668
5669 // Generate initial state values
5670 var X = this._X = [
5671 K[0], (K[3] << 16) | (K[2] >>> 16),
5672 K[1], (K[0] << 16) | (K[3] >>> 16),
5673 K[2], (K[1] << 16) | (K[0] >>> 16),
5674 K[3], (K[2] << 16) | (K[1] >>> 16)
5675 ];
5676
5677 // Generate initial counter values
5678 var C = this._C = [
5679 (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
5680 (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
5681 (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
5682 (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
5683 ];
5684
5685 // Carry bit
5686 this._b = 0;
5687
5688 // Iterate the system four times
5689 for (var i = 0; i < 4; i++) {
5690 nextState.call(this);
5691 }
5692
5693 // Modify the counters
5694 for (var i = 0; i < 8; i++) {
5695 C[i] ^= X[(i + 4) & 7];
5696 }
5697
5698 // IV setup
5699 if (iv) {
5700 // Shortcuts
5701 var IV = iv.words;
5702 var IV_0 = IV[0];
5703 var IV_1 = IV[1];
5704
5705 // Generate four subvectors
5706 var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
5707 var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
5708 var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
5709 var i3 = (i2 << 16) | (i0 & 0x0000ffff);
5710
5711 // Modify counter values
5712 C[0] ^= i0;
5713 C[1] ^= i1;
5714 C[2] ^= i2;
5715 C[3] ^= i3;
5716 C[4] ^= i0;
5717 C[5] ^= i1;
5718 C[6] ^= i2;
5719 C[7] ^= i3;
5720
5721 // Iterate the system four times
5722 for (var i = 0; i < 4; i++) {
5723 nextState.call(this);
5724 }
5725 }
5726 },
5727
5728 _doProcessBlock: function (M, offset) {
5729 // Shortcut
5730 var X = this._X;
5731
5732 // Iterate the system
5733 nextState.call(this);
5734
5735 // Generate four keystream words
5736 S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
5737 S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
5738 S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
5739 S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
5740
5741 for (var i = 0; i < 4; i++) {
5742 // Swap endian
5743 S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
5744 (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
5745
5746 // Encrypt
5747 M[offset + i] ^= S[i];
5748 }
5749 },
5750
5751 blockSize: 128/32,
5752
5753 ivSize: 64/32
5754 });
5755
5756 function nextState() {
5757 // Shortcuts
5758 var X = this._X;
5759 var C = this._C;
5760
5761 // Save old counter values
5762 for (var i = 0; i < 8; i++) {
5763 C_[i] = C[i];
5764 }
5765
5766 // Calculate new counter values
5767 C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
5768 C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
5769 C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
5770 C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
5771 C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
5772 C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
5773 C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
5774 C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
5775 this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
5776
5777 // Calculate the g-values
5778 for (var i = 0; i < 8; i++) {
5779 var gx = X[i] + C[i];
5780
5781 // Construct high and low argument for squaring
5782 var ga = gx & 0xffff;
5783 var gb = gx >>> 16;
5784
5785 // Calculate high and low result of squaring
5786 var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
5787 var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
5788
5789 // High XOR low
5790 G[i] = gh ^ gl;
5791 }
5792
5793 // Calculate new state values
5794 X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
5795 X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
5796 X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
5797 X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
5798 X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
5799 X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
5800 X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
5801 X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
5802 }
5803
5804 /**
5805 * Shortcut functions to the cipher's object interface.
5806 *
5807 * @example
5808 *
5809 * var ciphertext = CryptoJS.Rabbit.encrypt(message, key, cfg);
5810 * var plaintext = CryptoJS.Rabbit.decrypt(ciphertext, key, cfg);
5811 */
5812 C.Rabbit = StreamCipher._createHelper(Rabbit);
5813 }());
5814
5815
5816 /**
5817 * Counter block mode.
5818 */
5819 CryptoJS.mode.CTR = (function () {
5820 var CTR = CryptoJS.lib.BlockCipherMode.extend();
5821
5822 var Encryptor = CTR.Encryptor = CTR.extend({
5823 processBlock: function (words, offset) {
5824 // Shortcuts
5825 var cipher = this._cipher
5826 var blockSize = cipher.blockSize;
5827 var iv = this._iv;
5828 var counter = this._counter;
5829
5830 // Generate keystream
5831 if (iv) {
5832 counter = this._counter = iv.slice(0);
5833
5834 // Remove IV for subsequent blocks
5835 this._iv = undefined;
5836 }
5837 var keystream = counter.slice(0);
5838 cipher.encryptBlock(keystream, 0);
5839
5840 // Increment counter
5841 counter[blockSize - 1] = (counter[blockSize - 1] + 1) | 0
5842
5843 // Encrypt
5844 for (var i = 0; i < blockSize; i++) {
5845 words[offset + i] ^= keystream[i];
5846 }
5847 }
5848 });
5849
5850 CTR.Decryptor = Encryptor;
5851
5852 return CTR;
5853 }());
5854
5855
5856 (function () {
5857 // Shortcuts
5858 var C = CryptoJS;
5859 var C_lib = C.lib;
5860 var StreamCipher = C_lib.StreamCipher;
5861 var C_algo = C.algo;
5862
5863 // Reusable objects
5864 var S = [];
5865 var C_ = [];
5866 var G = [];
5867
5868 /**
5869 * Rabbit stream cipher algorithm.
5870 *
5871 * This is a legacy version that neglected to convert the key to little-endian.
5872 * This error doesn't affect the cipher's security,
5873 * but it does affect its compatibility with other implementations.
5874 */
5875 var RabbitLegacy = C_algo.RabbitLegacy = StreamCipher.extend({
5876 _doReset: function () {
5877 // Shortcuts
5878 var K = this._key.words;
5879 var iv = this.cfg.iv;
5880
5881 // Generate initial state values
5882 var X = this._X = [
5883 K[0], (K[3] << 16) | (K[2] >>> 16),
5884 K[1], (K[0] << 16) | (K[3] >>> 16),
5885 K[2], (K[1] << 16) | (K[0] >>> 16),
5886 K[3], (K[2] << 16) | (K[1] >>> 16)
5887 ];
5888
5889 // Generate initial counter values
5890 var C = this._C = [
5891 (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
5892 (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
5893 (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
5894 (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
5895 ];
5896
5897 // Carry bit
5898 this._b = 0;
5899
5900 // Iterate the system four times
5901 for (var i = 0; i < 4; i++) {
5902 nextState.call(this);
5903 }
5904
5905 // Modify the counters
5906 for (var i = 0; i < 8; i++) {
5907 C[i] ^= X[(i + 4) & 7];
5908 }
5909
5910 // IV setup
5911 if (iv) {
5912 // Shortcuts
5913 var IV = iv.words;
5914 var IV_0 = IV[0];
5915 var IV_1 = IV[1];
5916
5917 // Generate four subvectors
5918 var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
5919 var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
5920 var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
5921 var i3 = (i2 << 16) | (i0 & 0x0000ffff);
5922
5923 // Modify counter values
5924 C[0] ^= i0;
5925 C[1] ^= i1;
5926 C[2] ^= i2;
5927 C[3] ^= i3;
5928 C[4] ^= i0;
5929 C[5] ^= i1;
5930 C[6] ^= i2;
5931 C[7] ^= i3;
5932
5933 // Iterate the system four times
5934 for (var i = 0; i < 4; i++) {
5935 nextState.call(this);
5936 }
5937 }
5938 },
5939
5940 _doProcessBlock: function (M, offset) {
5941 // Shortcut
5942 var X = this._X;
5943
5944 // Iterate the system
5945 nextState.call(this);
5946
5947 // Generate four keystream words
5948 S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
5949 S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
5950 S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
5951 S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
5952
5953 for (var i = 0; i < 4; i++) {
5954 // Swap endian
5955 S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
5956 (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
5957
5958 // Encrypt
5959 M[offset + i] ^= S[i];
5960 }
5961 },
5962
5963 blockSize: 128/32,
5964
5965 ivSize: 64/32
5966 });
5967
5968 function nextState() {
5969 // Shortcuts
5970 var X = this._X;
5971 var C = this._C;
5972
5973 // Save old counter values
5974 for (var i = 0; i < 8; i++) {
5975 C_[i] = C[i];
5976 }
5977
5978 // Calculate new counter values
5979 C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
5980 C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
5981 C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
5982 C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
5983 C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
5984 C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
5985 C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
5986 C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
5987 this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
5988
5989 // Calculate the g-values
5990 for (var i = 0; i < 8; i++) {
5991 var gx = X[i] + C[i];
5992
5993 // Construct high and low argument for squaring
5994 var ga = gx & 0xffff;
5995 var gb = gx >>> 16;
5996
5997 // Calculate high and low result of squaring
5998 var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
5999 var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
6000
6001 // High XOR low
6002 G[i] = gh ^ gl;
6003 }
6004
6005 // Calculate new state values
6006 X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
6007 X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
6008 X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
6009 X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
6010 X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
6011 X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
6012 X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
6013 X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
6014 }
6015
6016 /**
6017 * Shortcut functions to the cipher's object interface.
6018 *
6019 * @example
6020 *
6021 * var ciphertext = CryptoJS.RabbitLegacy.encrypt(message, key, cfg);
6022 * var plaintext = CryptoJS.RabbitLegacy.decrypt(ciphertext, key, cfg);
6023 */
6024 C.RabbitLegacy = StreamCipher._createHelper(RabbitLegacy);
6025 }());
6026
6027
6028 /**
6029 * Zero padding strategy.
6030 */
6031 CryptoJS.pad.ZeroPadding = {
6032 pad: function (data, blockSize) {
6033 // Shortcut
6034 var blockSizeBytes = blockSize * 4;
6035
6036 // Pad
6037 data.clamp();
6038 data.sigBytes += blockSizeBytes - ((data.sigBytes % blockSizeBytes) || blockSizeBytes);
6039 },
6040
6041 unpad: function (data) {
6042 // Shortcut
6043 var dataWords = data.words;
6044
6045 // Unpad
6046 var i = data.sigBytes - 1;
6047 for (var i = data.sigBytes - 1; i >= 0; i--) {
6048 if (((dataWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff)) {
6049 data.sigBytes = i + 1;
6050 break;
6051 }
6052 }
6053 }
6054 };
6055
6056
6057 return CryptoJS;
6058
6059}));