crypto-js-fid

crypto-js

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/435392/987517/crypto-js-fid.js

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址