decode_common.js 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  1. /*globals window, global, require*/
  2. /**
  3. * CryptoJS core components.
  4. */
  5. var CryptoJS = CryptoJS || (function (Math, undefined) {
  6. var crypto;
  7. // Native crypto from window (Browser)
  8. if (typeof window !== 'undefined' && window.crypto) {
  9. crypto = window.crypto;
  10. }
  11. // Native (experimental IE 11) crypto from window (Browser)
  12. if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
  13. crypto = window.msCrypto;
  14. }
  15. // Native crypto from global (NodeJS)
  16. if (!crypto && typeof global !== 'undefined' && global.crypto) {
  17. crypto = global.crypto;
  18. }
  19. // Native crypto import via require (NodeJS)
  20. if (!crypto && typeof require === 'function') {
  21. try {
  22. crypto = require('crypto');
  23. } catch (err) {}
  24. }
  25. /*
  26. * Cryptographically secure pseudorandom number generator
  27. *
  28. * As Math.random() is cryptographically not safe to use
  29. */
  30. var cryptoSecureRandomInt = function () {
  31. if (crypto) {
  32. // Use getRandomValues method (Browser)
  33. if (typeof crypto.getRandomValues === 'function') {
  34. try {
  35. return crypto.getRandomValues(new Uint32Array(1))[0];
  36. } catch (err) {}
  37. }
  38. // Use randomBytes method (NodeJS)
  39. if (typeof crypto.randomBytes === 'function') {
  40. try {
  41. return crypto.randomBytes(4).readInt32LE();
  42. } catch (err) {}
  43. }
  44. }
  45. throw new Error('Native crypto module could not be used to get secure random number.');
  46. };
  47. /*
  48. * Local polyfill of Object.create
  49. */
  50. var create = Object.create || (function () {
  51. function F() {}
  52. return function (obj) {
  53. var subtype;
  54. F.prototype = obj;
  55. subtype = new F();
  56. F.prototype = null;
  57. return subtype;
  58. };
  59. }())
  60. /**
  61. * CryptoJS namespace.
  62. */
  63. var C = {};
  64. /**
  65. * Library namespace.
  66. */
  67. var C_lib = C.lib = {};
  68. /**
  69. * Base object for prototypal inheritance.
  70. */
  71. var Base = C_lib.Base = (function () {
  72. return {
  73. /**
  74. * Creates a new object that inherits from this object.
  75. *
  76. * @param {Object} overrides Properties to copy into the new object.
  77. *
  78. * @return {Object} The new object.
  79. *
  80. * @static
  81. *
  82. * @example
  83. *
  84. * var MyType = CryptoJS.lib.Base.extend({
  85. * field: 'value',
  86. *
  87. * method: function () {
  88. * }
  89. * });
  90. */
  91. extend: function (overrides) {
  92. // Spawn
  93. var subtype = create(this);
  94. // Augment
  95. if (overrides) {
  96. subtype.mixIn(overrides);
  97. }
  98. // Create default initializer
  99. if (!subtype.hasOwnProperty('init') || this.init === subtype.init) {
  100. subtype.init = function () {
  101. subtype.$super.init.apply(this, arguments);
  102. };
  103. }
  104. // Initializer's prototype is the subtype object
  105. subtype.init.prototype = subtype;
  106. // Reference supertype
  107. subtype.$super = this;
  108. return subtype;
  109. },
  110. /**
  111. * Extends this object and runs the init method.
  112. * Arguments to create() will be passed to init().
  113. *
  114. * @return {Object} The new object.
  115. *
  116. * @static
  117. *
  118. * @example
  119. *
  120. * var instance = MyType.create();
  121. */
  122. create: function () {
  123. var instance = this.extend();
  124. instance.init.apply(instance, arguments);
  125. return instance;
  126. },
  127. /**
  128. * Initializes a newly created object.
  129. * Override this method to add some logic when your objects are created.
  130. *
  131. * @example
  132. *
  133. * var MyType = CryptoJS.lib.Base.extend({
  134. * init: function () {
  135. * // ...
  136. * }
  137. * });
  138. */
  139. init: function () {
  140. },
  141. /**
  142. * Copies properties into this object.
  143. *
  144. * @param {Object} properties The properties to mix in.
  145. *
  146. * @example
  147. *
  148. * MyType.mixIn({
  149. * field: 'value'
  150. * });
  151. */
  152. mixIn: function (properties) {
  153. for (var propertyName in properties) {
  154. if (properties.hasOwnProperty(propertyName)) {
  155. this[propertyName] = properties[propertyName];
  156. }
  157. }
  158. // IE won't copy toString using the loop above
  159. if (properties.hasOwnProperty('toString')) {
  160. this.toString = properties.toString;
  161. }
  162. },
  163. /**
  164. * Creates a copy of this object.
  165. *
  166. * @return {Object} The clone.
  167. *
  168. * @example
  169. *
  170. * var clone = instance.clone();
  171. */
  172. clone: function () {
  173. return this.init.prototype.extend(this);
  174. }
  175. };
  176. }());
  177. /**
  178. * An array of 32-bit words.
  179. *
  180. * @property {Array} words The array of 32-bit words.
  181. * @property {number} sigBytes The number of significant bytes in this word array.
  182. */
  183. var WordArray = C_lib.WordArray = Base.extend({
  184. /**
  185. * Initializes a newly created word array.
  186. *
  187. * @param {Array} words (Optional) An array of 32-bit words.
  188. * @param {number} sigBytes (Optional) The number of significant bytes in the words.
  189. *
  190. * @example
  191. *
  192. * var wordArray = CryptoJS.lib.WordArray.create();
  193. * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
  194. * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
  195. */
  196. init: function (words, sigBytes) {
  197. words = this.words = words || [];
  198. if (sigBytes != undefined) {
  199. this.sigBytes = sigBytes;
  200. } else {
  201. this.sigBytes = words.length * 4;
  202. }
  203. },
  204. /**
  205. * Converts this word array to a string.
  206. *
  207. * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
  208. *
  209. * @return {string} The stringified word array.
  210. *
  211. * @example
  212. *
  213. * var string = wordArray + '';
  214. * var string = wordArray.toString();
  215. * var string = wordArray.toString(CryptoJS.enc.Utf8);
  216. */
  217. toString: function (encoder) {
  218. return (encoder || Hex).stringify(this);
  219. },
  220. /**
  221. * Concatenates a word array to this word array.
  222. *
  223. * @param {WordArray} wordArray The word array to append.
  224. *
  225. * @return {WordArray} This word array.
  226. *
  227. * @example
  228. *
  229. * wordArray1.concat(wordArray2);
  230. */
  231. concat: function (wordArray) {
  232. // Shortcuts
  233. var thisWords = this.words;
  234. var thatWords = wordArray.words;
  235. var thisSigBytes = this.sigBytes;
  236. var thatSigBytes = wordArray.sigBytes;
  237. // Clamp excess bits
  238. this.clamp();
  239. // Concat
  240. if (thisSigBytes % 4) {
  241. // Copy one byte at a time
  242. for (var i = 0; i < thatSigBytes; i++) {
  243. var thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  244. thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8);
  245. }
  246. } else {
  247. // Copy one word at a time
  248. for (var i = 0; i < thatSigBytes; i += 4) {
  249. thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2];
  250. }
  251. }
  252. this.sigBytes += thatSigBytes;
  253. // Chainable
  254. return this;
  255. },
  256. /**
  257. * Removes insignificant bits.
  258. *
  259. * @example
  260. *
  261. * wordArray.clamp();
  262. */
  263. clamp: function () {
  264. // Shortcuts
  265. var words = this.words;
  266. var sigBytes = this.sigBytes;
  267. // Clamp
  268. words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8);
  269. words.length = Math.ceil(sigBytes / 4);
  270. },
  271. /**
  272. * Creates a copy of this word array.
  273. *
  274. * @return {WordArray} The clone.
  275. *
  276. * @example
  277. *
  278. * var clone = wordArray.clone();
  279. */
  280. clone: function () {
  281. var clone = Base.clone.call(this);
  282. clone.words = this.words.slice(0);
  283. return clone;
  284. },
  285. /**
  286. * Creates a word array filled with random bytes.
  287. *
  288. * @param {number} nBytes The number of random bytes to generate.
  289. *
  290. * @return {WordArray} The random word array.
  291. *
  292. * @static
  293. *
  294. * @example
  295. *
  296. * var wordArray = CryptoJS.lib.WordArray.random(16);
  297. */
  298. random: function (nBytes) {
  299. var words = [];
  300. for (var i = 0; i < nBytes; i += 4) {
  301. words.push(cryptoSecureRandomInt());
  302. }
  303. return new WordArray.init(words, nBytes);
  304. }
  305. });
  306. /**
  307. * Encoder namespace.
  308. */
  309. var C_enc = C.enc = {};
  310. /**
  311. * Hex encoding strategy.
  312. */
  313. var Hex = C_enc.Hex = {
  314. /**
  315. * Converts a word array to a hex string.
  316. *
  317. * @param {WordArray} wordArray The word array.
  318. *
  319. * @return {string} The hex string.
  320. *
  321. * @static
  322. *
  323. * @example
  324. *
  325. * var hexString = CryptoJS.enc.Hex.stringify(wordArray);
  326. */
  327. stringify: function (wordArray) {
  328. // Shortcuts
  329. var words = wordArray.words;
  330. var sigBytes = wordArray.sigBytes;
  331. // Convert
  332. var hexChars = [];
  333. for (var i = 0; i < sigBytes; i++) {
  334. var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  335. hexChars.push((bite >>> 4).toString(16));
  336. hexChars.push((bite & 0x0f).toString(16));
  337. }
  338. return hexChars.join('');
  339. },
  340. /**
  341. * Converts a hex string to a word array.
  342. *
  343. * @param {string} hexStr The hex string.
  344. *
  345. * @return {WordArray} The word array.
  346. *
  347. * @static
  348. *
  349. * @example
  350. *
  351. * var wordArray = CryptoJS.enc.Hex.parse(hexString);
  352. */
  353. parse: function (hexStr) {
  354. // Shortcut
  355. var hexStrLength = hexStr.length;
  356. // Convert
  357. var words = [];
  358. for (var i = 0; i < hexStrLength; i += 2) {
  359. words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
  360. }
  361. return new WordArray.init(words, hexStrLength / 2);
  362. }
  363. };
  364. /**
  365. * Latin1 encoding strategy.
  366. */
  367. var Latin1 = C_enc.Latin1 = {
  368. /**
  369. * Converts a word array to a Latin1 string.
  370. *
  371. * @param {WordArray} wordArray The word array.
  372. *
  373. * @return {string} The Latin1 string.
  374. *
  375. * @static
  376. *
  377. * @example
  378. *
  379. * var latin1String = CryptoJS.enc.Latin1.stringify(wordArray);
  380. */
  381. stringify: function (wordArray) {
  382. // Shortcuts
  383. var words = wordArray.words;
  384. var sigBytes = wordArray.sigBytes;
  385. // Convert
  386. var latin1Chars = [];
  387. for (var i = 0; i < sigBytes; i++) {
  388. var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  389. latin1Chars.push(String.fromCharCode(bite));
  390. }
  391. return latin1Chars.join('');
  392. },
  393. /**
  394. * Converts a Latin1 string to a word array.
  395. *
  396. * @param {string} latin1Str The Latin1 string.
  397. *
  398. * @return {WordArray} The word array.
  399. *
  400. * @static
  401. *
  402. * @example
  403. *
  404. * var wordArray = CryptoJS.enc.Latin1.parse(latin1String);
  405. */
  406. parse: function (latin1Str) {
  407. // Shortcut
  408. var latin1StrLength = latin1Str.length;
  409. // Convert
  410. var words = [];
  411. for (var i = 0; i < latin1StrLength; i++) {
  412. words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
  413. }
  414. return new WordArray.init(words, latin1StrLength);
  415. }
  416. };
  417. /**
  418. * UTF-8 encoding strategy.
  419. */
  420. var Utf8 = C_enc.Utf8 = {
  421. /**
  422. * Converts a word array to a UTF-8 string.
  423. *
  424. * @param {WordArray} wordArray The word array.
  425. *
  426. * @return {string} The UTF-8 string.
  427. *
  428. * @static
  429. *
  430. * @example
  431. *
  432. * var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
  433. */
  434. stringify: function (wordArray) {
  435. try {
  436. return decodeURIComponent(escape(Latin1.stringify(wordArray)));
  437. } catch (e) {
  438. throw new Error('Malformed UTF-8 data');
  439. }
  440. },
  441. /**
  442. * Converts a UTF-8 string to a word array.
  443. *
  444. * @param {string} utf8Str The UTF-8 string.
  445. *
  446. * @return {WordArray} The word array.
  447. *
  448. * @static
  449. *
  450. * @example
  451. *
  452. * var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
  453. */
  454. parse: function (utf8Str) {
  455. return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
  456. }
  457. };
  458. /**
  459. * Abstract buffered block algorithm template.
  460. *
  461. * The property blockSize must be implemented in a concrete subtype.
  462. *
  463. * @property {number} _minBufferSize The number of blocks that should be kept unprocessed in the buffer. Default: 0
  464. */
  465. var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm = Base.extend({
  466. /**
  467. * Resets this block algorithm's data buffer to its initial state.
  468. *
  469. * @example
  470. *
  471. * bufferedBlockAlgorithm.reset();
  472. */
  473. reset: function () {
  474. // Initial values
  475. this._data = new WordArray.init();
  476. this._nDataBytes = 0;
  477. },
  478. /**
  479. * Adds new data to this block algorithm's buffer.
  480. *
  481. * @param {WordArray|string} data The data to append. Strings are converted to a WordArray using UTF-8.
  482. *
  483. * @example
  484. *
  485. * bufferedBlockAlgorithm._append('data');
  486. * bufferedBlockAlgorithm._append(wordArray);
  487. */
  488. _append: function (data) {
  489. // Convert string to WordArray, else assume WordArray already
  490. if (typeof data == 'string') {
  491. data = Utf8.parse(data);
  492. }
  493. // Append
  494. this._data.concat(data);
  495. this._nDataBytes += data.sigBytes;
  496. },
  497. /**
  498. * Processes available data blocks.
  499. *
  500. * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
  501. *
  502. * @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
  503. *
  504. * @return {WordArray} The processed data.
  505. *
  506. * @example
  507. *
  508. * var processedData = bufferedBlockAlgorithm._process();
  509. * var processedData = bufferedBlockAlgorithm._process(!!'flush');
  510. */
  511. _process: function (doFlush) {
  512. var processedWords;
  513. // Shortcuts
  514. var data = this._data;
  515. var dataWords = data.words;
  516. var dataSigBytes = data.sigBytes;
  517. var blockSize = this.blockSize;
  518. var blockSizeBytes = blockSize * 4;
  519. // Count blocks ready
  520. var nBlocksReady = dataSigBytes / blockSizeBytes;
  521. if (doFlush) {
  522. // Round up to include partial blocks
  523. nBlocksReady = Math.ceil(nBlocksReady);
  524. } else {
  525. // Round down to include only full blocks,
  526. // less the number of blocks that must remain in the buffer
  527. nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
  528. }
  529. // Count words ready
  530. var nWordsReady = nBlocksReady * blockSize;
  531. // Count bytes ready
  532. var nBytesReady = Math.min(nWordsReady * 4, dataSigBytes);
  533. // Process blocks
  534. if (nWordsReady) {
  535. for (var offset = 0; offset < nWordsReady; offset += blockSize) {
  536. // Perform concrete-algorithm logic
  537. this._doProcessBlock(dataWords, offset);
  538. }
  539. // Remove processed words
  540. processedWords = dataWords.splice(0, nWordsReady);
  541. data.sigBytes -= nBytesReady;
  542. }
  543. // Return processed words
  544. return new WordArray.init(processedWords, nBytesReady);
  545. },
  546. /**
  547. * Creates a copy of this object.
  548. *
  549. * @return {Object} The clone.
  550. *
  551. * @example
  552. *
  553. * var clone = bufferedBlockAlgorithm.clone();
  554. */
  555. clone: function () {
  556. var clone = Base.clone.call(this);
  557. clone._data = this._data.clone();
  558. return clone;
  559. },
  560. _minBufferSize: 0
  561. });
  562. /**
  563. * Abstract hasher template.
  564. *
  565. * @property {number} blockSize The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
  566. */
  567. var Hasher = C_lib.Hasher = BufferedBlockAlgorithm.extend({
  568. /**
  569. * Configuration options.
  570. */
  571. cfg: Base.extend(),
  572. /**
  573. * Initializes a newly created hasher.
  574. *
  575. * @param {Object} cfg (Optional) The configuration options to use for this hash computation.
  576. *
  577. * @example
  578. *
  579. * var hasher = CryptoJS.algo.SHA256.create();
  580. */
  581. init: function (cfg) {
  582. // Apply config defaults
  583. this.cfg = this.cfg.extend(cfg);
  584. // Set initial values
  585. this.reset();
  586. },
  587. /**
  588. * Resets this hasher to its initial state.
  589. *
  590. * @example
  591. *
  592. * hasher.reset();
  593. */
  594. reset: function () {
  595. // Reset data buffer
  596. BufferedBlockAlgorithm.reset.call(this);
  597. // Perform concrete-hasher logic
  598. this._doReset();
  599. },
  600. /**
  601. * Updates this hasher with a message.
  602. *
  603. * @param {WordArray|string} messageUpdate The message to append.
  604. *
  605. * @return {Hasher} This hasher.
  606. *
  607. * @example
  608. *
  609. * hasher.update('message');
  610. * hasher.update(wordArray);
  611. */
  612. update: function (messageUpdate) {
  613. // Append
  614. this._append(messageUpdate);
  615. // Update the hash
  616. this._process();
  617. // Chainable
  618. return this;
  619. },
  620. /**
  621. * Finalizes the hash computation.
  622. * Note that the finalize operation is effectively a destructive, read-once operation.
  623. *
  624. * @param {WordArray|string} messageUpdate (Optional) A final message update.
  625. *
  626. * @return {WordArray} The hash.
  627. *
  628. * @example
  629. *
  630. * var hash = hasher.finalize();
  631. * var hash = hasher.finalize('message');
  632. * var hash = hasher.finalize(wordArray);
  633. */
  634. finalize: function (messageUpdate) {
  635. // Final message update
  636. if (messageUpdate) {
  637. this._append(messageUpdate);
  638. }
  639. // Perform concrete-hasher logic
  640. var hash = this._doFinalize();
  641. return hash;
  642. },
  643. blockSize: 512/32,
  644. /**
  645. * Creates a shortcut function to a hasher's object interface.
  646. *
  647. * @param {Hasher} hasher The hasher to create a helper for.
  648. *
  649. * @return {Function} The shortcut function.
  650. *
  651. * @static
  652. *
  653. * @example
  654. *
  655. * var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
  656. */
  657. _createHelper: function (hasher) {
  658. return function (message, cfg) {
  659. return new hasher.init(cfg).finalize(message);
  660. };
  661. },
  662. /**
  663. * Creates a shortcut function to the HMAC's object interface.
  664. *
  665. * @param {Hasher} hasher The hasher to use in this HMAC helper.
  666. *
  667. * @return {Function} The shortcut function.
  668. *
  669. * @static
  670. *
  671. * @example
  672. *
  673. * var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
  674. */
  675. _createHmacHelper: function (hasher) {
  676. return function (message, key) {
  677. return new C_algo.HMAC.init(hasher, key).finalize(message);
  678. };
  679. }
  680. });
  681. /**
  682. * Algorithm namespace.
  683. */
  684. var C_algo = C.algo = {};
  685. return C;
  686. }(Math));
  687. (function () {
  688. // Shortcuts
  689. var C = CryptoJS;
  690. var C_lib = C.lib;
  691. var WordArray = C_lib.WordArray;
  692. var C_enc = C.enc;
  693. /**
  694. * Base64 encoding strategy.
  695. */
  696. var Base64 = C_enc.Base64 = {
  697. /**
  698. * Converts a word array to a Base64 string.
  699. *
  700. * @param {WordArray} wordArray The word array.
  701. *
  702. * @return {string} The Base64 string.
  703. *
  704. * @static
  705. *
  706. * @example
  707. *
  708. * var base64String = CryptoJS.enc.Base64.stringify(wordArray);
  709. */
  710. stringify: function (wordArray) {
  711. // Shortcuts
  712. var words = wordArray.words;
  713. var sigBytes = wordArray.sigBytes;
  714. var map = this._map;
  715. // Clamp excess bits
  716. wordArray.clamp();
  717. // Convert
  718. var base64Chars = [];
  719. for (var i = 0; i < sigBytes; i += 3) {
  720. var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  721. var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
  722. var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
  723. var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
  724. for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
  725. base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
  726. }
  727. }
  728. // Add padding
  729. var paddingChar = map.charAt(64);
  730. if (paddingChar) {
  731. while (base64Chars.length % 4) {
  732. base64Chars.push(paddingChar);
  733. }
  734. }
  735. return base64Chars.join('');
  736. },
  737. /**
  738. * Converts a Base64 string to a word array.
  739. *
  740. * @param {string} base64Str The Base64 string.
  741. *
  742. * @return {WordArray} The word array.
  743. *
  744. * @static
  745. *
  746. * @example
  747. *
  748. * var wordArray = CryptoJS.enc.Base64.parse(base64String);
  749. */
  750. parse: function (base64Str) {
  751. // Shortcuts
  752. var base64StrLength = base64Str.length;
  753. var map = this._map;
  754. var reverseMap = this._reverseMap;
  755. if (!reverseMap) {
  756. reverseMap = this._reverseMap = [];
  757. for (var j = 0; j < map.length; j++) {
  758. reverseMap[map.charCodeAt(j)] = j;
  759. }
  760. }
  761. // Ignore padding
  762. var paddingChar = map.charAt(64);
  763. if (paddingChar) {
  764. var paddingIndex = base64Str.indexOf(paddingChar);
  765. if (paddingIndex !== -1) {
  766. base64StrLength = paddingIndex;
  767. }
  768. }
  769. // Convert
  770. return parseLoop(base64Str, base64StrLength, reverseMap);
  771. },
  772. _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  773. };
  774. function parseLoop(base64Str, base64StrLength, reverseMap) {
  775. var words = [];
  776. var nBytes = 0;
  777. for (var i = 0; i < base64StrLength; i++) {
  778. if (i % 4) {
  779. var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);
  780. var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);
  781. var bitsCombined = bits1 | bits2;
  782. words[nBytes >>> 2] |= bitsCombined << (24 - (nBytes % 4) * 8);
  783. nBytes++;
  784. }
  785. }
  786. return WordArray.create(words, nBytes);
  787. }
  788. }());
  789. /**
  790. * Cipher core components.
  791. */
  792. CryptoJS.lib.Cipher || (function (undefined) {
  793. // Shortcuts
  794. var C = CryptoJS;
  795. var C_lib = C.lib;
  796. var Base = C_lib.Base;
  797. var WordArray = C_lib.WordArray;
  798. var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm;
  799. var C_enc = C.enc;
  800. var Utf8 = C_enc.Utf8;
  801. var Base64 = C_enc.Base64;
  802. var C_algo = C.algo;
  803. var EvpKDF = C_algo.EvpKDF;
  804. /**
  805. * Abstract base cipher template.
  806. *
  807. * @property {number} keySize This cipher's key size. Default: 4 (128 bits)
  808. * @property {number} ivSize This cipher's IV size. Default: 4 (128 bits)
  809. * @property {number} _ENC_XFORM_MODE A constant representing encryption mode.
  810. * @property {number} _DEC_XFORM_MODE A constant representing decryption mode.
  811. */
  812. var Cipher = C_lib.Cipher = BufferedBlockAlgorithm.extend({
  813. /**
  814. * Configuration options.
  815. *
  816. * @property {WordArray} iv The IV to use for this operation.
  817. */
  818. cfg: Base.extend(),
  819. /**
  820. * Creates this cipher in encryption mode.
  821. *
  822. * @param {WordArray} key The key.
  823. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  824. *
  825. * @return {Cipher} A cipher instance.
  826. *
  827. * @static
  828. *
  829. * @example
  830. *
  831. * var cipher = CryptoJS.algo.AES.createEncryptor(keyWordArray, { iv: ivWordArray });
  832. */
  833. createEncryptor: function (key, cfg) {
  834. return this.create(this._ENC_XFORM_MODE, key, cfg);
  835. },
  836. /**
  837. * Creates this cipher in decryption mode.
  838. *
  839. * @param {WordArray} key The key.
  840. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  841. *
  842. * @return {Cipher} A cipher instance.
  843. *
  844. * @static
  845. *
  846. * @example
  847. *
  848. * var cipher = CryptoJS.algo.AES.createDecryptor(keyWordArray, { iv: ivWordArray });
  849. */
  850. createDecryptor: function (key, cfg) {
  851. return this.create(this._DEC_XFORM_MODE, key, cfg);
  852. },
  853. /**
  854. * Initializes a newly created cipher.
  855. *
  856. * @param {number} xformMode Either the encryption or decryption transormation mode constant.
  857. * @param {WordArray} key The key.
  858. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  859. *
  860. * @example
  861. *
  862. * var cipher = CryptoJS.algo.AES.create(CryptoJS.algo.AES._ENC_XFORM_MODE, keyWordArray, { iv: ivWordArray });
  863. */
  864. init: function (xformMode, key, cfg) {
  865. // Apply config defaults
  866. this.cfg = this.cfg.extend(cfg);
  867. // Store transform mode and key
  868. this._xformMode = xformMode;
  869. this._key = key;
  870. // Set initial values
  871. this.reset();
  872. },
  873. /**
  874. * Resets this cipher to its initial state.
  875. *
  876. * @example
  877. *
  878. * cipher.reset();
  879. */
  880. reset: function () {
  881. // Reset data buffer
  882. BufferedBlockAlgorithm.reset.call(this);
  883. // Perform concrete-cipher logic
  884. this._doReset();
  885. },
  886. /**
  887. * Adds data to be encrypted or decrypted.
  888. *
  889. * @param {WordArray|string} dataUpdate The data to encrypt or decrypt.
  890. *
  891. * @return {WordArray} The data after processing.
  892. *
  893. * @example
  894. *
  895. * var encrypted = cipher.process('data');
  896. * var encrypted = cipher.process(wordArray);
  897. */
  898. process: function (dataUpdate) {
  899. // Append
  900. this._append(dataUpdate);
  901. // Process available blocks
  902. return this._process();
  903. },
  904. /**
  905. * Finalizes the encryption or decryption process.
  906. * Note that the finalize operation is effectively a destructive, read-once operation.
  907. *
  908. * @param {WordArray|string} dataUpdate The final data to encrypt or decrypt.
  909. *
  910. * @return {WordArray} The data after final processing.
  911. *
  912. * @example
  913. *
  914. * var encrypted = cipher.finalize();
  915. * var encrypted = cipher.finalize('data');
  916. * var encrypted = cipher.finalize(wordArray);
  917. */
  918. finalize: function (dataUpdate) {
  919. // Final data update
  920. if (dataUpdate) {
  921. this._append(dataUpdate);
  922. }
  923. // Perform concrete-cipher logic
  924. var finalProcessedData = this._doFinalize();
  925. return finalProcessedData;
  926. },
  927. keySize: 128/32,
  928. ivSize: 128/32,
  929. _ENC_XFORM_MODE: 1,
  930. _DEC_XFORM_MODE: 2,
  931. /**
  932. * Creates shortcut functions to a cipher's object interface.
  933. *
  934. * @param {Cipher} cipher The cipher to create a helper for.
  935. *
  936. * @return {Object} An object with encrypt and decrypt shortcut functions.
  937. *
  938. * @static
  939. *
  940. * @example
  941. *
  942. * var AES = CryptoJS.lib.Cipher._createHelper(CryptoJS.algo.AES);
  943. */
  944. _createHelper: (function () {
  945. function selectCipherStrategy(key) {
  946. if (typeof key == 'string') {
  947. return PasswordBasedCipher;
  948. } else {
  949. return SerializableCipher;
  950. }
  951. }
  952. return function (cipher) {
  953. return {
  954. encrypt: function (message, key, cfg) {
  955. return selectCipherStrategy(key).encrypt(cipher, message, key, cfg);
  956. },
  957. decrypt: function (ciphertext, key, cfg) {
  958. return selectCipherStrategy(key).decrypt(cipher, ciphertext, key, cfg);
  959. }
  960. };
  961. };
  962. }())
  963. });
  964. /**
  965. * Abstract base stream cipher template.
  966. *
  967. * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 1 (32 bits)
  968. */
  969. var StreamCipher = C_lib.StreamCipher = Cipher.extend({
  970. _doFinalize: function () {
  971. // Process partial blocks
  972. var finalProcessedBlocks = this._process(!!'flush');
  973. return finalProcessedBlocks;
  974. },
  975. blockSize: 1
  976. });
  977. /**
  978. * Mode namespace.
  979. */
  980. var C_mode = C.mode = {};
  981. /**
  982. * Abstract base block cipher mode template.
  983. */
  984. var BlockCipherMode = C_lib.BlockCipherMode = Base.extend({
  985. /**
  986. * Creates this mode for encryption.
  987. *
  988. * @param {Cipher} cipher A block cipher instance.
  989. * @param {Array} iv The IV words.
  990. *
  991. * @static
  992. *
  993. * @example
  994. *
  995. * var mode = CryptoJS.mode.CBC.createEncryptor(cipher, iv.words);
  996. */
  997. createEncryptor: function (cipher, iv) {
  998. return this.Encryptor.create(cipher, iv);
  999. },
  1000. /**
  1001. * Creates this mode for decryption.
  1002. *
  1003. * @param {Cipher} cipher A block cipher instance.
  1004. * @param {Array} iv The IV words.
  1005. *
  1006. * @static
  1007. *
  1008. * @example
  1009. *
  1010. * var mode = CryptoJS.mode.CBC.createDecryptor(cipher, iv.words);
  1011. */
  1012. createDecryptor: function (cipher, iv) {
  1013. return this.Decryptor.create(cipher, iv);
  1014. },
  1015. /**
  1016. * Initializes a newly created mode.
  1017. *
  1018. * @param {Cipher} cipher A block cipher instance.
  1019. * @param {Array} iv The IV words.
  1020. *
  1021. * @example
  1022. *
  1023. * var mode = CryptoJS.mode.CBC.Encryptor.create(cipher, iv.words);
  1024. */
  1025. init: function (cipher, iv) {
  1026. this._cipher = cipher;
  1027. this._iv = iv;
  1028. }
  1029. });
  1030. /**
  1031. * Cipher Block Chaining mode.
  1032. */
  1033. var CBC = C_mode.CBC = (function () {
  1034. /**
  1035. * Abstract base CBC mode.
  1036. */
  1037. var CBC = BlockCipherMode.extend();
  1038. /**
  1039. * CBC encryptor.
  1040. */
  1041. CBC.Encryptor = CBC.extend({
  1042. /**
  1043. * Processes the data block at offset.
  1044. *
  1045. * @param {Array} words The data words to operate on.
  1046. * @param {number} offset The offset where the block starts.
  1047. *
  1048. * @example
  1049. *
  1050. * mode.processBlock(data.words, offset);
  1051. */
  1052. processBlock: function (words, offset) {
  1053. // Shortcuts
  1054. var cipher = this._cipher;
  1055. var blockSize = cipher.blockSize;
  1056. // XOR and encrypt
  1057. xorBlock.call(this, words, offset, blockSize);
  1058. cipher.encryptBlock(words, offset);
  1059. // Remember this block to use with next block
  1060. this._prevBlock = words.slice(offset, offset + blockSize);
  1061. }
  1062. });
  1063. /**
  1064. * CBC decryptor.
  1065. */
  1066. CBC.Decryptor = CBC.extend({
  1067. /**
  1068. * Processes the data block at offset.
  1069. *
  1070. * @param {Array} words The data words to operate on.
  1071. * @param {number} offset The offset where the block starts.
  1072. *
  1073. * @example
  1074. *
  1075. * mode.processBlock(data.words, offset);
  1076. */
  1077. processBlock: function (words, offset) {
  1078. // Shortcuts
  1079. var cipher = this._cipher;
  1080. var blockSize = cipher.blockSize;
  1081. // Remember this block to use with next block
  1082. var thisBlock = words.slice(offset, offset + blockSize);
  1083. // Decrypt and XOR
  1084. cipher.decryptBlock(words, offset);
  1085. xorBlock.call(this, words, offset, blockSize);
  1086. // This block becomes the previous block
  1087. this._prevBlock = thisBlock;
  1088. }
  1089. });
  1090. function xorBlock(words, offset, blockSize) {
  1091. var block;
  1092. // Shortcut
  1093. var iv = this._iv;
  1094. // Choose mixing block
  1095. if (iv) {
  1096. block = iv;
  1097. // Remove IV for subsequent blocks
  1098. this._iv = undefined;
  1099. } else {
  1100. block = this._prevBlock;
  1101. }
  1102. // XOR blocks
  1103. for (var i = 0; i < blockSize; i++) {
  1104. words[offset + i] ^= block[i];
  1105. }
  1106. }
  1107. return CBC;
  1108. }());
  1109. /**
  1110. * Padding namespace.
  1111. */
  1112. var C_pad = C.pad = {};
  1113. /**
  1114. * PKCS #5/7 padding strategy.
  1115. */
  1116. var Pkcs7 = C_pad.Pkcs7 = {
  1117. /**
  1118. * Pads data using the algorithm defined in PKCS #5/7.
  1119. *
  1120. * @param {WordArray} data The data to pad.
  1121. * @param {number} blockSize The multiple that the data should be padded to.
  1122. *
  1123. * @static
  1124. *
  1125. * @example
  1126. *
  1127. * CryptoJS.pad.Pkcs7.pad(wordArray, 4);
  1128. */
  1129. pad: function (data, blockSize) {
  1130. // Shortcut
  1131. var blockSizeBytes = blockSize * 4;
  1132. // Count padding bytes
  1133. var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
  1134. // Create padding word
  1135. var paddingWord = (nPaddingBytes << 24) | (nPaddingBytes << 16) | (nPaddingBytes << 8) | nPaddingBytes;
  1136. // Create padding
  1137. var paddingWords = [];
  1138. for (var i = 0; i < nPaddingBytes; i += 4) {
  1139. paddingWords.push(paddingWord);
  1140. }
  1141. var padding = WordArray.create(paddingWords, nPaddingBytes);
  1142. // Add padding
  1143. data.concat(padding);
  1144. },
  1145. /**
  1146. * Unpads data that had been padded using the algorithm defined in PKCS #5/7.
  1147. *
  1148. * @param {WordArray} data The data to unpad.
  1149. *
  1150. * @static
  1151. *
  1152. * @example
  1153. *
  1154. * CryptoJS.pad.Pkcs7.unpad(wordArray);
  1155. */
  1156. unpad: function (data) {
  1157. // Get number of padding bytes from last byte
  1158. var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
  1159. // Remove padding
  1160. data.sigBytes -= nPaddingBytes;
  1161. }
  1162. };
  1163. /**
  1164. * Abstract base block cipher template.
  1165. *
  1166. * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 4 (128 bits)
  1167. */
  1168. var BlockCipher = C_lib.BlockCipher = Cipher.extend({
  1169. /**
  1170. * Configuration options.
  1171. *
  1172. * @property {Mode} mode The block mode to use. Default: CBC
  1173. * @property {Padding} padding The padding strategy to use. Default: Pkcs7
  1174. */
  1175. cfg: Cipher.cfg.extend({
  1176. mode: CBC,
  1177. padding: Pkcs7
  1178. }),
  1179. reset: function () {
  1180. var modeCreator;
  1181. // Reset cipher
  1182. Cipher.reset.call(this);
  1183. // Shortcuts
  1184. var cfg = this.cfg;
  1185. var iv = cfg.iv;
  1186. var mode = cfg.mode;
  1187. // Reset block mode
  1188. if (this._xformMode == this._ENC_XFORM_MODE) {
  1189. modeCreator = mode.createEncryptor;
  1190. } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
  1191. modeCreator = mode.createDecryptor;
  1192. // Keep at least one block in the buffer for unpadding
  1193. this._minBufferSize = 1;
  1194. }
  1195. if (this._mode && this._mode.__creator == modeCreator) {
  1196. this._mode.init(this, iv && iv.words);
  1197. } else {
  1198. this._mode = modeCreator.call(mode, this, iv && iv.words);
  1199. this._mode.__creator = modeCreator;
  1200. }
  1201. },
  1202. _doProcessBlock: function (words, offset) {
  1203. this._mode.processBlock(words, offset);
  1204. },
  1205. _doFinalize: function () {
  1206. var finalProcessedBlocks;
  1207. // Shortcut
  1208. var padding = this.cfg.padding;
  1209. // Finalize
  1210. if (this._xformMode == this._ENC_XFORM_MODE) {
  1211. // Pad data
  1212. padding.pad(this._data, this.blockSize);
  1213. // Process final blocks
  1214. finalProcessedBlocks = this._process(!!'flush');
  1215. } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
  1216. // Process final blocks
  1217. finalProcessedBlocks = this._process(!!'flush');
  1218. // Unpad data
  1219. padding.unpad(finalProcessedBlocks);
  1220. }
  1221. return finalProcessedBlocks;
  1222. },
  1223. blockSize: 128/32
  1224. });
  1225. /**
  1226. * A collection of cipher parameters.
  1227. *
  1228. * @property {WordArray} ciphertext The raw ciphertext.
  1229. * @property {WordArray} key The key to this ciphertext.
  1230. * @property {WordArray} iv The IV used in the ciphering operation.
  1231. * @property {WordArray} salt The salt used with a key derivation function.
  1232. * @property {Cipher} algorithm The cipher algorithm.
  1233. * @property {Mode} mode The block mode used in the ciphering operation.
  1234. * @property {Padding} padding The padding scheme used in the ciphering operation.
  1235. * @property {number} blockSize The block size of the cipher.
  1236. * @property {Format} formatter The default formatting strategy to convert this cipher params object to a string.
  1237. */
  1238. var CipherParams = C_lib.CipherParams = Base.extend({
  1239. /**
  1240. * Initializes a newly created cipher params object.
  1241. *
  1242. * @param {Object} cipherParams An object with any of the possible cipher parameters.
  1243. *
  1244. * @example
  1245. *
  1246. * var cipherParams = CryptoJS.lib.CipherParams.create({
  1247. * ciphertext: ciphertextWordArray,
  1248. * key: keyWordArray,
  1249. * iv: ivWordArray,
  1250. * salt: saltWordArray,
  1251. * algorithm: CryptoJS.algo.AES,
  1252. * mode: CryptoJS.mode.CBC,
  1253. * padding: CryptoJS.pad.PKCS7,
  1254. * blockSize: 4,
  1255. * formatter: CryptoJS.format.OpenSSL
  1256. * });
  1257. */
  1258. init: function (cipherParams) {
  1259. this.mixIn(cipherParams);
  1260. },
  1261. /**
  1262. * Converts this cipher params object to a string.
  1263. *
  1264. * @param {Format} formatter (Optional) The formatting strategy to use.
  1265. *
  1266. * @return {string} The stringified cipher params.
  1267. *
  1268. * @throws Error If neither the formatter nor the default formatter is set.
  1269. *
  1270. * @example
  1271. *
  1272. * var string = cipherParams + '';
  1273. * var string = cipherParams.toString();
  1274. * var string = cipherParams.toString(CryptoJS.format.OpenSSL);
  1275. */
  1276. toString: function (formatter) {
  1277. return (formatter || this.formatter).stringify(this);
  1278. }
  1279. });
  1280. /**
  1281. * Format namespace.
  1282. */
  1283. var C_format = C.format = {};
  1284. /**
  1285. * OpenSSL formatting strategy.
  1286. */
  1287. var OpenSSLFormatter = C_format.OpenSSL = {
  1288. /**
  1289. * Converts a cipher params object to an OpenSSL-compatible string.
  1290. *
  1291. * @param {CipherParams} cipherParams The cipher params object.
  1292. *
  1293. * @return {string} The OpenSSL-compatible string.
  1294. *
  1295. * @static
  1296. *
  1297. * @example
  1298. *
  1299. * var openSSLString = CryptoJS.format.OpenSSL.stringify(cipherParams);
  1300. */
  1301. stringify: function (cipherParams) {
  1302. var wordArray;
  1303. // Shortcuts
  1304. var ciphertext = cipherParams.ciphertext;
  1305. var salt = cipherParams.salt;
  1306. // Format
  1307. if (salt) {
  1308. wordArray = WordArray.create([0x53616c74, 0x65645f5f]).concat(salt).concat(ciphertext);
  1309. } else {
  1310. wordArray = ciphertext;
  1311. }
  1312. return wordArray.toString(Base64);
  1313. },
  1314. /**
  1315. * Converts an OpenSSL-compatible string to a cipher params object.
  1316. *
  1317. * @param {string} openSSLStr The OpenSSL-compatible string.
  1318. *
  1319. * @return {CipherParams} The cipher params object.
  1320. *
  1321. * @static
  1322. *
  1323. * @example
  1324. *
  1325. * var cipherParams = CryptoJS.format.OpenSSL.parse(openSSLString);
  1326. */
  1327. parse: function (openSSLStr) {
  1328. var salt;
  1329. // Parse base64
  1330. var ciphertext = Base64.parse(openSSLStr);
  1331. // Shortcut
  1332. var ciphertextWords = ciphertext.words;
  1333. // Test for salt
  1334. if (ciphertextWords[0] == 0x53616c74 && ciphertextWords[1] == 0x65645f5f) {
  1335. // Extract salt
  1336. salt = WordArray.create(ciphertextWords.slice(2, 4));
  1337. // Remove salt from ciphertext
  1338. ciphertextWords.splice(0, 4);
  1339. ciphertext.sigBytes -= 16;
  1340. }
  1341. return CipherParams.create({ ciphertext: ciphertext, salt: salt });
  1342. }
  1343. };
  1344. /**
  1345. * A cipher wrapper that returns ciphertext as a serializable cipher params object.
  1346. */
  1347. var SerializableCipher = C_lib.SerializableCipher = Base.extend({
  1348. /**
  1349. * Configuration options.
  1350. *
  1351. * @property {Formatter} format The formatting strategy to convert cipher param objects to and from a string. Default: OpenSSL
  1352. */
  1353. cfg: Base.extend({
  1354. format: OpenSSLFormatter
  1355. }),
  1356. /**
  1357. * Encrypts a message.
  1358. *
  1359. * @param {Cipher} cipher The cipher algorithm to use.
  1360. * @param {WordArray|string} message The message to encrypt.
  1361. * @param {WordArray} key The key.
  1362. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  1363. *
  1364. * @return {CipherParams} A cipher params object.
  1365. *
  1366. * @static
  1367. *
  1368. * @example
  1369. *
  1370. * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key);
  1371. * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv });
  1372. * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv, format: CryptoJS.format.OpenSSL });
  1373. */
  1374. encrypt: function (cipher, message, key, cfg) {
  1375. // Apply config defaults
  1376. cfg = this.cfg.extend(cfg);
  1377. // Encrypt
  1378. var encryptor = cipher.createEncryptor(key, cfg);
  1379. var ciphertext = encryptor.finalize(message);
  1380. // Shortcut
  1381. var cipherCfg = encryptor.cfg;
  1382. // Create and return serializable cipher params
  1383. return CipherParams.create({
  1384. ciphertext: ciphertext,
  1385. key: key,
  1386. iv: cipherCfg.iv,
  1387. algorithm: cipher,
  1388. mode: cipherCfg.mode,
  1389. padding: cipherCfg.padding,
  1390. blockSize: cipher.blockSize,
  1391. formatter: cfg.format
  1392. });
  1393. },
  1394. /**
  1395. * Decrypts serialized ciphertext.
  1396. *
  1397. * @param {Cipher} cipher The cipher algorithm to use.
  1398. * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
  1399. * @param {WordArray} key The key.
  1400. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  1401. *
  1402. * @return {WordArray} The plaintext.
  1403. *
  1404. * @static
  1405. *
  1406. * @example
  1407. *
  1408. * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, key, { iv: iv, format: CryptoJS.format.OpenSSL });
  1409. * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, key, { iv: iv, format: CryptoJS.format.OpenSSL });
  1410. */
  1411. decrypt: function (cipher, ciphertext, key, cfg) {
  1412. // Apply config defaults
  1413. cfg = this.cfg.extend(cfg);
  1414. // Convert string to CipherParams
  1415. ciphertext = this._parse(ciphertext, cfg.format);
  1416. // Decrypt
  1417. var plaintext = cipher.createDecryptor(key, cfg).finalize(ciphertext.ciphertext);
  1418. return plaintext;
  1419. },
  1420. /**
  1421. * Converts serialized ciphertext to CipherParams,
  1422. * else assumed CipherParams already and returns ciphertext unchanged.
  1423. *
  1424. * @param {CipherParams|string} ciphertext The ciphertext.
  1425. * @param {Formatter} format The formatting strategy to use to parse serialized ciphertext.
  1426. *
  1427. * @return {CipherParams} The unserialized ciphertext.
  1428. *
  1429. * @static
  1430. *
  1431. * @example
  1432. *
  1433. * var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
  1434. */
  1435. _parse: function (ciphertext, format) {
  1436. if (typeof ciphertext == 'string') {
  1437. return format.parse(ciphertext, this);
  1438. } else {
  1439. return ciphertext;
  1440. }
  1441. }
  1442. });
  1443. /**
  1444. * Key derivation function namespace.
  1445. */
  1446. var C_kdf = C.kdf = {};
  1447. /**
  1448. * OpenSSL key derivation function.
  1449. */
  1450. var OpenSSLKdf = C_kdf.OpenSSL = {
  1451. /**
  1452. * Derives a key and IV from a password.
  1453. *
  1454. * @param {string} password The password to derive from.
  1455. * @param {number} keySize The size in words of the key to generate.
  1456. * @param {number} ivSize The size in words of the IV to generate.
  1457. * @param {WordArray|string} salt (Optional) A 64-bit salt to use. If omitted, a salt will be generated randomly.
  1458. *
  1459. * @return {CipherParams} A cipher params object with the key, IV, and salt.
  1460. *
  1461. * @static
  1462. *
  1463. * @example
  1464. *
  1465. * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32);
  1466. * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32, 'saltsalt');
  1467. */
  1468. execute: function (password, keySize, ivSize, salt) {
  1469. // Generate random salt
  1470. if (!salt) {
  1471. salt = WordArray.random(64/8);
  1472. }
  1473. // Derive key and IV
  1474. var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);
  1475. // Separate key and IV
  1476. var iv = WordArray.create(key.words.slice(keySize), ivSize * 4);
  1477. key.sigBytes = keySize * 4;
  1478. // Return params
  1479. return CipherParams.create({ key: key, iv: iv, salt: salt });
  1480. }
  1481. };
  1482. /**
  1483. * A serializable cipher wrapper that derives the key from a password,
  1484. * and returns ciphertext as a serializable cipher params object.
  1485. */
  1486. var PasswordBasedCipher = C_lib.PasswordBasedCipher = SerializableCipher.extend({
  1487. /**
  1488. * Configuration options.
  1489. *
  1490. * @property {KDF} kdf The key derivation function to use to generate a key and IV from a password. Default: OpenSSL
  1491. */
  1492. cfg: SerializableCipher.cfg.extend({
  1493. kdf: OpenSSLKdf
  1494. }),
  1495. /**
  1496. * Encrypts a message using a password.
  1497. *
  1498. * @param {Cipher} cipher The cipher algorithm to use.
  1499. * @param {WordArray|string} message The message to encrypt.
  1500. * @param {string} password The password.
  1501. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  1502. *
  1503. * @return {CipherParams} A cipher params object.
  1504. *
  1505. * @static
  1506. *
  1507. * @example
  1508. *
  1509. * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password');
  1510. * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password', { format: CryptoJS.format.OpenSSL });
  1511. */
  1512. encrypt: function (cipher, message, password, cfg) {
  1513. // Apply config defaults
  1514. cfg = this.cfg.extend(cfg);
  1515. // Derive key and other params
  1516. var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize);
  1517. // Add IV to config
  1518. cfg.iv = derivedParams.iv;
  1519. // Encrypt
  1520. var ciphertext = SerializableCipher.encrypt.call(this, cipher, message, derivedParams.key, cfg);
  1521. // Mix in derived params
  1522. ciphertext.mixIn(derivedParams);
  1523. return ciphertext;
  1524. },
  1525. /**
  1526. * Decrypts serialized ciphertext using a password.
  1527. *
  1528. * @param {Cipher} cipher The cipher algorithm to use.
  1529. * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
  1530. * @param {string} password The password.
  1531. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  1532. *
  1533. * @return {WordArray} The plaintext.
  1534. *
  1535. * @static
  1536. *
  1537. * @example
  1538. *
  1539. * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, 'password', { format: CryptoJS.format.OpenSSL });
  1540. * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, 'password', { format: CryptoJS.format.OpenSSL });
  1541. */
  1542. decrypt: function (cipher, ciphertext, password, cfg) {
  1543. // Apply config defaults
  1544. cfg = this.cfg.extend(cfg);
  1545. // Convert string to CipherParams
  1546. ciphertext = this._parse(ciphertext, cfg.format);
  1547. // Derive key and other params
  1548. var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt);
  1549. // Add IV to config
  1550. cfg.iv = derivedParams.iv;
  1551. // Decrypt
  1552. var plaintext = SerializableCipher.decrypt.call(this, cipher, ciphertext, derivedParams.key, cfg);
  1553. return plaintext;
  1554. }
  1555. });
  1556. }());
  1557. (function () {
  1558. // Shortcuts
  1559. var C = CryptoJS;
  1560. var C_lib = C.lib;
  1561. var BlockCipher = C_lib.BlockCipher;
  1562. var C_algo = C.algo;
  1563. // Lookup tables
  1564. var SBOX = [];
  1565. var INV_SBOX = [];
  1566. var SUB_MIX_0 = [];
  1567. var SUB_MIX_1 = [];
  1568. var SUB_MIX_2 = [];
  1569. var SUB_MIX_3 = [];
  1570. var INV_SUB_MIX_0 = [];
  1571. var INV_SUB_MIX_1 = [];
  1572. var INV_SUB_MIX_2 = [];
  1573. var INV_SUB_MIX_3 = [];
  1574. // Compute lookup tables
  1575. (function () {
  1576. // Compute double table
  1577. var d = [];
  1578. for (var i = 0; i < 256; i++) {
  1579. if (i < 128) {
  1580. d[i] = i << 1;
  1581. } else {
  1582. d[i] = (i << 1) ^ 0x11b;
  1583. }
  1584. }
  1585. // Walk GF(2^8)
  1586. var x = 0;
  1587. var xi = 0;
  1588. for (var i = 0; i < 256; i++) {
  1589. // Compute sbox
  1590. var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
  1591. sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
  1592. SBOX[x] = sx;
  1593. INV_SBOX[sx] = x;
  1594. // Compute multiplication
  1595. var x2 = d[x];
  1596. var x4 = d[x2];
  1597. var x8 = d[x4];
  1598. // Compute sub bytes, mix columns tables
  1599. var t = (d[sx] * 0x101) ^ (sx * 0x1010100);
  1600. SUB_MIX_0[x] = (t << 24) | (t >>> 8);
  1601. SUB_MIX_1[x] = (t << 16) | (t >>> 16);
  1602. SUB_MIX_2[x] = (t << 8) | (t >>> 24);
  1603. SUB_MIX_3[x] = t;
  1604. // Compute inv sub bytes, inv mix columns tables
  1605. var t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
  1606. INV_SUB_MIX_0[sx] = (t << 24) | (t >>> 8);
  1607. INV_SUB_MIX_1[sx] = (t << 16) | (t >>> 16);
  1608. INV_SUB_MIX_2[sx] = (t << 8) | (t >>> 24);
  1609. INV_SUB_MIX_3[sx] = t;
  1610. // Compute next counter
  1611. if (!x) {
  1612. x = xi = 1;
  1613. } else {
  1614. x = x2 ^ d[d[d[x8 ^ x2]]];
  1615. xi ^= d[d[xi]];
  1616. }
  1617. }
  1618. }());
  1619. // Precomputed Rcon lookup
  1620. var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
  1621. /**
  1622. * AES block cipher algorithm.
  1623. */
  1624. var AES = C_algo.AES = BlockCipher.extend({
  1625. _doReset: function () {
  1626. var t;
  1627. // Skip reset of nRounds has been set before and key did not change
  1628. if (this._nRounds && this._keyPriorReset === this._key) {
  1629. return;
  1630. }
  1631. // Shortcuts
  1632. var key = this._keyPriorReset = this._key;
  1633. var keyWords = key.words;
  1634. var keySize = key.sigBytes / 4;
  1635. // Compute number of rounds
  1636. var nRounds = this._nRounds = keySize + 6;
  1637. // Compute number of key schedule rows
  1638. var ksRows = (nRounds + 1) * 4;
  1639. // Compute key schedule
  1640. var keySchedule = this._keySchedule = [];
  1641. for (var ksRow = 0; ksRow < ksRows; ksRow++) {
  1642. if (ksRow < keySize) {
  1643. keySchedule[ksRow] = keyWords[ksRow];
  1644. } else {
  1645. t = keySchedule[ksRow - 1];
  1646. if (!(ksRow % keySize)) {
  1647. // Rot word
  1648. t = (t << 8) | (t >>> 24);
  1649. // Sub word
  1650. t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
  1651. // Mix Rcon
  1652. t ^= RCON[(ksRow / keySize) | 0] << 24;
  1653. } else if (keySize > 6 && ksRow % keySize == 4) {
  1654. // Sub word
  1655. t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
  1656. }
  1657. keySchedule[ksRow] = keySchedule[ksRow - keySize] ^ t;
  1658. }
  1659. }
  1660. // Compute inv key schedule
  1661. var invKeySchedule = this._invKeySchedule = [];
  1662. for (var invKsRow = 0; invKsRow < ksRows; invKsRow++) {
  1663. var ksRow = ksRows - invKsRow;
  1664. if (invKsRow % 4) {
  1665. var t = keySchedule[ksRow];
  1666. } else {
  1667. var t = keySchedule[ksRow - 4];
  1668. }
  1669. if (invKsRow < 4 || ksRow <= 4) {
  1670. invKeySchedule[invKsRow] = t;
  1671. } else {
  1672. invKeySchedule[invKsRow] = INV_SUB_MIX_0[SBOX[t >>> 24]] ^ INV_SUB_MIX_1[SBOX[(t >>> 16) & 0xff]] ^
  1673. INV_SUB_MIX_2[SBOX[(t >>> 8) & 0xff]] ^ INV_SUB_MIX_3[SBOX[t & 0xff]];
  1674. }
  1675. }
  1676. },
  1677. encryptBlock: function (M, offset) {
  1678. this._doCryptBlock(M, offset, this._keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX);
  1679. },
  1680. decryptBlock: function (M, offset) {
  1681. // Swap 2nd and 4th rows
  1682. var t = M[offset + 1];
  1683. M[offset + 1] = M[offset + 3];
  1684. M[offset + 3] = t;
  1685. this._doCryptBlock(M, offset, this._invKeySchedule, INV_SUB_MIX_0, INV_SUB_MIX_1, INV_SUB_MIX_2, INV_SUB_MIX_3, INV_SBOX);
  1686. // Inv swap 2nd and 4th rows
  1687. var t = M[offset + 1];
  1688. M[offset + 1] = M[offset + 3];
  1689. M[offset + 3] = t;
  1690. },
  1691. _doCryptBlock: function (M, offset, keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX) {
  1692. // Shortcut
  1693. var nRounds = this._nRounds;
  1694. // Get input, add round key
  1695. var s0 = M[offset] ^ keySchedule[0];
  1696. var s1 = M[offset + 1] ^ keySchedule[1];
  1697. var s2 = M[offset + 2] ^ keySchedule[2];
  1698. var s3 = M[offset + 3] ^ keySchedule[3];
  1699. // Key schedule row counter
  1700. var ksRow = 4;
  1701. // Rounds
  1702. for (var round = 1; round < nRounds; round++) {
  1703. // Shift rows, sub bytes, mix columns, add round key
  1704. 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++];
  1705. 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++];
  1706. 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++];
  1707. 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++];
  1708. // Update state
  1709. s0 = t0;
  1710. s1 = t1;
  1711. s2 = t2;
  1712. s3 = t3;
  1713. }
  1714. // Shift rows, sub bytes, add round key
  1715. var t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
  1716. var t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
  1717. var t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
  1718. var t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
  1719. // Set output
  1720. M[offset] = t0;
  1721. M[offset + 1] = t1;
  1722. M[offset + 2] = t2;
  1723. M[offset + 3] = t3;
  1724. },
  1725. keySize: 256/32
  1726. });
  1727. /**
  1728. * Shortcut functions to the cipher's object interface.
  1729. *
  1730. * @example
  1731. *
  1732. * var ciphertext = CryptoJS.AES.encrypt(message, key, cfg);
  1733. * var plaintext = CryptoJS.AES.decrypt(ciphertext, key, cfg);
  1734. */
  1735. C.AES = BlockCipher._createHelper(AES);
  1736. }());
  1737. (function (Math) {
  1738. // Shortcuts
  1739. var C = CryptoJS;
  1740. var C_lib = C.lib;
  1741. var WordArray = C_lib.WordArray;
  1742. var Hasher = C_lib.Hasher;
  1743. var C_algo = C.algo;
  1744. // Constants table
  1745. var T = [];
  1746. // Compute constants
  1747. (function () {
  1748. for (var i = 0; i < 64; i++) {
  1749. T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0;
  1750. }
  1751. }());
  1752. /**
  1753. * MD5 hash algorithm.
  1754. */
  1755. var MD5 = C_algo.MD5 = Hasher.extend({
  1756. _doReset: function () {
  1757. this._hash = new WordArray.init([
  1758. 0x67452301, 0xefcdab89,
  1759. 0x98badcfe, 0x10325476
  1760. ]);
  1761. },
  1762. _doProcessBlock: function (M, offset) {
  1763. // Swap endian
  1764. for (var i = 0; i < 16; i++) {
  1765. // Shortcuts
  1766. var offset_i = offset + i;
  1767. var M_offset_i = M[offset_i];
  1768. M[offset_i] = (
  1769. (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
  1770. (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
  1771. );
  1772. }
  1773. // Shortcuts
  1774. var H = this._hash.words;
  1775. var M_offset_0 = M[offset + 0];
  1776. var M_offset_1 = M[offset + 1];
  1777. var M_offset_2 = M[offset + 2];
  1778. var M_offset_3 = M[offset + 3];
  1779. var M_offset_4 = M[offset + 4];
  1780. var M_offset_5 = M[offset + 5];
  1781. var M_offset_6 = M[offset + 6];
  1782. var M_offset_7 = M[offset + 7];
  1783. var M_offset_8 = M[offset + 8];
  1784. var M_offset_9 = M[offset + 9];
  1785. var M_offset_10 = M[offset + 10];
  1786. var M_offset_11 = M[offset + 11];
  1787. var M_offset_12 = M[offset + 12];
  1788. var M_offset_13 = M[offset + 13];
  1789. var M_offset_14 = M[offset + 14];
  1790. var M_offset_15 = M[offset + 15];
  1791. // Working varialbes
  1792. var a = H[0];
  1793. var b = H[1];
  1794. var c = H[2];
  1795. var d = H[3];
  1796. // Computation
  1797. a = FF(a, b, c, d, M_offset_0, 7, T[0]);
  1798. d = FF(d, a, b, c, M_offset_1, 12, T[1]);
  1799. c = FF(c, d, a, b, M_offset_2, 17, T[2]);
  1800. b = FF(b, c, d, a, M_offset_3, 22, T[3]);
  1801. a = FF(a, b, c, d, M_offset_4, 7, T[4]);
  1802. d = FF(d, a, b, c, M_offset_5, 12, T[5]);
  1803. c = FF(c, d, a, b, M_offset_6, 17, T[6]);
  1804. b = FF(b, c, d, a, M_offset_7, 22, T[7]);
  1805. a = FF(a, b, c, d, M_offset_8, 7, T[8]);
  1806. d = FF(d, a, b, c, M_offset_9, 12, T[9]);
  1807. c = FF(c, d, a, b, M_offset_10, 17, T[10]);
  1808. b = FF(b, c, d, a, M_offset_11, 22, T[11]);
  1809. a = FF(a, b, c, d, M_offset_12, 7, T[12]);
  1810. d = FF(d, a, b, c, M_offset_13, 12, T[13]);
  1811. c = FF(c, d, a, b, M_offset_14, 17, T[14]);
  1812. b = FF(b, c, d, a, M_offset_15, 22, T[15]);
  1813. a = GG(a, b, c, d, M_offset_1, 5, T[16]);
  1814. d = GG(d, a, b, c, M_offset_6, 9, T[17]);
  1815. c = GG(c, d, a, b, M_offset_11, 14, T[18]);
  1816. b = GG(b, c, d, a, M_offset_0, 20, T[19]);
  1817. a = GG(a, b, c, d, M_offset_5, 5, T[20]);
  1818. d = GG(d, a, b, c, M_offset_10, 9, T[21]);
  1819. c = GG(c, d, a, b, M_offset_15, 14, T[22]);
  1820. b = GG(b, c, d, a, M_offset_4, 20, T[23]);
  1821. a = GG(a, b, c, d, M_offset_9, 5, T[24]);
  1822. d = GG(d, a, b, c, M_offset_14, 9, T[25]);
  1823. c = GG(c, d, a, b, M_offset_3, 14, T[26]);
  1824. b = GG(b, c, d, a, M_offset_8, 20, T[27]);
  1825. a = GG(a, b, c, d, M_offset_13, 5, T[28]);
  1826. d = GG(d, a, b, c, M_offset_2, 9, T[29]);
  1827. c = GG(c, d, a, b, M_offset_7, 14, T[30]);
  1828. b = GG(b, c, d, a, M_offset_12, 20, T[31]);
  1829. a = HH(a, b, c, d, M_offset_5, 4, T[32]);
  1830. d = HH(d, a, b, c, M_offset_8, 11, T[33]);
  1831. c = HH(c, d, a, b, M_offset_11, 16, T[34]);
  1832. b = HH(b, c, d, a, M_offset_14, 23, T[35]);
  1833. a = HH(a, b, c, d, M_offset_1, 4, T[36]);
  1834. d = HH(d, a, b, c, M_offset_4, 11, T[37]);
  1835. c = HH(c, d, a, b, M_offset_7, 16, T[38]);
  1836. b = HH(b, c, d, a, M_offset_10, 23, T[39]);
  1837. a = HH(a, b, c, d, M_offset_13, 4, T[40]);
  1838. d = HH(d, a, b, c, M_offset_0, 11, T[41]);
  1839. c = HH(c, d, a, b, M_offset_3, 16, T[42]);
  1840. b = HH(b, c, d, a, M_offset_6, 23, T[43]);
  1841. a = HH(a, b, c, d, M_offset_9, 4, T[44]);
  1842. d = HH(d, a, b, c, M_offset_12, 11, T[45]);
  1843. c = HH(c, d, a, b, M_offset_15, 16, T[46]);
  1844. b = HH(b, c, d, a, M_offset_2, 23, T[47]);
  1845. a = II(a, b, c, d, M_offset_0, 6, T[48]);
  1846. d = II(d, a, b, c, M_offset_7, 10, T[49]);
  1847. c = II(c, d, a, b, M_offset_14, 15, T[50]);
  1848. b = II(b, c, d, a, M_offset_5, 21, T[51]);
  1849. a = II(a, b, c, d, M_offset_12, 6, T[52]);
  1850. d = II(d, a, b, c, M_offset_3, 10, T[53]);
  1851. c = II(c, d, a, b, M_offset_10, 15, T[54]);
  1852. b = II(b, c, d, a, M_offset_1, 21, T[55]);
  1853. a = II(a, b, c, d, M_offset_8, 6, T[56]);
  1854. d = II(d, a, b, c, M_offset_15, 10, T[57]);
  1855. c = II(c, d, a, b, M_offset_6, 15, T[58]);
  1856. b = II(b, c, d, a, M_offset_13, 21, T[59]);
  1857. a = II(a, b, c, d, M_offset_4, 6, T[60]);
  1858. d = II(d, a, b, c, M_offset_11, 10, T[61]);
  1859. c = II(c, d, a, b, M_offset_2, 15, T[62]);
  1860. b = II(b, c, d, a, M_offset_9, 21, T[63]);
  1861. // Intermediate hash value
  1862. H[0] = (H[0] + a) | 0;
  1863. H[1] = (H[1] + b) | 0;
  1864. H[2] = (H[2] + c) | 0;
  1865. H[3] = (H[3] + d) | 0;
  1866. },
  1867. _doFinalize: function () {
  1868. // Shortcuts
  1869. var data = this._data;
  1870. var dataWords = data.words;
  1871. var nBitsTotal = this._nDataBytes * 8;
  1872. var nBitsLeft = data.sigBytes * 8;
  1873. // Add padding
  1874. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  1875. var nBitsTotalH = Math.floor(nBitsTotal / 0x100000000);
  1876. var nBitsTotalL = nBitsTotal;
  1877. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = (
  1878. (((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff) |
  1879. (((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00)
  1880. );
  1881. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
  1882. (((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff) |
  1883. (((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00)
  1884. );
  1885. data.sigBytes = (dataWords.length + 1) * 4;
  1886. // Hash final blocks
  1887. this._process();
  1888. // Shortcuts
  1889. var hash = this._hash;
  1890. var H = hash.words;
  1891. // Swap endian
  1892. for (var i = 0; i < 4; i++) {
  1893. // Shortcut
  1894. var H_i = H[i];
  1895. H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
  1896. (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
  1897. }
  1898. // Return final computed hash
  1899. return hash;
  1900. },
  1901. clone: function () {
  1902. var clone = Hasher.clone.call(this);
  1903. clone._hash = this._hash.clone();
  1904. return clone;
  1905. }
  1906. });
  1907. function FF(a, b, c, d, x, s, t) {
  1908. var n = a + ((b & c) | (~b & d)) + x + t;
  1909. return ((n << s) | (n >>> (32 - s))) + b;
  1910. }
  1911. function GG(a, b, c, d, x, s, t) {
  1912. var n = a + ((b & d) | (c & ~d)) + x + t;
  1913. return ((n << s) | (n >>> (32 - s))) + b;
  1914. }
  1915. function HH(a, b, c, d, x, s, t) {
  1916. var n = a + (b ^ c ^ d) + x + t;
  1917. return ((n << s) | (n >>> (32 - s))) + b;
  1918. }
  1919. function II(a, b, c, d, x, s, t) {
  1920. var n = a + (c ^ (b | ~d)) + x + t;
  1921. return ((n << s) | (n >>> (32 - s))) + b;
  1922. }
  1923. /**
  1924. * Shortcut function to the hasher's object interface.
  1925. *
  1926. * @param {WordArray|string} message The message to hash.
  1927. *
  1928. * @return {WordArray} The hash.
  1929. *
  1930. * @static
  1931. *
  1932. * @example
  1933. *
  1934. * var hash = CryptoJS.MD5('message');
  1935. * var hash = CryptoJS.MD5(wordArray);
  1936. */
  1937. C.MD5 = Hasher._createHelper(MD5);
  1938. /**
  1939. * Shortcut function to the HMAC's object interface.
  1940. *
  1941. * @param {WordArray|string} message The message to hash.
  1942. * @param {WordArray|string} key The secret key.
  1943. *
  1944. * @return {WordArray} The HMAC.
  1945. *
  1946. * @static
  1947. *
  1948. * @example
  1949. *
  1950. * var hmac = CryptoJS.HmacMD5(message, key);
  1951. */
  1952. C.HmacMD5 = Hasher._createHmacHelper(MD5);
  1953. }(Math));