comparison scripts/minifier/otr/dep/crypto.js @ 12:1596660ddf72

Add minifier script for otr.js and its dependencies
author souliane <souliane@mailoo.org>
date Wed, 03 Sep 2014 19:38:05 +0200
parents
children
comparison
equal deleted inserted replaced
11:4920c8da790b 12:1596660ddf72
1 ;(function (root, factory) {
2
3 if (typeof define === "function" && define.amd) {
4 define(factory)
5 } else if (typeof module !== 'undefined' && module.exports) {
6 module.exports = factory()
7 } else {
8 root.CryptoJS = factory()
9 }
10
11 }(this, function () {
12
13 /*
14 CryptoJS v3.1.2
15 code.google.com/p/crypto-js
16 (c) 2009-2013 by Jeff Mott. All rights reserved.
17 code.google.com/p/crypto-js/wiki/License
18 */
19 /**
20 * CryptoJS core components.
21 */
22 var CryptoJS = CryptoJS || (function (Math, undefined) {
23 /**
24 * CryptoJS namespace.
25 */
26 var C = {};
27
28 /**
29 * Library namespace.
30 */
31 var C_lib = C.lib = {};
32
33 /**
34 * Base object for prototypal inheritance.
35 */
36 var Base = C_lib.Base = (function () {
37 function F() {}
38
39 return {
40 /**
41 * Creates a new object that inherits from this object.
42 *
43 * @param {Object} overrides Properties to copy into the new object.
44 *
45 * @return {Object} The new object.
46 *
47 * @static
48 *
49 * @example
50 *
51 * var MyType = CryptoJS.lib.Base.extend({
52 * field: 'value',
53 *
54 * method: function () {
55 * }
56 * });
57 */
58 extend: function (overrides) {
59 // Spawn
60 F.prototype = this;
61 var subtype = new F();
62
63 // Augment
64 if (overrides) {
65 subtype.mixIn(overrides);
66 }
67
68 // Create default initializer
69 if (!subtype.hasOwnProperty('init')) {
70 subtype.init = function () {
71 subtype.$super.init.apply(this, arguments);
72 };
73 }
74
75 // Initializer's prototype is the subtype object
76 subtype.init.prototype = subtype;
77
78 // Reference supertype
79 subtype.$super = this;
80
81 return subtype;
82 },
83
84 /**
85 * Extends this object and runs the init method.
86 * Arguments to create() will be passed to init().
87 *
88 * @return {Object} The new object.
89 *
90 * @static
91 *
92 * @example
93 *
94 * var instance = MyType.create();
95 */
96 create: function () {
97 var instance = this.extend();
98 instance.init.apply(instance, arguments);
99
100 return instance;
101 },
102
103 /**
104 * Initializes a newly created object.
105 * Override this method to add some logic when your objects are created.
106 *
107 * @example
108 *
109 * var MyType = CryptoJS.lib.Base.extend({
110 * init: function () {
111 * // ...
112 * }
113 * });
114 */
115 init: function () {
116 },
117
118 /**
119 * Copies properties into this object.
120 *
121 * @param {Object} properties The properties to mix in.
122 *
123 * @example
124 *
125 * MyType.mixIn({
126 * field: 'value'
127 * });
128 */
129 mixIn: function (properties) {
130 for (var propertyName in properties) {
131 if (properties.hasOwnProperty(propertyName)) {
132 this[propertyName] = properties[propertyName];
133 }
134 }
135
136 // IE won't copy toString using the loop above
137 if (properties.hasOwnProperty('toString')) {
138 this.toString = properties.toString;
139 }
140 },
141
142 /**
143 * Creates a copy of this object.
144 *
145 * @return {Object} The clone.
146 *
147 * @example
148 *
149 * var clone = instance.clone();
150 */
151 clone: function () {
152 return this.init.prototype.extend(this);
153 }
154 };
155 }());
156
157 /**
158 * An array of 32-bit words.
159 *
160 * @property {Array} words The array of 32-bit words.
161 * @property {number} sigBytes The number of significant bytes in this word array.
162 */
163 var WordArray = C_lib.WordArray = Base.extend({
164 /**
165 * Initializes a newly created word array.
166 *
167 * @param {Array} words (Optional) An array of 32-bit words.
168 * @param {number} sigBytes (Optional) The number of significant bytes in the words.
169 *
170 * @example
171 *
172 * var wordArray = CryptoJS.lib.WordArray.create();
173 * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
174 * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
175 */
176 init: function (words, sigBytes) {
177 words = this.words = words || [];
178
179 if (sigBytes != undefined) {
180 this.sigBytes = sigBytes;
181 } else {
182 this.sigBytes = words.length * 4;
183 }
184 },
185
186 /**
187 * Converts this word array to a string.
188 *
189 * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
190 *
191 * @return {string} The stringified word array.
192 *
193 * @example
194 *
195 * var string = wordArray + '';
196 * var string = wordArray.toString();
197 * var string = wordArray.toString(CryptoJS.enc.Utf8);
198 */
199 toString: function (encoder) {
200 return (encoder || Hex).stringify(this);
201 },
202
203 /**
204 * Concatenates a word array to this word array.
205 *
206 * @param {WordArray} wordArray The word array to append.
207 *
208 * @return {WordArray} This word array.
209 *
210 * @example
211 *
212 * wordArray1.concat(wordArray2);
213 */
214 concat: function (wordArray) {
215 // Shortcuts
216 var thisWords = this.words;
217 var thatWords = wordArray.words;
218 var thisSigBytes = this.sigBytes;
219 var thatSigBytes = wordArray.sigBytes;
220
221 // Clamp excess bits
222 this.clamp();
223
224 // Concat
225 if (thisSigBytes % 4) {
226 // Copy one byte at a time
227 for (var i = 0; i < thatSigBytes; i++) {
228 var thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
229 thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8);
230 }
231 } else if (thatWords.length > 0xffff) {
232 // Copy one word at a time
233 for (var i = 0; i < thatSigBytes; i += 4) {
234 thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2];
235 }
236 } else {
237 // Copy all words at once
238 thisWords.push.apply(thisWords, thatWords);
239 }
240 this.sigBytes += thatSigBytes;
241
242 // Chainable
243 return this;
244 },
245
246 /**
247 * Removes insignificant bits.
248 *
249 * @example
250 *
251 * wordArray.clamp();
252 */
253 clamp: function () {
254 // Shortcuts
255 var words = this.words;
256 var sigBytes = this.sigBytes;
257
258 // Clamp
259 words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8);
260 words.length = Math.ceil(sigBytes / 4);
261 },
262
263 /**
264 * Creates a copy of this word array.
265 *
266 * @return {WordArray} The clone.
267 *
268 * @example
269 *
270 * var clone = wordArray.clone();
271 */
272 clone: function () {
273 var clone = Base.clone.call(this);
274 clone.words = this.words.slice(0);
275
276 return clone;
277 },
278
279 /**
280 * Creates a word array filled with random bytes.
281 *
282 * @param {number} nBytes The number of random bytes to generate.
283 *
284 * @return {WordArray} The random word array.
285 *
286 * @static
287 *
288 * @example
289 *
290 * var wordArray = CryptoJS.lib.WordArray.random(16);
291 */
292 random: function (nBytes) {
293 var words = [];
294 for (var i = 0; i < nBytes; i += 4) {
295 words.push((Math.random() * 0x100000000) | 0);
296 }
297
298 return new WordArray.init(words, nBytes);
299 }
300 });
301
302 /**
303 * Encoder namespace.
304 */
305 var C_enc = C.enc = {};
306
307 /**
308 * Hex encoding strategy.
309 */
310 var Hex = C_enc.Hex = {
311 /**
312 * Converts a word array to a hex string.
313 *
314 * @param {WordArray} wordArray The word array.
315 *
316 * @return {string} The hex string.
317 *
318 * @static
319 *
320 * @example
321 *
322 * var hexString = CryptoJS.enc.Hex.stringify(wordArray);
323 */
324 stringify: function (wordArray) {
325 // Shortcuts
326 var words = wordArray.words;
327 var sigBytes = wordArray.sigBytes;
328
329 // Convert
330 var hexChars = [];
331 for (var i = 0; i < sigBytes; i++) {
332 var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
333 hexChars.push((bite >>> 4).toString(16));
334 hexChars.push((bite & 0x0f).toString(16));
335 }
336
337 return hexChars.join('');
338 },
339
340 /**
341 * Converts a hex string to a word array.
342 *
343 * @param {string} hexStr The hex string.
344 *
345 * @return {WordArray} The word array.
346 *
347 * @static
348 *
349 * @example
350 *
351 * var wordArray = CryptoJS.enc.Hex.parse(hexString);
352 */
353 parse: function (hexStr) {
354 // Shortcut
355 var hexStrLength = hexStr.length;
356
357 // Convert
358 var words = [];
359 for (var i = 0; i < hexStrLength; i += 2) {
360 words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
361 }
362
363 return new WordArray.init(words, hexStrLength / 2);
364 }
365 };
366
367 /**
368 * Latin1 encoding strategy.
369 */
370 var Latin1 = C_enc.Latin1 = {
371 /**
372 * Converts a word array to a Latin1 string.
373 *
374 * @param {WordArray} wordArray The word array.
375 *
376 * @return {string} The Latin1 string.
377 *
378 * @static
379 *
380 * @example
381 *
382 * var latin1String = CryptoJS.enc.Latin1.stringify(wordArray);
383 */
384 stringify: function (wordArray) {
385 // Shortcuts
386 var words = wordArray.words;
387 var sigBytes = wordArray.sigBytes;
388
389 // Convert
390 var latin1Chars = [];
391 for (var i = 0; i < sigBytes; i++) {
392 var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
393 latin1Chars.push(String.fromCharCode(bite));
394 }
395
396 return latin1Chars.join('');
397 },
398
399 /**
400 * Converts a Latin1 string to a word array.
401 *
402 * @param {string} latin1Str The Latin1 string.
403 *
404 * @return {WordArray} The word array.
405 *
406 * @static
407 *
408 * @example
409 *
410 * var wordArray = CryptoJS.enc.Latin1.parse(latin1String);
411 */
412 parse: function (latin1Str) {
413 // Shortcut
414 var latin1StrLength = latin1Str.length;
415
416 // Convert
417 var words = [];
418 for (var i = 0; i < latin1StrLength; i++) {
419 words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
420 }
421
422 return new WordArray.init(words, latin1StrLength);
423 }
424 };
425
426 /**
427 * UTF-8 encoding strategy.
428 */
429 var Utf8 = C_enc.Utf8 = {
430 /**
431 * Converts a word array to a UTF-8 string.
432 *
433 * @param {WordArray} wordArray The word array.
434 *
435 * @return {string} The UTF-8 string.
436 *
437 * @static
438 *
439 * @example
440 *
441 * var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
442 */
443 stringify: function (wordArray) {
444 try {
445 return decodeURIComponent(escape(Latin1.stringify(wordArray)));
446 } catch (e) {
447 throw new Error('Malformed UTF-8 data');
448 }
449 },
450
451 /**
452 * Converts a UTF-8 string to a word array.
453 *
454 * @param {string} utf8Str The UTF-8 string.
455 *
456 * @return {WordArray} The word array.
457 *
458 * @static
459 *
460 * @example
461 *
462 * var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
463 */
464 parse: function (utf8Str) {
465 return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
466 }
467 };
468
469 /**
470 * Abstract buffered block algorithm template.
471 *
472 * The property blockSize must be implemented in a concrete subtype.
473 *
474 * @property {number} _minBufferSize The number of blocks that should be kept unprocessed in the buffer. Default: 0
475 */
476 var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm = Base.extend({
477 /**
478 * Resets this block algorithm's data buffer to its initial state.
479 *
480 * @example
481 *
482 * bufferedBlockAlgorithm.reset();
483 */
484 reset: function () {
485 // Initial values
486 this._data = new WordArray.init();
487 this._nDataBytes = 0;
488 },
489
490 /**
491 * Adds new data to this block algorithm's buffer.
492 *
493 * @param {WordArray|string} data The data to append. Strings are converted to a WordArray using UTF-8.
494 *
495 * @example
496 *
497 * bufferedBlockAlgorithm._append('data');
498 * bufferedBlockAlgorithm._append(wordArray);
499 */
500 _append: function (data) {
501 // Convert string to WordArray, else assume WordArray already
502 if (typeof data == 'string') {
503 data = Utf8.parse(data);
504 }
505
506 // Append
507 this._data.concat(data);
508 this._nDataBytes += data.sigBytes;
509 },
510
511 /**
512 * Processes available data blocks.
513 *
514 * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
515 *
516 * @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
517 *
518 * @return {WordArray} The processed data.
519 *
520 * @example
521 *
522 * var processedData = bufferedBlockAlgorithm._process();
523 * var processedData = bufferedBlockAlgorithm._process(!!'flush');
524 */
525 _process: function (doFlush) {
526 // Shortcuts
527 var data = this._data;
528 var dataWords = data.words;
529 var dataSigBytes = data.sigBytes;
530 var blockSize = this.blockSize;
531 var blockSizeBytes = blockSize * 4;
532
533 // Count blocks ready
534 var nBlocksReady = dataSigBytes / blockSizeBytes;
535 if (doFlush) {
536 // Round up to include partial blocks
537 nBlocksReady = Math.ceil(nBlocksReady);
538 } else {
539 // Round down to include only full blocks,
540 // less the number of blocks that must remain in the buffer
541 nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
542 }
543
544 // Count words ready
545 var nWordsReady = nBlocksReady * blockSize;
546
547 // Count bytes ready
548 var nBytesReady = Math.min(nWordsReady * 4, dataSigBytes);
549
550 // Process blocks
551 if (nWordsReady) {
552 for (var offset = 0; offset < nWordsReady; offset += blockSize) {
553 // Perform concrete-algorithm logic
554 this._doProcessBlock(dataWords, offset);
555 }
556
557 // Remove processed words
558 var processedWords = dataWords.splice(0, nWordsReady);
559 data.sigBytes -= nBytesReady;
560 }
561
562 // Return processed words
563 return new WordArray.init(processedWords, nBytesReady);
564 },
565
566 /**
567 * Creates a copy of this object.
568 *
569 * @return {Object} The clone.
570 *
571 * @example
572 *
573 * var clone = bufferedBlockAlgorithm.clone();
574 */
575 clone: function () {
576 var clone = Base.clone.call(this);
577 clone._data = this._data.clone();
578
579 return clone;
580 },
581
582 _minBufferSize: 0
583 });
584
585 /**
586 * Abstract hasher template.
587 *
588 * @property {number} blockSize The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
589 */
590 var Hasher = C_lib.Hasher = BufferedBlockAlgorithm.extend({
591 /**
592 * Configuration options.
593 */
594 cfg: Base.extend(),
595
596 /**
597 * Initializes a newly created hasher.
598 *
599 * @param {Object} cfg (Optional) The configuration options to use for this hash computation.
600 *
601 * @example
602 *
603 * var hasher = CryptoJS.algo.SHA256.create();
604 */
605 init: function (cfg) {
606 // Apply config defaults
607 this.cfg = this.cfg.extend(cfg);
608
609 // Set initial values
610 this.reset();
611 },
612
613 /**
614 * Resets this hasher to its initial state.
615 *
616 * @example
617 *
618 * hasher.reset();
619 */
620 reset: function () {
621 // Reset data buffer
622 BufferedBlockAlgorithm.reset.call(this);
623
624 // Perform concrete-hasher logic
625 this._doReset();
626 },
627
628 /**
629 * Updates this hasher with a message.
630 *
631 * @param {WordArray|string} messageUpdate The message to append.
632 *
633 * @return {Hasher} This hasher.
634 *
635 * @example
636 *
637 * hasher.update('message');
638 * hasher.update(wordArray);
639 */
640 update: function (messageUpdate) {
641 // Append
642 this._append(messageUpdate);
643
644 // Update the hash
645 this._process();
646
647 // Chainable
648 return this;
649 },
650
651 /**
652 * Finalizes the hash computation.
653 * Note that the finalize operation is effectively a destructive, read-once operation.
654 *
655 * @param {WordArray|string} messageUpdate (Optional) A final message update.
656 *
657 * @return {WordArray} The hash.
658 *
659 * @example
660 *
661 * var hash = hasher.finalize();
662 * var hash = hasher.finalize('message');
663 * var hash = hasher.finalize(wordArray);
664 */
665 finalize: function (messageUpdate) {
666 // Final message update
667 if (messageUpdate) {
668 this._append(messageUpdate);
669 }
670
671 // Perform concrete-hasher logic
672 var hash = this._doFinalize();
673
674 return hash;
675 },
676
677 blockSize: 512/32,
678
679 /**
680 * Creates a shortcut function to a hasher's object interface.
681 *
682 * @param {Hasher} hasher The hasher to create a helper for.
683 *
684 * @return {Function} The shortcut function.
685 *
686 * @static
687 *
688 * @example
689 *
690 * var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
691 */
692 _createHelper: function (hasher) {
693 return function (message, cfg) {
694 return new hasher.init(cfg).finalize(message);
695 };
696 },
697
698 /**
699 * Creates a shortcut function to the HMAC's object interface.
700 *
701 * @param {Hasher} hasher The hasher to use in this HMAC helper.
702 *
703 * @return {Function} The shortcut function.
704 *
705 * @static
706 *
707 * @example
708 *
709 * var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
710 */
711 _createHmacHelper: function (hasher) {
712 return function (message, key) {
713 return new C_algo.HMAC.init(hasher, key).finalize(message);
714 };
715 }
716 });
717
718 /**
719 * Algorithm namespace.
720 */
721 var C_algo = C.algo = {};
722
723 return C;
724 }(Math));
725
726 /*
727 CryptoJS v3.1.2
728 code.google.com/p/crypto-js
729 (c) 2009-2013 by Jeff Mott. All rights reserved.
730 code.google.com/p/crypto-js/wiki/License
731 */
732 (function () {
733 // Shortcuts
734 var C = CryptoJS;
735 var C_lib = C.lib;
736 var WordArray = C_lib.WordArray;
737 var C_enc = C.enc;
738
739 /**
740 * Base64 encoding strategy.
741 */
742 var Base64 = C_enc.Base64 = {
743 /**
744 * Converts a word array to a Base64 string.
745 *
746 * @param {WordArray} wordArray The word array.
747 *
748 * @return {string} The Base64 string.
749 *
750 * @static
751 *
752 * @example
753 *
754 * var base64String = CryptoJS.enc.Base64.stringify(wordArray);
755 */
756 stringify: function (wordArray) {
757 // Shortcuts
758 var words = wordArray.words;
759 var sigBytes = wordArray.sigBytes;
760 var map = this._map;
761
762 // Clamp excess bits
763 wordArray.clamp();
764
765 // Convert
766 var base64Chars = [];
767 for (var i = 0; i < sigBytes; i += 3) {
768 var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
769 var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
770 var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
771
772 var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
773
774 for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
775 base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
776 }
777 }
778
779 // Add padding
780 var paddingChar = map.charAt(64);
781 if (paddingChar) {
782 while (base64Chars.length % 4) {
783 base64Chars.push(paddingChar);
784 }
785 }
786
787 return base64Chars.join('');
788 },
789
790 /**
791 * Converts a Base64 string to a word array.
792 *
793 * @param {string} base64Str The Base64 string.
794 *
795 * @return {WordArray} The word array.
796 *
797 * @static
798 *
799 * @example
800 *
801 * var wordArray = CryptoJS.enc.Base64.parse(base64String);
802 */
803 parse: function (base64Str) {
804 // Shortcuts
805 var base64StrLength = base64Str.length;
806 var map = this._map;
807
808 // Ignore padding
809 var paddingChar = map.charAt(64);
810 if (paddingChar) {
811 var paddingIndex = base64Str.indexOf(paddingChar);
812 if (paddingIndex != -1) {
813 base64StrLength = paddingIndex;
814 }
815 }
816
817 // Convert
818 var words = [];
819 var nBytes = 0;
820 for (var i = 0; i < base64StrLength; i++) {
821 if (i % 4) {
822 var bits1 = map.indexOf(base64Str.charAt(i - 1)) << ((i % 4) * 2);
823 var bits2 = map.indexOf(base64Str.charAt(i)) >>> (6 - (i % 4) * 2);
824 words[nBytes >>> 2] |= (bits1 | bits2) << (24 - (nBytes % 4) * 8);
825 nBytes++;
826 }
827 }
828
829 return WordArray.create(words, nBytes);
830 },
831
832 _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
833 };
834 }());
835
836 /*
837 CryptoJS v3.1.2
838 code.google.com/p/crypto-js
839 (c) 2009-2013 by Jeff Mott. All rights reserved.
840 code.google.com/p/crypto-js/wiki/License
841 */
842 /**
843 * Cipher core components.
844 */
845 CryptoJS.lib.Cipher || (function (undefined) {
846 // Shortcuts
847 var C = CryptoJS;
848 var C_lib = C.lib;
849 var Base = C_lib.Base;
850 var WordArray = C_lib.WordArray;
851 var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm;
852 var C_enc = C.enc;
853 var Utf8 = C_enc.Utf8;
854 var Base64 = C_enc.Base64;
855 var C_algo = C.algo;
856 var EvpKDF = C_algo.EvpKDF;
857
858 /**
859 * Abstract base cipher template.
860 *
861 * @property {number} keySize This cipher's key size. Default: 4 (128 bits)
862 * @property {number} ivSize This cipher's IV size. Default: 4 (128 bits)
863 * @property {number} _ENC_XFORM_MODE A constant representing encryption mode.
864 * @property {number} _DEC_XFORM_MODE A constant representing decryption mode.
865 */
866 var Cipher = C_lib.Cipher = BufferedBlockAlgorithm.extend({
867 /**
868 * Configuration options.
869 *
870 * @property {WordArray} iv The IV to use for this operation.
871 */
872 cfg: Base.extend(),
873
874 /**
875 * Creates this cipher in encryption mode.
876 *
877 * @param {WordArray} key The key.
878 * @param {Object} cfg (Optional) The configuration options to use for this operation.
879 *
880 * @return {Cipher} A cipher instance.
881 *
882 * @static
883 *
884 * @example
885 *
886 * var cipher = CryptoJS.algo.AES.createEncryptor(keyWordArray, { iv: ivWordArray });
887 */
888 createEncryptor: function (key, cfg) {
889 return this.create(this._ENC_XFORM_MODE, key, cfg);
890 },
891
892 /**
893 * Creates this cipher in decryption mode.
894 *
895 * @param {WordArray} key The key.
896 * @param {Object} cfg (Optional) The configuration options to use for this operation.
897 *
898 * @return {Cipher} A cipher instance.
899 *
900 * @static
901 *
902 * @example
903 *
904 * var cipher = CryptoJS.algo.AES.createDecryptor(keyWordArray, { iv: ivWordArray });
905 */
906 createDecryptor: function (key, cfg) {
907 return this.create(this._DEC_XFORM_MODE, key, cfg);
908 },
909
910 /**
911 * Initializes a newly created cipher.
912 *
913 * @param {number} xformMode Either the encryption or decryption transormation mode constant.
914 * @param {WordArray} key The key.
915 * @param {Object} cfg (Optional) The configuration options to use for this operation.
916 *
917 * @example
918 *
919 * var cipher = CryptoJS.algo.AES.create(CryptoJS.algo.AES._ENC_XFORM_MODE, keyWordArray, { iv: ivWordArray });
920 */
921 init: function (xformMode, key, cfg) {
922 // Apply config defaults
923 this.cfg = this.cfg.extend(cfg);
924
925 // Store transform mode and key
926 this._xformMode = xformMode;
927 this._key = key;
928
929 // Set initial values
930 this.reset();
931 },
932
933 /**
934 * Resets this cipher to its initial state.
935 *
936 * @example
937 *
938 * cipher.reset();
939 */
940 reset: function () {
941 // Reset data buffer
942 BufferedBlockAlgorithm.reset.call(this);
943
944 // Perform concrete-cipher logic
945 this._doReset();
946 },
947
948 /**
949 * Adds data to be encrypted or decrypted.
950 *
951 * @param {WordArray|string} dataUpdate The data to encrypt or decrypt.
952 *
953 * @return {WordArray} The data after processing.
954 *
955 * @example
956 *
957 * var encrypted = cipher.process('data');
958 * var encrypted = cipher.process(wordArray);
959 */
960 process: function (dataUpdate) {
961 // Append
962 this._append(dataUpdate);
963
964 // Process available blocks
965 return this._process();
966 },
967
968 /**
969 * Finalizes the encryption or decryption process.
970 * Note that the finalize operation is effectively a destructive, read-once operation.
971 *
972 * @param {WordArray|string} dataUpdate The final data to encrypt or decrypt.
973 *
974 * @return {WordArray} The data after final processing.
975 *
976 * @example
977 *
978 * var encrypted = cipher.finalize();
979 * var encrypted = cipher.finalize('data');
980 * var encrypted = cipher.finalize(wordArray);
981 */
982 finalize: function (dataUpdate) {
983 // Final data update
984 if (dataUpdate) {
985 this._append(dataUpdate);
986 }
987
988 // Perform concrete-cipher logic
989 var finalProcessedData = this._doFinalize();
990
991 return finalProcessedData;
992 },
993
994 keySize: 128/32,
995
996 ivSize: 128/32,
997
998 _ENC_XFORM_MODE: 1,
999
1000 _DEC_XFORM_MODE: 2,
1001
1002 /**
1003 * Creates shortcut functions to a cipher's object interface.
1004 *
1005 * @param {Cipher} cipher The cipher to create a helper for.
1006 *
1007 * @return {Object} An object with encrypt and decrypt shortcut functions.
1008 *
1009 * @static
1010 *
1011 * @example
1012 *
1013 * var AES = CryptoJS.lib.Cipher._createHelper(CryptoJS.algo.AES);
1014 */
1015 _createHelper: (function () {
1016 function selectCipherStrategy(key) {
1017 if (typeof key == 'string') {
1018 return PasswordBasedCipher;
1019 } else {
1020 return SerializableCipher;
1021 }
1022 }
1023
1024 return function (cipher) {
1025 return {
1026 encrypt: function (message, key, cfg) {
1027 return selectCipherStrategy(key).encrypt(cipher, message, key, cfg);
1028 },
1029
1030 decrypt: function (ciphertext, key, cfg) {
1031 return selectCipherStrategy(key).decrypt(cipher, ciphertext, key, cfg);
1032 }
1033 };
1034 };
1035 }())
1036 });
1037
1038 /**
1039 * Abstract base stream cipher template.
1040 *
1041 * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 1 (32 bits)
1042 */
1043 var StreamCipher = C_lib.StreamCipher = Cipher.extend({
1044 _doFinalize: function () {
1045 // Process partial blocks
1046 var finalProcessedBlocks = this._process(!!'flush');
1047
1048 return finalProcessedBlocks;
1049 },
1050
1051 blockSize: 1
1052 });
1053
1054 /**
1055 * Mode namespace.
1056 */
1057 var C_mode = C.mode = {};
1058
1059 /**
1060 * Abstract base block cipher mode template.
1061 */
1062 var BlockCipherMode = C_lib.BlockCipherMode = Base.extend({
1063 /**
1064 * Creates this mode for encryption.
1065 *
1066 * @param {Cipher} cipher A block cipher instance.
1067 * @param {Array} iv The IV words.
1068 *
1069 * @static
1070 *
1071 * @example
1072 *
1073 * var mode = CryptoJS.mode.CBC.createEncryptor(cipher, iv.words);
1074 */
1075 createEncryptor: function (cipher, iv) {
1076 return this.Encryptor.create(cipher, iv);
1077 },
1078
1079 /**
1080 * Creates this mode for decryption.
1081 *
1082 * @param {Cipher} cipher A block cipher instance.
1083 * @param {Array} iv The IV words.
1084 *
1085 * @static
1086 *
1087 * @example
1088 *
1089 * var mode = CryptoJS.mode.CBC.createDecryptor(cipher, iv.words);
1090 */
1091 createDecryptor: function (cipher, iv) {
1092 return this.Decryptor.create(cipher, iv);
1093 },
1094
1095 /**
1096 * Initializes a newly created mode.
1097 *
1098 * @param {Cipher} cipher A block cipher instance.
1099 * @param {Array} iv The IV words.
1100 *
1101 * @example
1102 *
1103 * var mode = CryptoJS.mode.CBC.Encryptor.create(cipher, iv.words);
1104 */
1105 init: function (cipher, iv) {
1106 this._cipher = cipher;
1107 this._iv = iv;
1108 }
1109 });
1110
1111 /**
1112 * Cipher Block Chaining mode.
1113 */
1114 var CBC = C_mode.CBC = (function () {
1115 /**
1116 * Abstract base CBC mode.
1117 */
1118 var CBC = BlockCipherMode.extend();
1119
1120 /**
1121 * CBC encryptor.
1122 */
1123 CBC.Encryptor = CBC.extend({
1124 /**
1125 * Processes the data block at offset.
1126 *
1127 * @param {Array} words The data words to operate on.
1128 * @param {number} offset The offset where the block starts.
1129 *
1130 * @example
1131 *
1132 * mode.processBlock(data.words, offset);
1133 */
1134 processBlock: function (words, offset) {
1135 // Shortcuts
1136 var cipher = this._cipher;
1137 var blockSize = cipher.blockSize;
1138
1139 // XOR and encrypt
1140 xorBlock.call(this, words, offset, blockSize);
1141 cipher.encryptBlock(words, offset);
1142
1143 // Remember this block to use with next block
1144 this._prevBlock = words.slice(offset, offset + blockSize);
1145 }
1146 });
1147
1148 /**
1149 * CBC decryptor.
1150 */
1151 CBC.Decryptor = CBC.extend({
1152 /**
1153 * Processes the data block at offset.
1154 *
1155 * @param {Array} words The data words to operate on.
1156 * @param {number} offset The offset where the block starts.
1157 *
1158 * @example
1159 *
1160 * mode.processBlock(data.words, offset);
1161 */
1162 processBlock: function (words, offset) {
1163 // Shortcuts
1164 var cipher = this._cipher;
1165 var blockSize = cipher.blockSize;
1166
1167 // Remember this block to use with next block
1168 var thisBlock = words.slice(offset, offset + blockSize);
1169
1170 // Decrypt and XOR
1171 cipher.decryptBlock(words, offset);
1172 xorBlock.call(this, words, offset, blockSize);
1173
1174 // This block becomes the previous block
1175 this._prevBlock = thisBlock;
1176 }
1177 });
1178
1179 function xorBlock(words, offset, blockSize) {
1180 // Shortcut
1181 var iv = this._iv;
1182
1183 // Choose mixing block
1184 if (iv) {
1185 var block = iv;
1186
1187 // Remove IV for subsequent blocks
1188 this._iv = undefined;
1189 } else {
1190 var block = this._prevBlock;
1191 }
1192
1193 // XOR blocks
1194 for (var i = 0; i < blockSize; i++) {
1195 words[offset + i] ^= block[i];
1196 }
1197 }
1198
1199 return CBC;
1200 }());
1201
1202 /**
1203 * Padding namespace.
1204 */
1205 var C_pad = C.pad = {};
1206
1207 /**
1208 * PKCS #5/7 padding strategy.
1209 */
1210 var Pkcs7 = C_pad.Pkcs7 = {
1211 /**
1212 * Pads data using the algorithm defined in PKCS #5/7.
1213 *
1214 * @param {WordArray} data The data to pad.
1215 * @param {number} blockSize The multiple that the data should be padded to.
1216 *
1217 * @static
1218 *
1219 * @example
1220 *
1221 * CryptoJS.pad.Pkcs7.pad(wordArray, 4);
1222 */
1223 pad: function (data, blockSize) {
1224 // Shortcut
1225 var blockSizeBytes = blockSize * 4;
1226
1227 // Count padding bytes
1228 var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
1229
1230 // Create padding word
1231 var paddingWord = (nPaddingBytes << 24) | (nPaddingBytes << 16) | (nPaddingBytes << 8) | nPaddingBytes;
1232
1233 // Create padding
1234 var paddingWords = [];
1235 for (var i = 0; i < nPaddingBytes; i += 4) {
1236 paddingWords.push(paddingWord);
1237 }
1238 var padding = WordArray.create(paddingWords, nPaddingBytes);
1239
1240 // Add padding
1241 data.concat(padding);
1242 },
1243
1244 /**
1245 * Unpads data that had been padded using the algorithm defined in PKCS #5/7.
1246 *
1247 * @param {WordArray} data The data to unpad.
1248 *
1249 * @static
1250 *
1251 * @example
1252 *
1253 * CryptoJS.pad.Pkcs7.unpad(wordArray);
1254 */
1255 unpad: function (data) {
1256 // Get number of padding bytes from last byte
1257 var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
1258
1259 // Remove padding
1260 data.sigBytes -= nPaddingBytes;
1261 }
1262 };
1263
1264 /**
1265 * Abstract base block cipher template.
1266 *
1267 * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 4 (128 bits)
1268 */
1269 var BlockCipher = C_lib.BlockCipher = Cipher.extend({
1270 /**
1271 * Configuration options.
1272 *
1273 * @property {Mode} mode The block mode to use. Default: CBC
1274 * @property {Padding} padding The padding strategy to use. Default: Pkcs7
1275 */
1276 cfg: Cipher.cfg.extend({
1277 mode: CBC,
1278 padding: Pkcs7
1279 }),
1280
1281 reset: function () {
1282 // Reset cipher
1283 Cipher.reset.call(this);
1284
1285 // Shortcuts
1286 var cfg = this.cfg;
1287 var iv = cfg.iv;
1288 var mode = cfg.mode;
1289
1290 // Reset block mode
1291 if (this._xformMode == this._ENC_XFORM_MODE) {
1292 var modeCreator = mode.createEncryptor;
1293 } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
1294 var modeCreator = mode.createDecryptor;
1295
1296 // Keep at least one block in the buffer for unpadding
1297 this._minBufferSize = 1;
1298 }
1299 this._mode = modeCreator.call(mode, this, iv && iv.words);
1300 },
1301
1302 _doProcessBlock: function (words, offset) {
1303 this._mode.processBlock(words, offset);
1304 },
1305
1306 _doFinalize: function () {
1307 // Shortcut
1308 var padding = this.cfg.padding;
1309
1310 // Finalize
1311 if (this._xformMode == this._ENC_XFORM_MODE) {
1312 // Pad data
1313 padding.pad(this._data, this.blockSize);
1314
1315 // Process final blocks
1316 var finalProcessedBlocks = this._process(!!'flush');
1317 } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
1318 // Process final blocks
1319 var finalProcessedBlocks = this._process(!!'flush');
1320
1321 // Unpad data
1322 padding.unpad(finalProcessedBlocks);
1323 }
1324
1325 return finalProcessedBlocks;
1326 },
1327
1328 blockSize: 128/32
1329 });
1330
1331 /**
1332 * A collection of cipher parameters.
1333 *
1334 * @property {WordArray} ciphertext The raw ciphertext.
1335 * @property {WordArray} key The key to this ciphertext.
1336 * @property {WordArray} iv The IV used in the ciphering operation.
1337 * @property {WordArray} salt The salt used with a key derivation function.
1338 * @property {Cipher} algorithm The cipher algorithm.
1339 * @property {Mode} mode The block mode used in the ciphering operation.
1340 * @property {Padding} padding The padding scheme used in the ciphering operation.
1341 * @property {number} blockSize The block size of the cipher.
1342 * @property {Format} formatter The default formatting strategy to convert this cipher params object to a string.
1343 */
1344 var CipherParams = C_lib.CipherParams = Base.extend({
1345 /**
1346 * Initializes a newly created cipher params object.
1347 *
1348 * @param {Object} cipherParams An object with any of the possible cipher parameters.
1349 *
1350 * @example
1351 *
1352 * var cipherParams = CryptoJS.lib.CipherParams.create({
1353 * ciphertext: ciphertextWordArray,
1354 * key: keyWordArray,
1355 * iv: ivWordArray,
1356 * salt: saltWordArray,
1357 * algorithm: CryptoJS.algo.AES,
1358 * mode: CryptoJS.mode.CBC,
1359 * padding: CryptoJS.pad.PKCS7,
1360 * blockSize: 4,
1361 * formatter: CryptoJS.format.OpenSSL
1362 * });
1363 */
1364 init: function (cipherParams) {
1365 this.mixIn(cipherParams);
1366 },
1367
1368 /**
1369 * Converts this cipher params object to a string.
1370 *
1371 * @param {Format} formatter (Optional) The formatting strategy to use.
1372 *
1373 * @return {string} The stringified cipher params.
1374 *
1375 * @throws Error If neither the formatter nor the default formatter is set.
1376 *
1377 * @example
1378 *
1379 * var string = cipherParams + '';
1380 * var string = cipherParams.toString();
1381 * var string = cipherParams.toString(CryptoJS.format.OpenSSL);
1382 */
1383 toString: function (formatter) {
1384 return (formatter || this.formatter).stringify(this);
1385 }
1386 });
1387
1388 /**
1389 * Format namespace.
1390 */
1391 var C_format = C.format = {};
1392
1393 /**
1394 * OpenSSL formatting strategy.
1395 */
1396 var OpenSSLFormatter = C_format.OpenSSL = {
1397 /**
1398 * Converts a cipher params object to an OpenSSL-compatible string.
1399 *
1400 * @param {CipherParams} cipherParams The cipher params object.
1401 *
1402 * @return {string} The OpenSSL-compatible string.
1403 *
1404 * @static
1405 *
1406 * @example
1407 *
1408 * var openSSLString = CryptoJS.format.OpenSSL.stringify(cipherParams);
1409 */
1410 stringify: function (cipherParams) {
1411 // Shortcuts
1412 var ciphertext = cipherParams.ciphertext;
1413 var salt = cipherParams.salt;
1414
1415 // Format
1416 if (salt) {
1417 var wordArray = WordArray.create([0x53616c74, 0x65645f5f]).concat(salt).concat(ciphertext);
1418 } else {
1419 var wordArray = ciphertext;
1420 }
1421
1422 return wordArray.toString(Base64);
1423 },
1424
1425 /**
1426 * Converts an OpenSSL-compatible string to a cipher params object.
1427 *
1428 * @param {string} openSSLStr The OpenSSL-compatible string.
1429 *
1430 * @return {CipherParams} The cipher params object.
1431 *
1432 * @static
1433 *
1434 * @example
1435 *
1436 * var cipherParams = CryptoJS.format.OpenSSL.parse(openSSLString);
1437 */
1438 parse: function (openSSLStr) {
1439 // Parse base64
1440 var ciphertext = Base64.parse(openSSLStr);
1441
1442 // Shortcut
1443 var ciphertextWords = ciphertext.words;
1444
1445 // Test for salt
1446 if (ciphertextWords[0] == 0x53616c74 && ciphertextWords[1] == 0x65645f5f) {
1447 // Extract salt
1448 var salt = WordArray.create(ciphertextWords.slice(2, 4));
1449
1450 // Remove salt from ciphertext
1451 ciphertextWords.splice(0, 4);
1452 ciphertext.sigBytes -= 16;
1453 }
1454
1455 return CipherParams.create({ ciphertext: ciphertext, salt: salt });
1456 }
1457 };
1458
1459 /**
1460 * A cipher wrapper that returns ciphertext as a serializable cipher params object.
1461 */
1462 var SerializableCipher = C_lib.SerializableCipher = Base.extend({
1463 /**
1464 * Configuration options.
1465 *
1466 * @property {Formatter} format The formatting strategy to convert cipher param objects to and from a string. Default: OpenSSL
1467 */
1468 cfg: Base.extend({
1469 format: OpenSSLFormatter
1470 }),
1471
1472 /**
1473 * Encrypts a message.
1474 *
1475 * @param {Cipher} cipher The cipher algorithm to use.
1476 * @param {WordArray|string} message The message to encrypt.
1477 * @param {WordArray} key The key.
1478 * @param {Object} cfg (Optional) The configuration options to use for this operation.
1479 *
1480 * @return {CipherParams} A cipher params object.
1481 *
1482 * @static
1483 *
1484 * @example
1485 *
1486 * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key);
1487 * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv });
1488 * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv, format: CryptoJS.format.OpenSSL });
1489 */
1490 encrypt: function (cipher, message, key, cfg) {
1491 // Apply config defaults
1492 cfg = this.cfg.extend(cfg);
1493
1494 // Encrypt
1495 var encryptor = cipher.createEncryptor(key, cfg);
1496 var ciphertext = encryptor.finalize(message);
1497
1498 // Shortcut
1499 var cipherCfg = encryptor.cfg;
1500
1501 // Create and return serializable cipher params
1502 return CipherParams.create({
1503 ciphertext: ciphertext,
1504 key: key,
1505 iv: cipherCfg.iv,
1506 algorithm: cipher,
1507 mode: cipherCfg.mode,
1508 padding: cipherCfg.padding,
1509 blockSize: cipher.blockSize,
1510 formatter: cfg.format
1511 });
1512 },
1513
1514 /**
1515 * Decrypts serialized ciphertext.
1516 *
1517 * @param {Cipher} cipher The cipher algorithm to use.
1518 * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
1519 * @param {WordArray} key The key.
1520 * @param {Object} cfg (Optional) The configuration options to use for this operation.
1521 *
1522 * @return {WordArray} The plaintext.
1523 *
1524 * @static
1525 *
1526 * @example
1527 *
1528 * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, key, { iv: iv, format: CryptoJS.format.OpenSSL });
1529 * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, key, { iv: iv, format: CryptoJS.format.OpenSSL });
1530 */
1531 decrypt: function (cipher, ciphertext, key, cfg) {
1532 // Apply config defaults
1533 cfg = this.cfg.extend(cfg);
1534
1535 // Convert string to CipherParams
1536 ciphertext = this._parse(ciphertext, cfg.format);
1537
1538 // Decrypt
1539 var plaintext = cipher.createDecryptor(key, cfg).finalize(ciphertext.ciphertext);
1540
1541 return plaintext;
1542 },
1543
1544 /**
1545 * Converts serialized ciphertext to CipherParams,
1546 * else assumed CipherParams already and returns ciphertext unchanged.
1547 *
1548 * @param {CipherParams|string} ciphertext The ciphertext.
1549 * @param {Formatter} format The formatting strategy to use to parse serialized ciphertext.
1550 *
1551 * @return {CipherParams} The unserialized ciphertext.
1552 *
1553 * @static
1554 *
1555 * @example
1556 *
1557 * var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
1558 */
1559 _parse: function (ciphertext, format) {
1560 if (typeof ciphertext == 'string') {
1561 return format.parse(ciphertext, this);
1562 } else {
1563 return ciphertext;
1564 }
1565 }
1566 });
1567
1568 /**
1569 * Key derivation function namespace.
1570 */
1571 var C_kdf = C.kdf = {};
1572
1573 /**
1574 * OpenSSL key derivation function.
1575 */
1576 var OpenSSLKdf = C_kdf.OpenSSL = {
1577 /**
1578 * Derives a key and IV from a password.
1579 *
1580 * @param {string} password The password to derive from.
1581 * @param {number} keySize The size in words of the key to generate.
1582 * @param {number} ivSize The size in words of the IV to generate.
1583 * @param {WordArray|string} salt (Optional) A 64-bit salt to use. If omitted, a salt will be generated randomly.
1584 *
1585 * @return {CipherParams} A cipher params object with the key, IV, and salt.
1586 *
1587 * @static
1588 *
1589 * @example
1590 *
1591 * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32);
1592 * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32, 'saltsalt');
1593 */
1594 execute: function (password, keySize, ivSize, salt) {
1595 // Generate random salt
1596 if (!salt) {
1597 salt = WordArray.random(64/8);
1598 }
1599
1600 // Derive key and IV
1601 var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);
1602
1603 // Separate key and IV
1604 var iv = WordArray.create(key.words.slice(keySize), ivSize * 4);
1605 key.sigBytes = keySize * 4;
1606
1607 // Return params
1608 return CipherParams.create({ key: key, iv: iv, salt: salt });
1609 }
1610 };
1611
1612 /**
1613 * A serializable cipher wrapper that derives the key from a password,
1614 * and returns ciphertext as a serializable cipher params object.
1615 */
1616 var PasswordBasedCipher = C_lib.PasswordBasedCipher = SerializableCipher.extend({
1617 /**
1618 * Configuration options.
1619 *
1620 * @property {KDF} kdf The key derivation function to use to generate a key and IV from a password. Default: OpenSSL
1621 */
1622 cfg: SerializableCipher.cfg.extend({
1623 kdf: OpenSSLKdf
1624 }),
1625
1626 /**
1627 * Encrypts a message using a password.
1628 *
1629 * @param {Cipher} cipher The cipher algorithm to use.
1630 * @param {WordArray|string} message The message to encrypt.
1631 * @param {string} password The password.
1632 * @param {Object} cfg (Optional) The configuration options to use for this operation.
1633 *
1634 * @return {CipherParams} A cipher params object.
1635 *
1636 * @static
1637 *
1638 * @example
1639 *
1640 * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password');
1641 * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password', { format: CryptoJS.format.OpenSSL });
1642 */
1643 encrypt: function (cipher, message, password, cfg) {
1644 // Apply config defaults
1645 cfg = this.cfg.extend(cfg);
1646
1647 // Derive key and other params
1648 var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize);
1649
1650 // Add IV to config
1651 cfg.iv = derivedParams.iv;
1652
1653 // Encrypt
1654 var ciphertext = SerializableCipher.encrypt.call(this, cipher, message, derivedParams.key, cfg);
1655
1656 // Mix in derived params
1657 ciphertext.mixIn(derivedParams);
1658
1659 return ciphertext;
1660 },
1661
1662 /**
1663 * Decrypts serialized ciphertext using a password.
1664 *
1665 * @param {Cipher} cipher The cipher algorithm to use.
1666 * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
1667 * @param {string} password The password.
1668 * @param {Object} cfg (Optional) The configuration options to use for this operation.
1669 *
1670 * @return {WordArray} The plaintext.
1671 *
1672 * @static
1673 *
1674 * @example
1675 *
1676 * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, 'password', { format: CryptoJS.format.OpenSSL });
1677 * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, 'password', { format: CryptoJS.format.OpenSSL });
1678 */
1679 decrypt: function (cipher, ciphertext, password, cfg) {
1680 // Apply config defaults
1681 cfg = this.cfg.extend(cfg);
1682
1683 // Convert string to CipherParams
1684 ciphertext = this._parse(ciphertext, cfg.format);
1685
1686 // Derive key and other params
1687 var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt);
1688
1689 // Add IV to config
1690 cfg.iv = derivedParams.iv;
1691
1692 // Decrypt
1693 var plaintext = SerializableCipher.decrypt.call(this, cipher, ciphertext, derivedParams.key, cfg);
1694
1695 return plaintext;
1696 }
1697 });
1698 }());
1699
1700 /*
1701 CryptoJS v3.1.2
1702 code.google.com/p/crypto-js
1703 (c) 2009-2013 by Jeff Mott. All rights reserved.
1704 code.google.com/p/crypto-js/wiki/License
1705 */
1706 (function () {
1707 // Shortcuts
1708 var C = CryptoJS;
1709 var C_lib = C.lib;
1710 var BlockCipher = C_lib.BlockCipher;
1711 var C_algo = C.algo;
1712
1713 // Lookup tables
1714 var SBOX = [];
1715 var INV_SBOX = [];
1716 var SUB_MIX_0 = [];
1717 var SUB_MIX_1 = [];
1718 var SUB_MIX_2 = [];
1719 var SUB_MIX_3 = [];
1720 var INV_SUB_MIX_0 = [];
1721 var INV_SUB_MIX_1 = [];
1722 var INV_SUB_MIX_2 = [];
1723 var INV_SUB_MIX_3 = [];
1724
1725 // Compute lookup tables
1726 (function () {
1727 // Compute double table
1728 var d = [];
1729 for (var i = 0; i < 256; i++) {
1730 if (i < 128) {
1731 d[i] = i << 1;
1732 } else {
1733 d[i] = (i << 1) ^ 0x11b;
1734 }
1735 }
1736
1737 // Walk GF(2^8)
1738 var x = 0;
1739 var xi = 0;
1740 for (var i = 0; i < 256; i++) {
1741 // Compute sbox
1742 var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
1743 sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
1744 SBOX[x] = sx;
1745 INV_SBOX[sx] = x;
1746
1747 // Compute multiplication
1748 var x2 = d[x];
1749 var x4 = d[x2];
1750 var x8 = d[x4];
1751
1752 // Compute sub bytes, mix columns tables
1753 var t = (d[sx] * 0x101) ^ (sx * 0x1010100);
1754 SUB_MIX_0[x] = (t << 24) | (t >>> 8);
1755 SUB_MIX_1[x] = (t << 16) | (t >>> 16);
1756 SUB_MIX_2[x] = (t << 8) | (t >>> 24);
1757 SUB_MIX_3[x] = t;
1758
1759 // Compute inv sub bytes, inv mix columns tables
1760 var t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
1761 INV_SUB_MIX_0[sx] = (t << 24) | (t >>> 8);
1762 INV_SUB_MIX_1[sx] = (t << 16) | (t >>> 16);
1763 INV_SUB_MIX_2[sx] = (t << 8) | (t >>> 24);
1764 INV_SUB_MIX_3[sx] = t;
1765
1766 // Compute next counter
1767 if (!x) {
1768 x = xi = 1;
1769 } else {
1770 x = x2 ^ d[d[d[x8 ^ x2]]];
1771 xi ^= d[d[xi]];
1772 }
1773 }
1774 }());
1775
1776 // Precomputed Rcon lookup
1777 var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
1778
1779 /**
1780 * AES block cipher algorithm.
1781 */
1782 var AES = C_algo.AES = BlockCipher.extend({
1783 _doReset: function () {
1784 // Shortcuts
1785 var key = this._key;
1786 var keyWords = key.words;
1787 var keySize = key.sigBytes / 4;
1788
1789 // Compute number of rounds
1790 var nRounds = this._nRounds = keySize + 6
1791
1792 // Compute number of key schedule rows
1793 var ksRows = (nRounds + 1) * 4;
1794
1795 // Compute key schedule
1796 var keySchedule = this._keySchedule = [];
1797 for (var ksRow = 0; ksRow < ksRows; ksRow++) {
1798 if (ksRow < keySize) {
1799 keySchedule[ksRow] = keyWords[ksRow];
1800 } else {
1801 var t = keySchedule[ksRow - 1];
1802
1803 if (!(ksRow % keySize)) {
1804 // Rot word
1805 t = (t << 8) | (t >>> 24);
1806
1807 // Sub word
1808 t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
1809
1810 // Mix Rcon
1811 t ^= RCON[(ksRow / keySize) | 0] << 24;
1812 } else if (keySize > 6 && ksRow % keySize == 4) {
1813 // Sub word
1814 t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
1815 }
1816
1817 keySchedule[ksRow] = keySchedule[ksRow - keySize] ^ t;
1818 }
1819 }
1820
1821 // Compute inv key schedule
1822 var invKeySchedule = this._invKeySchedule = [];
1823 for (var invKsRow = 0; invKsRow < ksRows; invKsRow++) {
1824 var ksRow = ksRows - invKsRow;
1825
1826 if (invKsRow % 4) {
1827 var t = keySchedule[ksRow];
1828 } else {
1829 var t = keySchedule[ksRow - 4];
1830 }
1831
1832 if (invKsRow < 4 || ksRow <= 4) {
1833 invKeySchedule[invKsRow] = t;
1834 } else {
1835 invKeySchedule[invKsRow] = INV_SUB_MIX_0[SBOX[t >>> 24]] ^ INV_SUB_MIX_1[SBOX[(t >>> 16) & 0xff]] ^
1836 INV_SUB_MIX_2[SBOX[(t >>> 8) & 0xff]] ^ INV_SUB_MIX_3[SBOX[t & 0xff]];
1837 }
1838 }
1839 },
1840
1841 encryptBlock: function (M, offset) {
1842 this._doCryptBlock(M, offset, this._keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX);
1843 },
1844
1845 decryptBlock: function (M, offset) {
1846 // Swap 2nd and 4th rows
1847 var t = M[offset + 1];
1848 M[offset + 1] = M[offset + 3];
1849 M[offset + 3] = t;
1850
1851 this._doCryptBlock(M, offset, this._invKeySchedule, INV_SUB_MIX_0, INV_SUB_MIX_1, INV_SUB_MIX_2, INV_SUB_MIX_3, INV_SBOX);
1852
1853 // Inv swap 2nd and 4th rows
1854 var t = M[offset + 1];
1855 M[offset + 1] = M[offset + 3];
1856 M[offset + 3] = t;
1857 },
1858
1859 _doCryptBlock: function (M, offset, keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX) {
1860 // Shortcut
1861 var nRounds = this._nRounds;
1862
1863 // Get input, add round key
1864 var s0 = M[offset] ^ keySchedule[0];
1865 var s1 = M[offset + 1] ^ keySchedule[1];
1866 var s2 = M[offset + 2] ^ keySchedule[2];
1867 var s3 = M[offset + 3] ^ keySchedule[3];
1868
1869 // Key schedule row counter
1870 var ksRow = 4;
1871
1872 // Rounds
1873 for (var round = 1; round < nRounds; round++) {
1874 // Shift rows, sub bytes, mix columns, add round key
1875 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++];
1876 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++];
1877 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++];
1878 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++];
1879
1880 // Update state
1881 s0 = t0;
1882 s1 = t1;
1883 s2 = t2;
1884 s3 = t3;
1885 }
1886
1887 // Shift rows, sub bytes, add round key
1888 var t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
1889 var t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
1890 var t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
1891 var t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
1892
1893 // Set output
1894 M[offset] = t0;
1895 M[offset + 1] = t1;
1896 M[offset + 2] = t2;
1897 M[offset + 3] = t3;
1898 },
1899
1900 keySize: 256/32
1901 });
1902
1903 /**
1904 * Shortcut functions to the cipher's object interface.
1905 *
1906 * @example
1907 *
1908 * var ciphertext = CryptoJS.AES.encrypt(message, key, cfg);
1909 * var plaintext = CryptoJS.AES.decrypt(ciphertext, key, cfg);
1910 */
1911 C.AES = BlockCipher._createHelper(AES);
1912 }());
1913
1914 /*
1915 CryptoJS v3.1.2
1916 code.google.com/p/crypto-js
1917 (c) 2009-2013 by Jeff Mott. All rights reserved.
1918 code.google.com/p/crypto-js/wiki/License
1919 */
1920 (function () {
1921 // Shortcuts
1922 var C = CryptoJS;
1923 var C_lib = C.lib;
1924 var WordArray = C_lib.WordArray;
1925 var Hasher = C_lib.Hasher;
1926 var C_algo = C.algo;
1927
1928 // Reusable object
1929 var W = [];
1930
1931 /**
1932 * SHA-1 hash algorithm.
1933 */
1934 var SHA1 = C_algo.SHA1 = Hasher.extend({
1935 _doReset: function () {
1936 this._hash = new WordArray.init([
1937 0x67452301, 0xefcdab89,
1938 0x98badcfe, 0x10325476,
1939 0xc3d2e1f0
1940 ]);
1941 },
1942
1943 _doProcessBlock: function (M, offset) {
1944 // Shortcut
1945 var H = this._hash.words;
1946
1947 // Working variables
1948 var a = H[0];
1949 var b = H[1];
1950 var c = H[2];
1951 var d = H[3];
1952 var e = H[4];
1953
1954 // Computation
1955 for (var i = 0; i < 80; i++) {
1956 if (i < 16) {
1957 W[i] = M[offset + i] | 0;
1958 } else {
1959 var n = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
1960 W[i] = (n << 1) | (n >>> 31);
1961 }
1962
1963 var t = ((a << 5) | (a >>> 27)) + e + W[i];
1964 if (i < 20) {
1965 t += ((b & c) | (~b & d)) + 0x5a827999;
1966 } else if (i < 40) {
1967 t += (b ^ c ^ d) + 0x6ed9eba1;
1968 } else if (i < 60) {
1969 t += ((b & c) | (b & d) | (c & d)) - 0x70e44324;
1970 } else /* if (i < 80) */ {
1971 t += (b ^ c ^ d) - 0x359d3e2a;
1972 }
1973
1974 e = d;
1975 d = c;
1976 c = (b << 30) | (b >>> 2);
1977 b = a;
1978 a = t;
1979 }
1980
1981 // Intermediate hash value
1982 H[0] = (H[0] + a) | 0;
1983 H[1] = (H[1] + b) | 0;
1984 H[2] = (H[2] + c) | 0;
1985 H[3] = (H[3] + d) | 0;
1986 H[4] = (H[4] + e) | 0;
1987 },
1988
1989 _doFinalize: function () {
1990 // Shortcuts
1991 var data = this._data;
1992 var dataWords = data.words;
1993
1994 var nBitsTotal = this._nDataBytes * 8;
1995 var nBitsLeft = data.sigBytes * 8;
1996
1997 // Add padding
1998 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
1999 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
2000 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
2001 data.sigBytes = dataWords.length * 4;
2002
2003 // Hash final blocks
2004 this._process();
2005
2006 // Return final computed hash
2007 return this._hash;
2008 },
2009
2010 clone: function () {
2011 var clone = Hasher.clone.call(this);
2012 clone._hash = this._hash.clone();
2013
2014 return clone;
2015 }
2016 });
2017
2018 /**
2019 * Shortcut function to the hasher's object interface.
2020 *
2021 * @param {WordArray|string} message The message to hash.
2022 *
2023 * @return {WordArray} The hash.
2024 *
2025 * @static
2026 *
2027 * @example
2028 *
2029 * var hash = CryptoJS.SHA1('message');
2030 * var hash = CryptoJS.SHA1(wordArray);
2031 */
2032 C.SHA1 = Hasher._createHelper(SHA1);
2033
2034 /**
2035 * Shortcut function to the HMAC's object interface.
2036 *
2037 * @param {WordArray|string} message The message to hash.
2038 * @param {WordArray|string} key The secret key.
2039 *
2040 * @return {WordArray} The HMAC.
2041 *
2042 * @static
2043 *
2044 * @example
2045 *
2046 * var hmac = CryptoJS.HmacSHA1(message, key);
2047 */
2048 C.HmacSHA1 = Hasher._createHmacHelper(SHA1);
2049 }());
2050
2051 /*
2052 CryptoJS v3.1.2
2053 code.google.com/p/crypto-js
2054 (c) 2009-2013 by Jeff Mott. All rights reserved.
2055 code.google.com/p/crypto-js/wiki/License
2056 */
2057 (function (Math) {
2058 // Shortcuts
2059 var C = CryptoJS;
2060 var C_lib = C.lib;
2061 var WordArray = C_lib.WordArray;
2062 var Hasher = C_lib.Hasher;
2063 var C_algo = C.algo;
2064
2065 // Initialization and round constants tables
2066 var H = [];
2067 var K = [];
2068
2069 // Compute constants
2070 (function () {
2071 function isPrime(n) {
2072 var sqrtN = Math.sqrt(n);
2073 for (var factor = 2; factor <= sqrtN; factor++) {
2074 if (!(n % factor)) {
2075 return false;
2076 }
2077 }
2078
2079 return true;
2080 }
2081
2082 function getFractionalBits(n) {
2083 return ((n - (n | 0)) * 0x100000000) | 0;
2084 }
2085
2086 var n = 2;
2087 var nPrime = 0;
2088 while (nPrime < 64) {
2089 if (isPrime(n)) {
2090 if (nPrime < 8) {
2091 H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2));
2092 }
2093 K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3));
2094
2095 nPrime++;
2096 }
2097
2098 n++;
2099 }
2100 }());
2101
2102 // Reusable object
2103 var W = [];
2104
2105 /**
2106 * SHA-256 hash algorithm.
2107 */
2108 var SHA256 = C_algo.SHA256 = Hasher.extend({
2109 _doReset: function () {
2110 this._hash = new WordArray.init(H.slice(0));
2111 },
2112
2113 _doProcessBlock: function (M, offset) {
2114 // Shortcut
2115 var H = this._hash.words;
2116
2117 // Working variables
2118 var a = H[0];
2119 var b = H[1];
2120 var c = H[2];
2121 var d = H[3];
2122 var e = H[4];
2123 var f = H[5];
2124 var g = H[6];
2125 var h = H[7];
2126
2127 // Computation
2128 for (var i = 0; i < 64; i++) {
2129 if (i < 16) {
2130 W[i] = M[offset + i] | 0;
2131 } else {
2132 var gamma0x = W[i - 15];
2133 var gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
2134 ((gamma0x << 14) | (gamma0x >>> 18)) ^
2135 (gamma0x >>> 3);
2136
2137 var gamma1x = W[i - 2];
2138 var gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
2139 ((gamma1x << 13) | (gamma1x >>> 19)) ^
2140 (gamma1x >>> 10);
2141
2142 W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
2143 }
2144
2145 var ch = (e & f) ^ (~e & g);
2146 var maj = (a & b) ^ (a & c) ^ (b & c);
2147
2148 var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
2149 var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
2150
2151 var t1 = h + sigma1 + ch + K[i] + W[i];
2152 var t2 = sigma0 + maj;
2153
2154 h = g;
2155 g = f;
2156 f = e;
2157 e = (d + t1) | 0;
2158 d = c;
2159 c = b;
2160 b = a;
2161 a = (t1 + t2) | 0;
2162 }
2163
2164 // Intermediate hash value
2165 H[0] = (H[0] + a) | 0;
2166 H[1] = (H[1] + b) | 0;
2167 H[2] = (H[2] + c) | 0;
2168 H[3] = (H[3] + d) | 0;
2169 H[4] = (H[4] + e) | 0;
2170 H[5] = (H[5] + f) | 0;
2171 H[6] = (H[6] + g) | 0;
2172 H[7] = (H[7] + h) | 0;
2173 },
2174
2175 _doFinalize: function () {
2176 // Shortcuts
2177 var data = this._data;
2178 var dataWords = data.words;
2179
2180 var nBitsTotal = this._nDataBytes * 8;
2181 var nBitsLeft = data.sigBytes * 8;
2182
2183 // Add padding
2184 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
2185 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
2186 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
2187 data.sigBytes = dataWords.length * 4;
2188
2189 // Hash final blocks
2190 this._process();
2191
2192 // Return final computed hash
2193 return this._hash;
2194 },
2195
2196 clone: function () {
2197 var clone = Hasher.clone.call(this);
2198 clone._hash = this._hash.clone();
2199
2200 return clone;
2201 }
2202 });
2203
2204 /**
2205 * Shortcut function to the hasher's object interface.
2206 *
2207 * @param {WordArray|string} message The message to hash.
2208 *
2209 * @return {WordArray} The hash.
2210 *
2211 * @static
2212 *
2213 * @example
2214 *
2215 * var hash = CryptoJS.SHA256('message');
2216 * var hash = CryptoJS.SHA256(wordArray);
2217 */
2218 C.SHA256 = Hasher._createHelper(SHA256);
2219
2220 /**
2221 * Shortcut function to the HMAC's object interface.
2222 *
2223 * @param {WordArray|string} message The message to hash.
2224 * @param {WordArray|string} key The secret key.
2225 *
2226 * @return {WordArray} The HMAC.
2227 *
2228 * @static
2229 *
2230 * @example
2231 *
2232 * var hmac = CryptoJS.HmacSHA256(message, key);
2233 */
2234 C.HmacSHA256 = Hasher._createHmacHelper(SHA256);
2235 }(Math));
2236
2237 /*
2238 CryptoJS v3.1.2
2239 code.google.com/p/crypto-js
2240 (c) 2009-2013 by Jeff Mott. All rights reserved.
2241 code.google.com/p/crypto-js/wiki/License
2242 */
2243 (function () {
2244 // Shortcuts
2245 var C = CryptoJS;
2246 var C_lib = C.lib;
2247 var Base = C_lib.Base;
2248 var C_enc = C.enc;
2249 var Utf8 = C_enc.Utf8;
2250 var C_algo = C.algo;
2251
2252 /**
2253 * HMAC algorithm.
2254 */
2255 var HMAC = C_algo.HMAC = Base.extend({
2256 /**
2257 * Initializes a newly created HMAC.
2258 *
2259 * @param {Hasher} hasher The hash algorithm to use.
2260 * @param {WordArray|string} key The secret key.
2261 *
2262 * @example
2263 *
2264 * var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
2265 */
2266 init: function (hasher, key) {
2267 // Init hasher
2268 hasher = this._hasher = new hasher.init();
2269
2270 // Convert string to WordArray, else assume WordArray already
2271 if (typeof key == 'string') {
2272 key = Utf8.parse(key);
2273 }
2274
2275 // Shortcuts
2276 var hasherBlockSize = hasher.blockSize;
2277 var hasherBlockSizeBytes = hasherBlockSize * 4;
2278
2279 // Allow arbitrary length keys
2280 if (key.sigBytes > hasherBlockSizeBytes) {
2281 key = hasher.finalize(key);
2282 }
2283
2284 // Clamp excess bits
2285 key.clamp();
2286
2287 // Clone key for inner and outer pads
2288 var oKey = this._oKey = key.clone();
2289 var iKey = this._iKey = key.clone();
2290
2291 // Shortcuts
2292 var oKeyWords = oKey.words;
2293 var iKeyWords = iKey.words;
2294
2295 // XOR keys with pad constants
2296 for (var i = 0; i < hasherBlockSize; i++) {
2297 oKeyWords[i] ^= 0x5c5c5c5c;
2298 iKeyWords[i] ^= 0x36363636;
2299 }
2300 oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
2301
2302 // Set initial values
2303 this.reset();
2304 },
2305
2306 /**
2307 * Resets this HMAC to its initial state.
2308 *
2309 * @example
2310 *
2311 * hmacHasher.reset();
2312 */
2313 reset: function () {
2314 // Shortcut
2315 var hasher = this._hasher;
2316
2317 // Reset
2318 hasher.reset();
2319 hasher.update(this._iKey);
2320 },
2321
2322 /**
2323 * Updates this HMAC with a message.
2324 *
2325 * @param {WordArray|string} messageUpdate The message to append.
2326 *
2327 * @return {HMAC} This HMAC instance.
2328 *
2329 * @example
2330 *
2331 * hmacHasher.update('message');
2332 * hmacHasher.update(wordArray);
2333 */
2334 update: function (messageUpdate) {
2335 this._hasher.update(messageUpdate);
2336
2337 // Chainable
2338 return this;
2339 },
2340
2341 /**
2342 * Finalizes the HMAC computation.
2343 * Note that the finalize operation is effectively a destructive, read-once operation.
2344 *
2345 * @param {WordArray|string} messageUpdate (Optional) A final message update.
2346 *
2347 * @return {WordArray} The HMAC.
2348 *
2349 * @example
2350 *
2351 * var hmac = hmacHasher.finalize();
2352 * var hmac = hmacHasher.finalize('message');
2353 * var hmac = hmacHasher.finalize(wordArray);
2354 */
2355 finalize: function (messageUpdate) {
2356 // Shortcut
2357 var hasher = this._hasher;
2358
2359 // Compute HMAC
2360 var innerHash = hasher.finalize(messageUpdate);
2361 hasher.reset();
2362 var hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
2363
2364 return hmac;
2365 }
2366 });
2367 }());
2368
2369 /*
2370 CryptoJS v3.1.2
2371 code.google.com/p/crypto-js
2372 (c) 2009-2013 by Jeff Mott. All rights reserved.
2373 code.google.com/p/crypto-js/wiki/License
2374 */
2375 /**
2376 * A noop padding strategy.
2377 */
2378 CryptoJS.pad.NoPadding = {
2379 pad: function () {
2380 },
2381
2382 unpad: function () {
2383 }
2384 };
2385
2386 /*
2387 CryptoJS v3.1.2
2388 code.google.com/p/crypto-js
2389 (c) 2009-2013 by Jeff Mott. All rights reserved.
2390 code.google.com/p/crypto-js/wiki/License
2391 */
2392 /**
2393 * Counter block mode.
2394 */
2395 CryptoJS.mode.CTR = (function () {
2396 var CTR = CryptoJS.lib.BlockCipherMode.extend();
2397
2398 var Encryptor = CTR.Encryptor = CTR.extend({
2399 processBlock: function (words, offset) {
2400 // Shortcuts
2401 var cipher = this._cipher
2402 var blockSize = cipher.blockSize;
2403 var iv = this._iv;
2404 var counter = this._counter;
2405
2406 // Generate keystream
2407 if (iv) {
2408 counter = this._counter = iv.slice(0);
2409
2410 // Remove IV for subsequent blocks
2411 this._iv = undefined;
2412 }
2413 var keystream = counter.slice(0);
2414 cipher.encryptBlock(keystream, 0);
2415
2416 // Increment counter
2417 counter[blockSize - 1] = (counter[blockSize - 1] + 1) | 0
2418
2419 // Encrypt
2420 for (var i = 0; i < blockSize; i++) {
2421 words[offset + i] ^= keystream[i];
2422 }
2423 }
2424 });
2425
2426 CTR.Decryptor = Encryptor;
2427
2428 return CTR;
2429 }());
2430
2431
2432 return CryptoJS
2433
2434 }))