AI销管
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

2 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. ;(function (root, factory) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory();
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. // AMD
  8. define([], factory);
  9. }
  10. else {
  11. // Global (browser)
  12. root.CryptoJS = factory();
  13. }
  14. }(this, function () {
  15. /*globals window, global, require*/
  16. /**
  17. * CryptoJS core components.
  18. */
  19. var CryptoJS = CryptoJS || (function (Math, undefined) {
  20. var crypto;
  21. // Native crypto from window (Browser)
  22. if (typeof window !== 'undefined' && window.crypto) {
  23. crypto = window.crypto;
  24. }
  25. // Native crypto in web worker (Browser)
  26. if (typeof self !== 'undefined' && self.crypto) {
  27. crypto = self.crypto;
  28. }
  29. // Native crypto from worker
  30. if (typeof globalThis !== 'undefined' && globalThis.crypto) {
  31. crypto = globalThis.crypto;
  32. }
  33. // Native (experimental IE 11) crypto from window (Browser)
  34. if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
  35. crypto = window.msCrypto;
  36. }
  37. // Native crypto from global (NodeJS)
  38. if (!crypto && typeof global !== 'undefined' && global.crypto) {
  39. crypto = global.crypto;
  40. }
  41. // Native crypto import via require (NodeJS)
  42. if (!crypto && typeof require === 'function') {
  43. try {
  44. crypto = require('crypto');
  45. } catch (err) {}
  46. }
  47. /*
  48. * Cryptographically secure pseudorandom number generator
  49. *
  50. * As Math.random() is cryptographically not safe to use
  51. */
  52. var cryptoSecureRandomInt = function () {
  53. if (crypto) {
  54. // Use getRandomValues method (Browser)
  55. if (typeof crypto.getRandomValues === 'function') {
  56. try {
  57. return crypto.getRandomValues(new Uint32Array(1))[0];
  58. } catch (err) {}
  59. }
  60. // Use randomBytes method (NodeJS)
  61. if (typeof crypto.randomBytes === 'function') {
  62. try {
  63. return crypto.randomBytes(4).readInt32LE();
  64. } catch (err) {}
  65. }
  66. }
  67. throw new Error('Native crypto module could not be used to get secure random number.');
  68. };
  69. /*
  70. * Local polyfill of Object.create
  71. */
  72. var create = Object.create || (function () {
  73. function F() {}
  74. return function (obj) {
  75. var subtype;
  76. F.prototype = obj;
  77. subtype = new F();
  78. F.prototype = null;
  79. return subtype;
  80. };
  81. }());
  82. /**
  83. * CryptoJS namespace.
  84. */
  85. var C = {};
  86. /**
  87. * Library namespace.
  88. */
  89. var C_lib = C.lib = {};
  90. /**
  91. * Base object for prototypal inheritance.
  92. */
  93. var Base = C_lib.Base = (function () {
  94. return {
  95. /**
  96. * Creates a new object that inherits from this object.
  97. *
  98. * @param {Object} overrides Properties to copy into the new object.
  99. *
  100. * @return {Object} The new object.
  101. *
  102. * @static
  103. *
  104. * @example
  105. *
  106. * var MyType = CryptoJS.lib.Base.extend({
  107. * field: 'value',
  108. *
  109. * method: function () {
  110. * }
  111. * });
  112. */
  113. extend: function (overrides) {
  114. // Spawn
  115. var subtype = create(this);
  116. // Augment
  117. if (overrides) {
  118. subtype.mixIn(overrides);
  119. }
  120. // Create default initializer
  121. if (!subtype.hasOwnProperty('init') || this.init === subtype.init) {
  122. subtype.init = function () {
  123. subtype.$super.init.apply(this, arguments);
  124. };
  125. }
  126. // Initializer's prototype is the subtype object
  127. subtype.init.prototype = subtype;
  128. // Reference supertype
  129. subtype.$super = this;
  130. return subtype;
  131. },
  132. /**
  133. * Extends this object and runs the init method.
  134. * Arguments to create() will be passed to init().
  135. *
  136. * @return {Object} The new object.
  137. *
  138. * @static
  139. *
  140. * @example
  141. *
  142. * var instance = MyType.create();
  143. */
  144. create: function () {
  145. var instance = this.extend();
  146. instance.init.apply(instance, arguments);
  147. return instance;
  148. },
  149. /**
  150. * Initializes a newly created object.
  151. * Override this method to add some logic when your objects are created.
  152. *
  153. * @example
  154. *
  155. * var MyType = CryptoJS.lib.Base.extend({
  156. * init: function () {
  157. * // ...
  158. * }
  159. * });
  160. */
  161. init: function () {
  162. },
  163. /**
  164. * Copies properties into this object.
  165. *
  166. * @param {Object} properties The properties to mix in.
  167. *
  168. * @example
  169. *
  170. * MyType.mixIn({
  171. * field: 'value'
  172. * });
  173. */
  174. mixIn: function (properties) {
  175. for (var propertyName in properties) {
  176. if (properties.hasOwnProperty(propertyName)) {
  177. this[propertyName] = properties[propertyName];
  178. }
  179. }
  180. // IE won't copy toString using the loop above
  181. if (properties.hasOwnProperty('toString')) {
  182. this.toString = properties.toString;
  183. }
  184. },
  185. /**
  186. * Creates a copy of this object.
  187. *
  188. * @return {Object} The clone.
  189. *
  190. * @example
  191. *
  192. * var clone = instance.clone();
  193. */
  194. clone: function () {
  195. return this.init.prototype.extend(this);
  196. }
  197. };
  198. }());
  199. /**
  200. * An array of 32-bit words.
  201. *
  202. * @property {Array} words The array of 32-bit words.
  203. * @property {number} sigBytes The number of significant bytes in this word array.
  204. */
  205. var WordArray = C_lib.WordArray = Base.extend({
  206. /**
  207. * Initializes a newly created word array.
  208. *
  209. * @param {Array} words (Optional) An array of 32-bit words.
  210. * @param {number} sigBytes (Optional) The number of significant bytes in the words.
  211. *
  212. * @example
  213. *
  214. * var wordArray = CryptoJS.lib.WordArray.create();
  215. * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
  216. * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
  217. */
  218. init: function (words, sigBytes) {
  219. words = this.words = words || [];
  220. if (sigBytes != undefined) {
  221. this.sigBytes = sigBytes;
  222. } else {
  223. this.sigBytes = words.length * 4;
  224. }
  225. },
  226. /**
  227. * Converts this word array to a string.
  228. *
  229. * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
  230. *
  231. * @return {string} The stringified word array.
  232. *
  233. * @example
  234. *
  235. * var string = wordArray + '';
  236. * var string = wordArray.toString();
  237. * var string = wordArray.toString(CryptoJS.enc.Utf8);
  238. */
  239. toString: function (encoder) {
  240. return (encoder || Hex).stringify(this);
  241. },
  242. /**
  243. * Concatenates a word array to this word array.
  244. *
  245. * @param {WordArray} wordArray The word array to append.
  246. *
  247. * @return {WordArray} This word array.
  248. *
  249. * @example
  250. *
  251. * wordArray1.concat(wordArray2);
  252. */
  253. concat: function (wordArray) {
  254. // Shortcuts
  255. var thisWords = this.words;
  256. var thatWords = wordArray.words;
  257. var thisSigBytes = this.sigBytes;
  258. var thatSigBytes = wordArray.sigBytes;
  259. // Clamp excess bits
  260. this.clamp();
  261. // Concat
  262. if (thisSigBytes % 4) {
  263. // Copy one byte at a time
  264. for (var i = 0; i < thatSigBytes; i++) {
  265. var thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  266. thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8);
  267. }
  268. } else {
  269. // Copy one word at a time
  270. for (var j = 0; j < thatSigBytes; j += 4) {
  271. thisWords[(thisSigBytes + j) >>> 2] = thatWords[j >>> 2];
  272. }
  273. }
  274. this.sigBytes += thatSigBytes;
  275. // Chainable
  276. return this;
  277. },
  278. /**
  279. * Removes insignificant bits.
  280. *
  281. * @example
  282. *
  283. * wordArray.clamp();
  284. */
  285. clamp: function () {
  286. // Shortcuts
  287. var words = this.words;
  288. var sigBytes = this.sigBytes;
  289. // Clamp
  290. words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8);
  291. words.length = Math.ceil(sigBytes / 4);
  292. },
  293. /**
  294. * Creates a copy of this word array.
  295. *
  296. * @return {WordArray} The clone.
  297. *
  298. * @example
  299. *
  300. * var clone = wordArray.clone();
  301. */
  302. clone: function () {
  303. var clone = Base.clone.call(this);
  304. clone.words = this.words.slice(0);
  305. return clone;
  306. },
  307. /**
  308. * Creates a word array filled with random bytes.
  309. *
  310. * @param {number} nBytes The number of random bytes to generate.
  311. *
  312. * @return {WordArray} The random word array.
  313. *
  314. * @static
  315. *
  316. * @example
  317. *
  318. * var wordArray = CryptoJS.lib.WordArray.random(16);
  319. */
  320. random: function (nBytes) {
  321. var words = [];
  322. for (var i = 0; i < nBytes; i += 4) {
  323. words.push(cryptoSecureRandomInt());
  324. }
  325. return new WordArray.init(words, nBytes);
  326. }
  327. });
  328. /**
  329. * Encoder namespace.
  330. */
  331. var C_enc = C.enc = {};
  332. /**
  333. * Hex encoding strategy.
  334. */
  335. var Hex = C_enc.Hex = {
  336. /**
  337. * Converts a word array to a hex string.
  338. *
  339. * @param {WordArray} wordArray The word array.
  340. *
  341. * @return {string} The hex string.
  342. *
  343. * @static
  344. *
  345. * @example
  346. *
  347. * var hexString = CryptoJS.enc.Hex.stringify(wordArray);
  348. */
  349. stringify: function (wordArray) {
  350. // Shortcuts
  351. var words = wordArray.words;
  352. var sigBytes = wordArray.sigBytes;
  353. // Convert
  354. var hexChars = [];
  355. for (var i = 0; i < sigBytes; i++) {
  356. var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  357. hexChars.push((bite >>> 4).toString(16));
  358. hexChars.push((bite & 0x0f).toString(16));
  359. }
  360. return hexChars.join('');
  361. },
  362. /**
  363. * Converts a hex string to a word array.
  364. *
  365. * @param {string} hexStr The hex string.
  366. *
  367. * @return {WordArray} The word array.
  368. *
  369. * @static
  370. *
  371. * @example
  372. *
  373. * var wordArray = CryptoJS.enc.Hex.parse(hexString);
  374. */
  375. parse: function (hexStr) {
  376. // Shortcut
  377. var hexStrLength = hexStr.length;
  378. // Convert
  379. var words = [];
  380. for (var i = 0; i < hexStrLength; i += 2) {
  381. words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
  382. }
  383. return new WordArray.init(words, hexStrLength / 2);
  384. }
  385. };
  386. /**
  387. * Latin1 encoding strategy.
  388. */
  389. var Latin1 = C_enc.Latin1 = {
  390. /**
  391. * Converts a word array to a Latin1 string.
  392. *
  393. * @param {WordArray} wordArray The word array.
  394. *
  395. * @return {string} The Latin1 string.
  396. *
  397. * @static
  398. *
  399. * @example
  400. *
  401. * var latin1String = CryptoJS.enc.Latin1.stringify(wordArray);
  402. */
  403. stringify: function (wordArray) {
  404. // Shortcuts
  405. var words = wordArray.words;
  406. var sigBytes = wordArray.sigBytes;
  407. // Convert
  408. var latin1Chars = [];
  409. for (var i = 0; i < sigBytes; i++) {
  410. var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  411. latin1Chars.push(String.fromCharCode(bite));
  412. }
  413. return latin1Chars.join('');
  414. },
  415. /**
  416. * Converts a Latin1 string to a word array.
  417. *
  418. * @param {string} latin1Str The Latin1 string.
  419. *
  420. * @return {WordArray} The word array.
  421. *
  422. * @static
  423. *
  424. * @example
  425. *
  426. * var wordArray = CryptoJS.enc.Latin1.parse(latin1String);
  427. */
  428. parse: function (latin1Str) {
  429. // Shortcut
  430. var latin1StrLength = latin1Str.length;
  431. // Convert
  432. var words = [];
  433. for (var i = 0; i < latin1StrLength; i++) {
  434. words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
  435. }
  436. return new WordArray.init(words, latin1StrLength);
  437. }
  438. };
  439. /**
  440. * UTF-8 encoding strategy.
  441. */
  442. var Utf8 = C_enc.Utf8 = {
  443. /**
  444. * Converts a word array to a UTF-8 string.
  445. *
  446. * @param {WordArray} wordArray The word array.
  447. *
  448. * @return {string} The UTF-8 string.
  449. *
  450. * @static
  451. *
  452. * @example
  453. *
  454. * var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
  455. */
  456. stringify: function (wordArray) {
  457. try {
  458. return decodeURIComponent(escape(Latin1.stringify(wordArray)));
  459. } catch (e) {
  460. throw new Error('Malformed UTF-8 data');
  461. }
  462. },
  463. /**
  464. * Converts a UTF-8 string to a word array.
  465. *
  466. * @param {string} utf8Str The UTF-8 string.
  467. *
  468. * @return {WordArray} The word array.
  469. *
  470. * @static
  471. *
  472. * @example
  473. *
  474. * var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
  475. */
  476. parse: function (utf8Str) {
  477. return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
  478. }
  479. };
  480. /**
  481. * Abstract buffered block algorithm template.
  482. *
  483. * The property blockSize must be implemented in a concrete subtype.
  484. *
  485. * @property {number} _minBufferSize The number of blocks that should be kept unprocessed in the buffer. Default: 0
  486. */
  487. var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm = Base.extend({
  488. /**
  489. * Resets this block algorithm's data buffer to its initial state.
  490. *
  491. * @example
  492. *
  493. * bufferedBlockAlgorithm.reset();
  494. */
  495. reset: function () {
  496. // Initial values
  497. this._data = new WordArray.init();
  498. this._nDataBytes = 0;
  499. },
  500. /**
  501. * Adds new data to this block algorithm's buffer.
  502. *
  503. * @param {WordArray|string} data The data to append. Strings are converted to a WordArray using UTF-8.
  504. *
  505. * @example
  506. *
  507. * bufferedBlockAlgorithm._append('data');
  508. * bufferedBlockAlgorithm._append(wordArray);
  509. */
  510. _append: function (data) {
  511. // Convert string to WordArray, else assume WordArray already
  512. if (typeof data == 'string') {
  513. data = Utf8.parse(data);
  514. }
  515. // Append
  516. this._data.concat(data);
  517. this._nDataBytes += data.sigBytes;
  518. },
  519. /**
  520. * Processes available data blocks.
  521. *
  522. * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
  523. *
  524. * @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
  525. *
  526. * @return {WordArray} The processed data.
  527. *
  528. * @example
  529. *
  530. * var processedData = bufferedBlockAlgorithm._process();
  531. * var processedData = bufferedBlockAlgorithm._process(!!'flush');
  532. */
  533. _process: function (doFlush) {
  534. var processedWords;
  535. // Shortcuts
  536. var data = this._data;
  537. var dataWords = data.words;
  538. var dataSigBytes = data.sigBytes;
  539. var blockSize = this.blockSize;
  540. var blockSizeBytes = blockSize * 4;
  541. // Count blocks ready
  542. var nBlocksReady = dataSigBytes / blockSizeBytes;
  543. if (doFlush) {
  544. // Round up to include partial blocks
  545. nBlocksReady = Math.ceil(nBlocksReady);
  546. } else {
  547. // Round down to include only full blocks,
  548. // less the number of blocks that must remain in the buffer
  549. nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
  550. }
  551. // Count words ready
  552. var nWordsReady = nBlocksReady * blockSize;
  553. // Count bytes ready
  554. var nBytesReady = Math.min(nWordsReady * 4, dataSigBytes);
  555. // Process blocks
  556. if (nWordsReady) {
  557. for (var offset = 0; offset < nWordsReady; offset += blockSize) {
  558. // Perform concrete-algorithm logic
  559. this._doProcessBlock(dataWords, offset);
  560. }
  561. // Remove processed words
  562. processedWords = dataWords.splice(0, nWordsReady);
  563. data.sigBytes -= nBytesReady;
  564. }
  565. // Return processed words
  566. return new WordArray.init(processedWords, nBytesReady);
  567. },
  568. /**
  569. * Creates a copy of this object.
  570. *
  571. * @return {Object} The clone.
  572. *
  573. * @example
  574. *
  575. * var clone = bufferedBlockAlgorithm.clone();
  576. */
  577. clone: function () {
  578. var clone = Base.clone.call(this);
  579. clone._data = this._data.clone();
  580. return clone;
  581. },
  582. _minBufferSize: 0
  583. });
  584. /**
  585. * Abstract hasher template.
  586. *
  587. * @property {number} blockSize The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
  588. */
  589. var Hasher = C_lib.Hasher = BufferedBlockAlgorithm.extend({
  590. /**
  591. * Configuration options.
  592. */
  593. cfg: Base.extend(),
  594. /**
  595. * Initializes a newly created hasher.
  596. *
  597. * @param {Object} cfg (Optional) The configuration options to use for this hash computation.
  598. *
  599. * @example
  600. *
  601. * var hasher = CryptoJS.algo.SHA256.create();
  602. */
  603. init: function (cfg) {
  604. // Apply config defaults
  605. this.cfg = this.cfg.extend(cfg);
  606. // Set initial values
  607. this.reset();
  608. },
  609. /**
  610. * Resets this hasher to its initial state.
  611. *
  612. * @example
  613. *
  614. * hasher.reset();
  615. */
  616. reset: function () {
  617. // Reset data buffer
  618. BufferedBlockAlgorithm.reset.call(this);
  619. // Perform concrete-hasher logic
  620. this._doReset();
  621. },
  622. /**
  623. * Updates this hasher with a message.
  624. *
  625. * @param {WordArray|string} messageUpdate The message to append.
  626. *
  627. * @return {Hasher} This hasher.
  628. *
  629. * @example
  630. *
  631. * hasher.update('message');
  632. * hasher.update(wordArray);
  633. */
  634. update: function (messageUpdate) {
  635. // Append
  636. this._append(messageUpdate);
  637. // Update the hash
  638. this._process();
  639. // Chainable
  640. return this;
  641. },
  642. /**
  643. * Finalizes the hash computation.
  644. * Note that the finalize operation is effectively a destructive, read-once operation.
  645. *
  646. * @param {WordArray|string} messageUpdate (Optional) A final message update.
  647. *
  648. * @return {WordArray} The hash.
  649. *
  650. * @example
  651. *
  652. * var hash = hasher.finalize();
  653. * var hash = hasher.finalize('message');
  654. * var hash = hasher.finalize(wordArray);
  655. */
  656. finalize: function (messageUpdate) {
  657. // Final message update
  658. if (messageUpdate) {
  659. this._append(messageUpdate);
  660. }
  661. // Perform concrete-hasher logic
  662. var hash = this._doFinalize();
  663. return hash;
  664. },
  665. blockSize: 512/32,
  666. /**
  667. * Creates a shortcut function to a hasher's object interface.
  668. *
  669. * @param {Hasher} hasher The hasher to create a helper for.
  670. *
  671. * @return {Function} The shortcut function.
  672. *
  673. * @static
  674. *
  675. * @example
  676. *
  677. * var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
  678. */
  679. _createHelper: function (hasher) {
  680. return function (message, cfg) {
  681. return new hasher.init(cfg).finalize(message);
  682. };
  683. },
  684. /**
  685. * Creates a shortcut function to the HMAC's object interface.
  686. *
  687. * @param {Hasher} hasher The hasher to use in this HMAC helper.
  688. *
  689. * @return {Function} The shortcut function.
  690. *
  691. * @static
  692. *
  693. * @example
  694. *
  695. * var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
  696. */
  697. _createHmacHelper: function (hasher) {
  698. return function (message, key) {
  699. return new C_algo.HMAC.init(hasher, key).finalize(message);
  700. };
  701. }
  702. });
  703. /**
  704. * Algorithm namespace.
  705. */
  706. var C_algo = C.algo = {};
  707. return C;
  708. }(Math));
  709. return CryptoJS;
  710. }));