crypto-js4.1.1

crypto-js4.1.1文件

目前为 2022-03-14 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/441505/1028182/crypto-js411.js

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