推广小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

876 lines
29 KiB

2 months ago
  1. /**
  2. * [js-md5]{@link https://github.com/emn178/js-md5}
  3. *
  4. * @namespace md5
  5. * @version 0.8.3
  6. * @author Chen, Yi-Cyuan [emn178@gmail.com]
  7. * @copyright Chen, Yi-Cyuan 2014-2023
  8. * @license MIT
  9. */
  10. (function () {
  11. 'use strict';
  12. var INPUT_ERROR = 'input is invalid type';
  13. var FINALIZE_ERROR = 'finalize already called';
  14. var WINDOW = typeof window === 'object';
  15. var root = WINDOW ? window : {};
  16. if (root.JS_MD5_NO_WINDOW) {
  17. WINDOW = false;
  18. }
  19. var WEB_WORKER = !WINDOW && typeof self === 'object';
  20. var NODE_JS = !root.JS_MD5_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
  21. if (NODE_JS) {
  22. root = global;
  23. } else if (WEB_WORKER) {
  24. root = self;
  25. }
  26. var COMMON_JS = !root.JS_MD5_NO_COMMON_JS && typeof module === 'object' && module.exports;
  27. var AMD = typeof define === 'function' && define.amd;
  28. var ARRAY_BUFFER = !root.JS_MD5_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
  29. var HEX_CHARS = '0123456789abcdef'.split('');
  30. var EXTRA = [128, 32768, 8388608, -2147483648];
  31. var SHIFT = [0, 8, 16, 24];
  32. var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer', 'base64'];
  33. var BASE64_ENCODE_CHAR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
  34. var blocks = [], buffer8;
  35. if (ARRAY_BUFFER) {
  36. var buffer = new ArrayBuffer(68);
  37. buffer8 = new Uint8Array(buffer);
  38. blocks = new Uint32Array(buffer);
  39. }
  40. var isArray = Array.isArray;
  41. if (root.JS_MD5_NO_NODE_JS || !isArray) {
  42. isArray = function (obj) {
  43. return Object.prototype.toString.call(obj) === '[object Array]';
  44. };
  45. }
  46. var isView = ArrayBuffer.isView;
  47. if (ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
  48. isView = function (obj) {
  49. return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
  50. };
  51. }
  52. // [message: string, isString: bool]
  53. var formatMessage = function (message) {
  54. var type = typeof message;
  55. if (type === 'string') {
  56. return [message, true];
  57. }
  58. if (type !== 'object' || message === null) {
  59. throw new Error(INPUT_ERROR);
  60. }
  61. if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
  62. return [new Uint8Array(message), false];
  63. }
  64. if (!isArray(message) && !isView(message)) {
  65. throw new Error(INPUT_ERROR);
  66. }
  67. return [message, false];
  68. }
  69. /**
  70. * @method hex
  71. * @memberof md5
  72. * @description Output hash as hex string
  73. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  74. * @returns {String} Hex string
  75. * @example
  76. * md5.hex('The quick brown fox jumps over the lazy dog');
  77. * // equal to
  78. * md5('The quick brown fox jumps over the lazy dog');
  79. */
  80. /**
  81. * @method digest
  82. * @memberof md5
  83. * @description Output hash as bytes array
  84. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  85. * @returns {Array} Bytes array
  86. * @example
  87. * md5.digest('The quick brown fox jumps over the lazy dog');
  88. */
  89. /**
  90. * @method array
  91. * @memberof md5
  92. * @description Output hash as bytes array
  93. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  94. * @returns {Array} Bytes array
  95. * @example
  96. * md5.array('The quick brown fox jumps over the lazy dog');
  97. */
  98. /**
  99. * @method arrayBuffer
  100. * @memberof md5
  101. * @description Output hash as ArrayBuffer
  102. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  103. * @returns {ArrayBuffer} ArrayBuffer
  104. * @example
  105. * md5.arrayBuffer('The quick brown fox jumps over the lazy dog');
  106. */
  107. /**
  108. * @method buffer
  109. * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
  110. * @memberof md5
  111. * @description Output hash as ArrayBuffer
  112. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  113. * @returns {ArrayBuffer} ArrayBuffer
  114. * @example
  115. * md5.buffer('The quick brown fox jumps over the lazy dog');
  116. */
  117. /**
  118. * @method base64
  119. * @memberof md5
  120. * @description Output hash as base64 string
  121. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  122. * @returns {String} base64 string
  123. * @example
  124. * md5.base64('The quick brown fox jumps over the lazy dog');
  125. */
  126. var createOutputMethod = function (outputType) {
  127. return function (message) {
  128. return new Md5(true).update(message)[outputType]();
  129. };
  130. };
  131. /**
  132. * @method create
  133. * @memberof md5
  134. * @description Create Md5 object
  135. * @returns {Md5} Md5 object.
  136. * @example
  137. * var hash = md5.create();
  138. */
  139. /**
  140. * @method update
  141. * @memberof md5
  142. * @description Create and update Md5 object
  143. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  144. * @returns {Md5} Md5 object.
  145. * @example
  146. * var hash = md5.update('The quick brown fox jumps over the lazy dog');
  147. * // equal to
  148. * var hash = md5.create();
  149. * hash.update('The quick brown fox jumps over the lazy dog');
  150. */
  151. var createMethod = function () {
  152. var method = createOutputMethod('hex');
  153. if (NODE_JS) {
  154. method = nodeWrap(method);
  155. }
  156. method.create = function () {
  157. return new Md5();
  158. };
  159. method.update = function (message) {
  160. return method.create().update(message);
  161. };
  162. for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
  163. var type = OUTPUT_TYPES[i];
  164. method[type] = createOutputMethod(type);
  165. }
  166. return method;
  167. };
  168. var nodeWrap = function (method) {
  169. var crypto = require('crypto')
  170. var Buffer = require('buffer').Buffer;
  171. var bufferFrom;
  172. if (Buffer.from && !root.JS_MD5_NO_BUFFER_FROM) {
  173. bufferFrom = Buffer.from;
  174. } else {
  175. bufferFrom = function (message) {
  176. return new Buffer(message);
  177. };
  178. }
  179. var nodeMethod = function (message) {
  180. if (typeof message === 'string') {
  181. return crypto.createHash('md5').update(message, 'utf8').digest('hex');
  182. } else {
  183. if (message === null || message === undefined) {
  184. throw new Error(INPUT_ERROR);
  185. } else if (message.constructor === ArrayBuffer) {
  186. message = new Uint8Array(message);
  187. }
  188. }
  189. if (isArray(message) || isView(message) ||
  190. message.constructor === Buffer) {
  191. return crypto.createHash('md5').update(bufferFrom(message)).digest('hex');
  192. } else {
  193. return method(message);
  194. }
  195. };
  196. return nodeMethod;
  197. };
  198. /**
  199. * @namespace md5.hmac
  200. */
  201. /**
  202. * @method hex
  203. * @memberof md5.hmac
  204. * @description Output hash as hex string
  205. * @param {String|Array|Uint8Array|ArrayBuffer} key key
  206. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  207. * @returns {String} Hex string
  208. * @example
  209. * md5.hmac.hex('key', 'The quick brown fox jumps over the lazy dog');
  210. * // equal to
  211. * md5.hmac('key', 'The quick brown fox jumps over the lazy dog');
  212. */
  213. /**
  214. * @method digest
  215. * @memberof md5.hmac
  216. * @description Output hash as bytes array
  217. * @param {String|Array|Uint8Array|ArrayBuffer} key key
  218. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  219. * @returns {Array} Bytes array
  220. * @example
  221. * md5.hmac.digest('key', 'The quick brown fox jumps over the lazy dog');
  222. */
  223. /**
  224. * @method array
  225. * @memberof md5.hmac
  226. * @description Output hash as bytes array
  227. * @param {String|Array|Uint8Array|ArrayBuffer} key key
  228. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  229. * @returns {Array} Bytes array
  230. * @example
  231. * md5.hmac.array('key', 'The quick brown fox jumps over the lazy dog');
  232. */
  233. /**
  234. * @method arrayBuffer
  235. * @memberof md5.hmac
  236. * @description Output hash as ArrayBuffer
  237. * @param {String|Array|Uint8Array|ArrayBuffer} key key
  238. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  239. * @returns {ArrayBuffer} ArrayBuffer
  240. * @example
  241. * md5.hmac.arrayBuffer('key', 'The quick brown fox jumps over the lazy dog');
  242. */
  243. /**
  244. * @method buffer
  245. * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
  246. * @memberof md5.hmac
  247. * @description Output hash as ArrayBuffer
  248. * @param {String|Array|Uint8Array|ArrayBuffer} key key
  249. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  250. * @returns {ArrayBuffer} ArrayBuffer
  251. * @example
  252. * md5.hmac.buffer('key', 'The quick brown fox jumps over the lazy dog');
  253. */
  254. /**
  255. * @method base64
  256. * @memberof md5.hmac
  257. * @description Output hash as base64 string
  258. * @param {String|Array|Uint8Array|ArrayBuffer} key key
  259. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  260. * @returns {String} base64 string
  261. * @example
  262. * md5.hmac.base64('key', 'The quick brown fox jumps over the lazy dog');
  263. */
  264. var createHmacOutputMethod = function (outputType) {
  265. return function (key, message) {
  266. return new HmacMd5(key, true).update(message)[outputType]();
  267. };
  268. };
  269. /**
  270. * @method create
  271. * @memberof md5.hmac
  272. * @description Create HmacMd5 object
  273. * @param {String|Array|Uint8Array|ArrayBuffer} key key
  274. * @returns {HmacMd5} HmacMd5 object.
  275. * @example
  276. * var hash = md5.hmac.create('key');
  277. */
  278. /**
  279. * @method update
  280. * @memberof md5.hmac
  281. * @description Create and update HmacMd5 object
  282. * @param {String|Array|Uint8Array|ArrayBuffer} key key
  283. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  284. * @returns {HmacMd5} HmacMd5 object.
  285. * @example
  286. * var hash = md5.hmac.update('key', 'The quick brown fox jumps over the lazy dog');
  287. * // equal to
  288. * var hash = md5.hmac.create('key');
  289. * hash.update('The quick brown fox jumps over the lazy dog');
  290. */
  291. var createHmacMethod = function () {
  292. var method = createHmacOutputMethod('hex');
  293. method.create = function (key) {
  294. return new HmacMd5(key);
  295. };
  296. method.update = function (key, message) {
  297. return method.create(key).update(message);
  298. };
  299. for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
  300. var type = OUTPUT_TYPES[i];
  301. method[type] = createHmacOutputMethod(type);
  302. }
  303. return method;
  304. };
  305. /**
  306. * Md5 class
  307. * @class Md5
  308. * @description This is internal class.
  309. * @see {@link md5.create}
  310. */
  311. function Md5(sharedMemory) {
  312. if (sharedMemory) {
  313. blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  314. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  315. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  316. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  317. this.blocks = blocks;
  318. this.buffer8 = buffer8;
  319. } else {
  320. if (ARRAY_BUFFER) {
  321. var buffer = new ArrayBuffer(68);
  322. this.buffer8 = new Uint8Array(buffer);
  323. this.blocks = new Uint32Array(buffer);
  324. } else {
  325. this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  326. }
  327. }
  328. this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = this.hBytes = 0;
  329. this.finalized = this.hashed = false;
  330. this.first = true;
  331. }
  332. /**
  333. * @method update
  334. * @memberof Md5
  335. * @instance
  336. * @description Update hash
  337. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  338. * @returns {Md5} Md5 object.
  339. * @see {@link md5.update}
  340. */
  341. Md5.prototype.update = function (message) {
  342. if (this.finalized) {
  343. throw new Error(FINALIZE_ERROR);
  344. }
  345. var result = formatMessage(message);
  346. message = result[0];
  347. var isString = result[1];
  348. var code, index = 0, i, length = message.length, blocks = this.blocks;
  349. var buffer8 = this.buffer8;
  350. while (index < length) {
  351. if (this.hashed) {
  352. this.hashed = false;
  353. blocks[0] = blocks[16];
  354. blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  355. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  356. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  357. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  358. }
  359. if (isString) {
  360. if (ARRAY_BUFFER) {
  361. for (i = this.start; index < length && i < 64; ++index) {
  362. code = message.charCodeAt(index);
  363. if (code < 0x80) {
  364. buffer8[i++] = code;
  365. } else if (code < 0x800) {
  366. buffer8[i++] = 0xc0 | (code >>> 6);
  367. buffer8[i++] = 0x80 | (code & 0x3f);
  368. } else if (code < 0xd800 || code >= 0xe000) {
  369. buffer8[i++] = 0xe0 | (code >>> 12);
  370. buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f);
  371. buffer8[i++] = 0x80 | (code & 0x3f);
  372. } else {
  373. code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
  374. buffer8[i++] = 0xf0 | (code >>> 18);
  375. buffer8[i++] = 0x80 | ((code >>> 12) & 0x3f);
  376. buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f);
  377. buffer8[i++] = 0x80 | (code & 0x3f);
  378. }
  379. }
  380. } else {
  381. for (i = this.start; index < length && i < 64; ++index) {
  382. code = message.charCodeAt(index);
  383. if (code < 0x80) {
  384. blocks[i >>> 2] |= code << SHIFT[i++ & 3];
  385. } else if (code < 0x800) {
  386. blocks[i >>> 2] |= (0xc0 | (code >>> 6)) << SHIFT[i++ & 3];
  387. blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  388. } else if (code < 0xd800 || code >= 0xe000) {
  389. blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3];
  390. blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
  391. blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  392. } else {
  393. code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
  394. blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3];
  395. blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3];
  396. blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
  397. blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  398. }
  399. }
  400. }
  401. } else {
  402. if (ARRAY_BUFFER) {
  403. for (i = this.start; index < length && i < 64; ++index) {
  404. buffer8[i++] = message[index];
  405. }
  406. } else {
  407. for (i = this.start; index < length && i < 64; ++index) {
  408. blocks[i >>> 2] |= message[index] << SHIFT[i++ & 3];
  409. }
  410. }
  411. }
  412. this.lastByteIndex = i;
  413. this.bytes += i - this.start;
  414. if (i >= 64) {
  415. this.start = i - 64;
  416. this.hash();
  417. this.hashed = true;
  418. } else {
  419. this.start = i;
  420. }
  421. }
  422. if (this.bytes > 4294967295) {
  423. this.hBytes += this.bytes / 4294967296 << 0;
  424. this.bytes = this.bytes % 4294967296;
  425. }
  426. return this;
  427. };
  428. Md5.prototype.finalize = function () {
  429. if (this.finalized) {
  430. return;
  431. }
  432. this.finalized = true;
  433. var blocks = this.blocks, i = this.lastByteIndex;
  434. blocks[i >>> 2] |= EXTRA[i & 3];
  435. if (i >= 56) {
  436. if (!this.hashed) {
  437. this.hash();
  438. }
  439. blocks[0] = blocks[16];
  440. blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  441. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  442. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  443. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  444. }
  445. blocks[14] = this.bytes << 3;
  446. blocks[15] = this.hBytes << 3 | this.bytes >>> 29;
  447. this.hash();
  448. };
  449. Md5.prototype.hash = function () {
  450. var a, b, c, d, bc, da, blocks = this.blocks;
  451. if (this.first) {
  452. a = blocks[0] - 680876937;
  453. a = (a << 7 | a >>> 25) - 271733879 << 0;
  454. d = (-1732584194 ^ a & 2004318071) + blocks[1] - 117830708;
  455. d = (d << 12 | d >>> 20) + a << 0;
  456. c = (-271733879 ^ (d & (a ^ -271733879))) + blocks[2] - 1126478375;
  457. c = (c << 17 | c >>> 15) + d << 0;
  458. b = (a ^ (c & (d ^ a))) + blocks[3] - 1316259209;
  459. b = (b << 22 | b >>> 10) + c << 0;
  460. } else {
  461. a = this.h0;
  462. b = this.h1;
  463. c = this.h2;
  464. d = this.h3;
  465. a += (d ^ (b & (c ^ d))) + blocks[0] - 680876936;
  466. a = (a << 7 | a >>> 25) + b << 0;
  467. d += (c ^ (a & (b ^ c))) + blocks[1] - 389564586;
  468. d = (d << 12 | d >>> 20) + a << 0;
  469. c += (b ^ (d & (a ^ b))) + blocks[2] + 606105819;
  470. c = (c << 17 | c >>> 15) + d << 0;
  471. b += (a ^ (c & (d ^ a))) + blocks[3] - 1044525330;
  472. b = (b << 22 | b >>> 10) + c << 0;
  473. }
  474. a += (d ^ (b & (c ^ d))) + blocks[4] - 176418897;
  475. a = (a << 7 | a >>> 25) + b << 0;
  476. d += (c ^ (a & (b ^ c))) + blocks[5] + 1200080426;
  477. d = (d << 12 | d >>> 20) + a << 0;
  478. c += (b ^ (d & (a ^ b))) + blocks[6] - 1473231341;
  479. c = (c << 17 | c >>> 15) + d << 0;
  480. b += (a ^ (c & (d ^ a))) + blocks[7] - 45705983;
  481. b = (b << 22 | b >>> 10) + c << 0;
  482. a += (d ^ (b & (c ^ d))) + blocks[8] + 1770035416;
  483. a = (a << 7 | a >>> 25) + b << 0;
  484. d += (c ^ (a & (b ^ c))) + blocks[9] - 1958414417;
  485. d = (d << 12 | d >>> 20) + a << 0;
  486. c += (b ^ (d & (a ^ b))) + blocks[10] - 42063;
  487. c = (c << 17 | c >>> 15) + d << 0;
  488. b += (a ^ (c & (d ^ a))) + blocks[11] - 1990404162;
  489. b = (b << 22 | b >>> 10) + c << 0;
  490. a += (d ^ (b & (c ^ d))) + blocks[12] + 1804603682;
  491. a = (a << 7 | a >>> 25) + b << 0;
  492. d += (c ^ (a & (b ^ c))) + blocks[13] - 40341101;
  493. d = (d << 12 | d >>> 20) + a << 0;
  494. c += (b ^ (d & (a ^ b))) + blocks[14] - 1502002290;
  495. c = (c << 17 | c >>> 15) + d << 0;
  496. b += (a ^ (c & (d ^ a))) + blocks[15] + 1236535329;
  497. b = (b << 22 | b >>> 10) + c << 0;
  498. a += (c ^ (d & (b ^ c))) + blocks[1] - 165796510;
  499. a = (a << 5 | a >>> 27) + b << 0;
  500. d += (b ^ (c & (a ^ b))) + blocks[6] - 1069501632;
  501. d = (d << 9 | d >>> 23) + a << 0;
  502. c += (a ^ (b & (d ^ a))) + blocks[11] + 643717713;
  503. c = (c << 14 | c >>> 18) + d << 0;
  504. b += (d ^ (a & (c ^ d))) + blocks[0] - 373897302;
  505. b = (b << 20 | b >>> 12) + c << 0;
  506. a += (c ^ (d & (b ^ c))) + blocks[5] - 701558691;
  507. a = (a << 5 | a >>> 27) + b << 0;
  508. d += (b ^ (c & (a ^ b))) + blocks[10] + 38016083;
  509. d = (d << 9 | d >>> 23) + a << 0;
  510. c += (a ^ (b & (d ^ a))) + blocks[15] - 660478335;
  511. c = (c << 14 | c >>> 18) + d << 0;
  512. b += (d ^ (a & (c ^ d))) + blocks[4] - 405537848;
  513. b = (b << 20 | b >>> 12) + c << 0;
  514. a += (c ^ (d & (b ^ c))) + blocks[9] + 568446438;
  515. a = (a << 5 | a >>> 27) + b << 0;
  516. d += (b ^ (c & (a ^ b))) + blocks[14] - 1019803690;
  517. d = (d << 9 | d >>> 23) + a << 0;
  518. c += (a ^ (b & (d ^ a))) + blocks[3] - 187363961;
  519. c = (c << 14 | c >>> 18) + d << 0;
  520. b += (d ^ (a & (c ^ d))) + blocks[8] + 1163531501;
  521. b = (b << 20 | b >>> 12) + c << 0;
  522. a += (c ^ (d & (b ^ c))) + blocks[13] - 1444681467;
  523. a = (a << 5 | a >>> 27) + b << 0;
  524. d += (b ^ (c & (a ^ b))) + blocks[2] - 51403784;
  525. d = (d << 9 | d >>> 23) + a << 0;
  526. c += (a ^ (b & (d ^ a))) + blocks[7] + 1735328473;
  527. c = (c << 14 | c >>> 18) + d << 0;
  528. b += (d ^ (a & (c ^ d))) + blocks[12] - 1926607734;
  529. b = (b << 20 | b >>> 12) + c << 0;
  530. bc = b ^ c;
  531. a += (bc ^ d) + blocks[5] - 378558;
  532. a = (a << 4 | a >>> 28) + b << 0;
  533. d += (bc ^ a) + blocks[8] - 2022574463;
  534. d = (d << 11 | d >>> 21) + a << 0;
  535. da = d ^ a;
  536. c += (da ^ b) + blocks[11] + 1839030562;
  537. c = (c << 16 | c >>> 16) + d << 0;
  538. b += (da ^ c) + blocks[14] - 35309556;
  539. b = (b << 23 | b >>> 9) + c << 0;
  540. bc = b ^ c;
  541. a += (bc ^ d) + blocks[1] - 1530992060;
  542. a = (a << 4 | a >>> 28) + b << 0;
  543. d += (bc ^ a) + blocks[4] + 1272893353;
  544. d = (d << 11 | d >>> 21) + a << 0;
  545. da = d ^ a;
  546. c += (da ^ b) + blocks[7] - 155497632;
  547. c = (c << 16 | c >>> 16) + d << 0;
  548. b += (da ^ c) + blocks[10] - 1094730640;
  549. b = (b << 23 | b >>> 9) + c << 0;
  550. bc = b ^ c;
  551. a += (bc ^ d) + blocks[13] + 681279174;
  552. a = (a << 4 | a >>> 28) + b << 0;
  553. d += (bc ^ a) + blocks[0] - 358537222;
  554. d = (d << 11 | d >>> 21) + a << 0;
  555. da = d ^ a;
  556. c += (da ^ b) + blocks[3] - 722521979;
  557. c = (c << 16 | c >>> 16) + d << 0;
  558. b += (da ^ c) + blocks[6] + 76029189;
  559. b = (b << 23 | b >>> 9) + c << 0;
  560. bc = b ^ c;
  561. a += (bc ^ d) + blocks[9] - 640364487;
  562. a = (a << 4 | a >>> 28) + b << 0;
  563. d += (bc ^ a) + blocks[12] - 421815835;
  564. d = (d << 11 | d >>> 21) + a << 0;
  565. da = d ^ a;
  566. c += (da ^ b) + blocks[15] + 530742520;
  567. c = (c << 16 | c >>> 16) + d << 0;
  568. b += (da ^ c) + blocks[2] - 995338651;
  569. b = (b << 23 | b >>> 9) + c << 0;
  570. a += (c ^ (b | ~d)) + blocks[0] - 198630844;
  571. a = (a << 6 | a >>> 26) + b << 0;
  572. d += (b ^ (a | ~c)) + blocks[7] + 1126891415;
  573. d = (d << 10 | d >>> 22) + a << 0;
  574. c += (a ^ (d | ~b)) + blocks[14] - 1416354905;
  575. c = (c << 15 | c >>> 17) + d << 0;
  576. b += (d ^ (c | ~a)) + blocks[5] - 57434055;
  577. b = (b << 21 | b >>> 11) + c << 0;
  578. a += (c ^ (b | ~d)) + blocks[12] + 1700485571;
  579. a = (a << 6 | a >>> 26) + b << 0;
  580. d += (b ^ (a | ~c)) + blocks[3] - 1894986606;
  581. d = (d << 10 | d >>> 22) + a << 0;
  582. c += (a ^ (d | ~b)) + blocks[10] - 1051523;
  583. c = (c << 15 | c >>> 17) + d << 0;
  584. b += (d ^ (c | ~a)) + blocks[1] - 2054922799;
  585. b = (b << 21 | b >>> 11) + c << 0;
  586. a += (c ^ (b | ~d)) + blocks[8] + 1873313359;
  587. a = (a << 6 | a >>> 26) + b << 0;
  588. d += (b ^ (a | ~c)) + blocks[15] - 30611744;
  589. d = (d << 10 | d >>> 22) + a << 0;
  590. c += (a ^ (d | ~b)) + blocks[6] - 1560198380;
  591. c = (c << 15 | c >>> 17) + d << 0;
  592. b += (d ^ (c | ~a)) + blocks[13] + 1309151649;
  593. b = (b << 21 | b >>> 11) + c << 0;
  594. a += (c ^ (b | ~d)) + blocks[4] - 145523070;
  595. a = (a << 6 | a >>> 26) + b << 0;
  596. d += (b ^ (a | ~c)) + blocks[11] - 1120210379;
  597. d = (d << 10 | d >>> 22) + a << 0;
  598. c += (a ^ (d | ~b)) + blocks[2] + 718787259;
  599. c = (c << 15 | c >>> 17) + d << 0;
  600. b += (d ^ (c | ~a)) + blocks[9] - 343485551;
  601. b = (b << 21 | b >>> 11) + c << 0;
  602. if (this.first) {
  603. this.h0 = a + 1732584193 << 0;
  604. this.h1 = b - 271733879 << 0;
  605. this.h2 = c - 1732584194 << 0;
  606. this.h3 = d + 271733878 << 0;
  607. this.first = false;
  608. } else {
  609. this.h0 = this.h0 + a << 0;
  610. this.h1 = this.h1 + b << 0;
  611. this.h2 = this.h2 + c << 0;
  612. this.h3 = this.h3 + d << 0;
  613. }
  614. };
  615. /**
  616. * @method hex
  617. * @memberof Md5
  618. * @instance
  619. * @description Output hash as hex string
  620. * @returns {String} Hex string
  621. * @see {@link md5.hex}
  622. * @example
  623. * hash.hex();
  624. */
  625. Md5.prototype.hex = function () {
  626. this.finalize();
  627. var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
  628. return HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
  629. HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] +
  630. HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] +
  631. HEX_CHARS[(h0 >>> 28) & 0x0F] + HEX_CHARS[(h0 >>> 24) & 0x0F] +
  632. HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
  633. HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] +
  634. HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] +
  635. HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] +
  636. HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
  637. HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] +
  638. HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] +
  639. HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] +
  640. HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
  641. HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] +
  642. HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] +
  643. HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F];
  644. };
  645. /**
  646. * @method toString
  647. * @memberof Md5
  648. * @instance
  649. * @description Output hash as hex string
  650. * @returns {String} Hex string
  651. * @see {@link md5.hex}
  652. * @example
  653. * hash.toString();
  654. */
  655. Md5.prototype.toString = Md5.prototype.hex;
  656. /**
  657. * @method digest
  658. * @memberof Md5
  659. * @instance
  660. * @description Output hash as bytes array
  661. * @returns {Array} Bytes array
  662. * @see {@link md5.digest}
  663. * @example
  664. * hash.digest();
  665. */
  666. Md5.prototype.digest = function () {
  667. this.finalize();
  668. var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
  669. return [
  670. h0 & 0xFF, (h0 >>> 8) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 24) & 0xFF,
  671. h1 & 0xFF, (h1 >>> 8) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 24) & 0xFF,
  672. h2 & 0xFF, (h2 >>> 8) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 24) & 0xFF,
  673. h3 & 0xFF, (h3 >>> 8) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 24) & 0xFF
  674. ];
  675. };
  676. /**
  677. * @method array
  678. * @memberof Md5
  679. * @instance
  680. * @description Output hash as bytes array
  681. * @returns {Array} Bytes array
  682. * @see {@link md5.array}
  683. * @example
  684. * hash.array();
  685. */
  686. Md5.prototype.array = Md5.prototype.digest;
  687. /**
  688. * @method arrayBuffer
  689. * @memberof Md5
  690. * @instance
  691. * @description Output hash as ArrayBuffer
  692. * @returns {ArrayBuffer} ArrayBuffer
  693. * @see {@link md5.arrayBuffer}
  694. * @example
  695. * hash.arrayBuffer();
  696. */
  697. Md5.prototype.arrayBuffer = function () {
  698. this.finalize();
  699. var buffer = new ArrayBuffer(16);
  700. var blocks = new Uint32Array(buffer);
  701. blocks[0] = this.h0;
  702. blocks[1] = this.h1;
  703. blocks[2] = this.h2;
  704. blocks[3] = this.h3;
  705. return buffer;
  706. };
  707. /**
  708. * @method buffer
  709. * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
  710. * @memberof Md5
  711. * @instance
  712. * @description Output hash as ArrayBuffer
  713. * @returns {ArrayBuffer} ArrayBuffer
  714. * @see {@link md5.buffer}
  715. * @example
  716. * hash.buffer();
  717. */
  718. Md5.prototype.buffer = Md5.prototype.arrayBuffer;
  719. /**
  720. * @method base64
  721. * @memberof Md5
  722. * @instance
  723. * @description Output hash as base64 string
  724. * @returns {String} base64 string
  725. * @see {@link md5.base64}
  726. * @example
  727. * hash.base64();
  728. */
  729. Md5.prototype.base64 = function () {
  730. var v1, v2, v3, base64Str = '', bytes = this.array();
  731. for (var i = 0; i < 15;) {
  732. v1 = bytes[i++];
  733. v2 = bytes[i++];
  734. v3 = bytes[i++];
  735. base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
  736. BASE64_ENCODE_CHAR[(v1 << 4 | v2 >>> 4) & 63] +
  737. BASE64_ENCODE_CHAR[(v2 << 2 | v3 >>> 6) & 63] +
  738. BASE64_ENCODE_CHAR[v3 & 63];
  739. }
  740. v1 = bytes[i];
  741. base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
  742. BASE64_ENCODE_CHAR[(v1 << 4) & 63] +
  743. '==';
  744. return base64Str;
  745. };
  746. /**
  747. * HmacMd5 class
  748. * @class HmacMd5
  749. * @extends Md5
  750. * @description This is internal class.
  751. * @see {@link md5.hmac.create}
  752. */
  753. function HmacMd5(key, sharedMemory) {
  754. var i, result = formatMessage(key);
  755. key = result[0];
  756. if (result[1]) {
  757. var bytes = [], length = key.length, index = 0, code;
  758. for (i = 0; i < length; ++i) {
  759. code = key.charCodeAt(i);
  760. if (code < 0x80) {
  761. bytes[index++] = code;
  762. } else if (code < 0x800) {
  763. bytes[index++] = (0xc0 | (code >>> 6));
  764. bytes[index++] = (0x80 | (code & 0x3f));
  765. } else if (code < 0xd800 || code >= 0xe000) {
  766. bytes[index++] = (0xe0 | (code >>> 12));
  767. bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
  768. bytes[index++] = (0x80 | (code & 0x3f));
  769. } else {
  770. code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
  771. bytes[index++] = (0xf0 | (code >>> 18));
  772. bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
  773. bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
  774. bytes[index++] = (0x80 | (code & 0x3f));
  775. }
  776. }
  777. key = bytes;
  778. }
  779. if (key.length > 64) {
  780. key = (new Md5(true)).update(key).array();
  781. }
  782. var oKeyPad = [], iKeyPad = [];
  783. for (i = 0; i < 64; ++i) {
  784. var b = key[i] || 0;
  785. oKeyPad[i] = 0x5c ^ b;
  786. iKeyPad[i] = 0x36 ^ b;
  787. }
  788. Md5.call(this, sharedMemory);
  789. this.update(iKeyPad);
  790. this.oKeyPad = oKeyPad;
  791. this.inner = true;
  792. this.sharedMemory = sharedMemory;
  793. }
  794. HmacMd5.prototype = new Md5();
  795. HmacMd5.prototype.finalize = function () {
  796. Md5.prototype.finalize.call(this);
  797. if (this.inner) {
  798. this.inner = false;
  799. var innerHash = this.array();
  800. Md5.call(this, this.sharedMemory);
  801. this.update(this.oKeyPad);
  802. this.update(innerHash);
  803. Md5.prototype.finalize.call(this);
  804. }
  805. };
  806. var exports = createMethod();
  807. exports.md5 = exports;
  808. exports.md5.hmac = createHmacMethod();
  809. if (COMMON_JS) {
  810. module.exports = exports;
  811. } else {
  812. /**
  813. * @method md5
  814. * @description Md5 hash function, export to global in browsers.
  815. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  816. * @returns {String} md5 hashes
  817. * @example
  818. * md5(''); // d41d8cd98f00b204e9800998ecf8427e
  819. * md5('The quick brown fox jumps over the lazy dog'); // 9e107d9d372bb6826bd81d3542a419d6
  820. * md5('The quick brown fox jumps over the lazy dog.'); // e4d909c290d0fb1ca068ffaddf22cbd0
  821. *
  822. * // It also supports UTF-8 encoding
  823. * md5('中文'); // a7bac2239fcdcb3a067903d8077c4a07
  824. *
  825. * // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
  826. * md5([]); // d41d8cd98f00b204e9800998ecf8427e
  827. * md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e
  828. */
  829. root.md5 = exports;
  830. if (AMD) {
  831. define(function () {
  832. return exports;
  833. });
  834. }
  835. }
  836. })();