md5.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /* https://github.com/emn178/js-md5 */
  2. (function () {
  3. 'use strict';
  4. var ERROR = 'input is invalid type';
  5. var WINDOW = typeof window === 'object';
  6. var root = WINDOW ? window : {};
  7. if (root.JS_MD5_NO_WINDOW) {
  8. WINDOW = false;
  9. }
  10. var WEB_WORKER = !WINDOW && typeof self === 'object';
  11. var NODE_JS = !root.JS_MD5_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
  12. if (NODE_JS) {
  13. root = global;
  14. } else if (WEB_WORKER) {
  15. root = self;
  16. }
  17. var COMMON_JS = !root.JS_MD5_NO_COMMON_JS && typeof module === 'object' && module.exports;
  18. var AMD = typeof define === 'function' && define.amd;
  19. var ARRAY_BUFFER = !root.JS_MD5_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
  20. var HEX_CHARS = '0123456789abcdef'.split('');
  21. var EXTRA = [128, 32768, 8388608, -2147483648];
  22. var SHIFT = [0, 8, 16, 24];
  23. var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer', 'base64'];
  24. var BASE64_ENCODE_CHAR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
  25. var blocks = [], buffer8;
  26. if (ARRAY_BUFFER) {
  27. var buffer = new ArrayBuffer(68);
  28. buffer8 = new Uint8Array(buffer);
  29. blocks = new Uint32Array(buffer);
  30. }
  31. if (root.JS_MD5_NO_NODE_JS || !Array.isArray) {
  32. Array.isArray = function (obj) {
  33. return Object.prototype.toString.call(obj) === '[object Array]';
  34. };
  35. }
  36. if (ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
  37. ArrayBuffer.isView = function (obj) {
  38. return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
  39. };
  40. }
  41. /**
  42. * @method hex
  43. * @memberof md5
  44. * @description Output hash as hex string
  45. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  46. * @returns {String} Hex string
  47. * @example
  48. * md5.hex('The quick brown fox jumps over the lazy dog');
  49. * // equal to
  50. * md5('The quick brown fox jumps over the lazy dog');
  51. */
  52. /**
  53. * @method digest
  54. * @memberof md5
  55. * @description Output hash as bytes array
  56. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  57. * @returns {Array} Bytes array
  58. * @example
  59. * md5.digest('The quick brown fox jumps over the lazy dog');
  60. */
  61. /**
  62. * @method array
  63. * @memberof md5
  64. * @description Output hash as bytes array
  65. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  66. * @returns {Array} Bytes array
  67. * @example
  68. * md5.array('The quick brown fox jumps over the lazy dog');
  69. */
  70. /**
  71. * @method arrayBuffer
  72. * @memberof md5
  73. * @description Output hash as ArrayBuffer
  74. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  75. * @returns {ArrayBuffer} ArrayBuffer
  76. * @example
  77. * md5.arrayBuffer('The quick brown fox jumps over the lazy dog');
  78. */
  79. /**
  80. * @method buffer
  81. * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
  82. * @memberof md5
  83. * @description Output hash as ArrayBuffer
  84. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  85. * @returns {ArrayBuffer} ArrayBuffer
  86. * @example
  87. * md5.buffer('The quick brown fox jumps over the lazy dog');
  88. */
  89. /**
  90. * @method base64
  91. * @memberof md5
  92. * @description Output hash as base64 string
  93. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  94. * @returns {String} base64 string
  95. * @example
  96. * md5.base64('The quick brown fox jumps over the lazy dog');
  97. */
  98. var createOutputMethod = function (outputType) {
  99. return function (message) {
  100. return new Md5(true).update(message)[outputType]();
  101. };
  102. };
  103. /**
  104. * @method create
  105. * @memberof md5
  106. * @description Create Md5 object
  107. * @returns {Md5} Md5 object.
  108. * @example
  109. * var hash = md5.create();
  110. */
  111. /**
  112. * @method update
  113. * @memberof md5
  114. * @description Create and update Md5 object
  115. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  116. * @returns {Md5} Md5 object.
  117. * @example
  118. * var hash = md5.update('The quick brown fox jumps over the lazy dog');
  119. * // equal to
  120. * var hash = md5.create();
  121. * hash.update('The quick brown fox jumps over the lazy dog');
  122. */
  123. var createMethod = function () {
  124. var method = createOutputMethod('hex');
  125. if (NODE_JS) {
  126. method = nodeWrap(method);
  127. }
  128. method.getCtx = method.create = function () {
  129. return new Md5();
  130. };
  131. method.update = function (message) {
  132. return method.create().update(message);
  133. };
  134. for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
  135. var type = OUTPUT_TYPES[i];
  136. method[type] = createOutputMethod(type);
  137. }
  138. return method;
  139. };
  140. var nodeWrap = function (method) {
  141. var crypto = eval("require('crypto')");
  142. var Buffer = eval("require('buffer').Buffer");
  143. var nodeMethod = function (message) {
  144. if (typeof message === 'string') {
  145. return crypto.createHash('md5').update(message, 'utf8').digest('hex');
  146. } else {
  147. if (message === null || message === undefined) {
  148. throw ERROR;
  149. } else if (message.constructor === ArrayBuffer) {
  150. message = new Uint8Array(message);
  151. }
  152. }
  153. if (Array.isArray(message) || ArrayBuffer.isView(message) ||
  154. message.constructor === Buffer) {
  155. return crypto.createHash('md5').update(new Buffer(message)).digest('hex');
  156. } else {
  157. return method(message);
  158. }
  159. };
  160. return nodeMethod;
  161. };
  162. /**
  163. * Md5 class
  164. * @class Md5
  165. * @description This is internal class.
  166. * @see {@link md5.create}
  167. */
  168. function Md5(sharedMemory) {
  169. if (sharedMemory) {
  170. blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  171. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  172. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  173. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  174. this.blocks = blocks;
  175. this.buffer8 = buffer8;
  176. } else {
  177. if (ARRAY_BUFFER) {
  178. var buffer = new ArrayBuffer(68);
  179. this.buffer8 = new Uint8Array(buffer);
  180. this.blocks = new Uint32Array(buffer);
  181. } else {
  182. this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  183. }
  184. }
  185. this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = this.hBytes = 0;
  186. this.finalized = this.hashed = false;
  187. this.first = true;
  188. }
  189. /**
  190. * @method update
  191. * @memberof Md5
  192. * @instance
  193. * @description Update hash
  194. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  195. * @returns {Md5} Md5 object.
  196. * @see {@link md5.update}
  197. */
  198. Md5.prototype.update = function (message) {
  199. if (this.finalized) {
  200. return;
  201. }
  202. var notString, type = typeof message;
  203. if (type !== 'string') {
  204. if (type === 'object') {
  205. if (message === null) {
  206. throw ERROR;
  207. } else if (ARRAY_BUFFER && (message.constructor === ArrayBuffer || message.constructor.name === 'ArrayBuffer')) {
  208. message = new Uint8Array(message);
  209. } else if (!Array.isArray(message)) {
  210. if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
  211. throw ERROR;
  212. }
  213. }
  214. } else {
  215. throw ERROR;
  216. }
  217. notString = true;
  218. }
  219. var code, index = 0, i, length = message.length, blocks = this.blocks;
  220. var buffer8 = this.buffer8;
  221. while (index < length) {
  222. if (this.hashed) {
  223. this.hashed = false;
  224. blocks[0] = blocks[16];
  225. blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  226. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  227. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  228. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  229. }
  230. if (notString) {
  231. if (ARRAY_BUFFER) {
  232. for (i = this.start; index < length && i < 64; ++index) {
  233. buffer8[i++] = message[index];
  234. }
  235. } else {
  236. for (i = this.start; index < length && i < 64; ++index) {
  237. blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
  238. }
  239. }
  240. } else {
  241. if (ARRAY_BUFFER) {
  242. for (i = this.start; index < length && i < 64; ++index) {
  243. code = message.charCodeAt(index);
  244. if (code < 0x80) {
  245. buffer8[i++] = code;
  246. } else if (code < 0x800) {
  247. buffer8[i++] = 0xc0 | (code >> 6);
  248. buffer8[i++] = 0x80 | (code & 0x3f);
  249. } else if (code < 0xd800 || code >= 0xe000) {
  250. buffer8[i++] = 0xe0 | (code >> 12);
  251. buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
  252. buffer8[i++] = 0x80 | (code & 0x3f);
  253. } else {
  254. code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
  255. buffer8[i++] = 0xf0 | (code >> 18);
  256. buffer8[i++] = 0x80 | ((code >> 12) & 0x3f);
  257. buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
  258. buffer8[i++] = 0x80 | (code & 0x3f);
  259. }
  260. }
  261. } else {
  262. for (i = this.start; index < length && i < 64; ++index) {
  263. code = message.charCodeAt(index);
  264. if (code < 0x80) {
  265. blocks[i >> 2] |= code << SHIFT[i++ & 3];
  266. } else if (code < 0x800) {
  267. blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
  268. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  269. } else if (code < 0xd800 || code >= 0xe000) {
  270. blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
  271. blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
  272. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  273. } else {
  274. code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
  275. blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
  276. blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
  277. blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
  278. blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
  279. }
  280. }
  281. }
  282. }
  283. this.lastByteIndex = i;
  284. this.bytes += i - this.start;
  285. if (i >= 64) {
  286. this.start = i - 64;
  287. this.hash();
  288. this.hashed = true;
  289. } else {
  290. this.start = i;
  291. }
  292. }
  293. if (this.bytes > 4294967295) {
  294. this.hBytes += this.bytes / 4294967296 << 0;
  295. this.bytes = this.bytes % 4294967296;
  296. }
  297. return this;
  298. };
  299. Md5.prototype.finalize = function () {
  300. if (this.finalized) {
  301. return;
  302. }
  303. this.finalized = true;
  304. var blocks = this.blocks, i = this.lastByteIndex;
  305. blocks[i >> 2] |= EXTRA[i & 3];
  306. if (i >= 56) {
  307. if (!this.hashed) {
  308. this.hash();
  309. }
  310. blocks[0] = blocks[16];
  311. blocks[16] = blocks[1] = blocks[2] = blocks[3] =
  312. blocks[4] = blocks[5] = blocks[6] = blocks[7] =
  313. blocks[8] = blocks[9] = blocks[10] = blocks[11] =
  314. blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
  315. }
  316. blocks[14] = this.bytes << 3;
  317. blocks[15] = this.hBytes << 3 | this.bytes >>> 29;
  318. this.hash();
  319. };
  320. Md5.prototype.hash = function () {
  321. var a, b, c, d, bc, da, blocks = this.blocks;
  322. if (this.first) {
  323. a = blocks[0] - 680876937;
  324. a = (a << 7 | a >>> 25) - 271733879 << 0;
  325. d = (-1732584194 ^ a & 2004318071) + blocks[1] - 117830708;
  326. d = (d << 12 | d >>> 20) + a << 0;
  327. c = (-271733879 ^ (d & (a ^ -271733879))) + blocks[2] - 1126478375;
  328. c = (c << 17 | c >>> 15) + d << 0;
  329. b = (a ^ (c & (d ^ a))) + blocks[3] - 1316259209;
  330. b = (b << 22 | b >>> 10) + c << 0;
  331. } else {
  332. a = this.h0;
  333. b = this.h1;
  334. c = this.h2;
  335. d = this.h3;
  336. a += (d ^ (b & (c ^ d))) + blocks[0] - 680876936;
  337. a = (a << 7 | a >>> 25) + b << 0;
  338. d += (c ^ (a & (b ^ c))) + blocks[1] - 389564586;
  339. d = (d << 12 | d >>> 20) + a << 0;
  340. c += (b ^ (d & (a ^ b))) + blocks[2] + 606105819;
  341. c = (c << 17 | c >>> 15) + d << 0;
  342. b += (a ^ (c & (d ^ a))) + blocks[3] - 1044525330;
  343. b = (b << 22 | b >>> 10) + c << 0;
  344. }
  345. a += (d ^ (b & (c ^ d))) + blocks[4] - 176418897;
  346. a = (a << 7 | a >>> 25) + b << 0;
  347. d += (c ^ (a & (b ^ c))) + blocks[5] + 1200080426;
  348. d = (d << 12 | d >>> 20) + a << 0;
  349. c += (b ^ (d & (a ^ b))) + blocks[6] - 1473231341;
  350. c = (c << 17 | c >>> 15) + d << 0;
  351. b += (a ^ (c & (d ^ a))) + blocks[7] - 45705983;
  352. b = (b << 22 | b >>> 10) + c << 0;
  353. a += (d ^ (b & (c ^ d))) + blocks[8] + 1770035416;
  354. a = (a << 7 | a >>> 25) + b << 0;
  355. d += (c ^ (a & (b ^ c))) + blocks[9] - 1958414417;
  356. d = (d << 12 | d >>> 20) + a << 0;
  357. c += (b ^ (d & (a ^ b))) + blocks[10] - 42063;
  358. c = (c << 17 | c >>> 15) + d << 0;
  359. b += (a ^ (c & (d ^ a))) + blocks[11] - 1990404162;
  360. b = (b << 22 | b >>> 10) + c << 0;
  361. a += (d ^ (b & (c ^ d))) + blocks[12] + 1804603682;
  362. a = (a << 7 | a >>> 25) + b << 0;
  363. d += (c ^ (a & (b ^ c))) + blocks[13] - 40341101;
  364. d = (d << 12 | d >>> 20) + a << 0;
  365. c += (b ^ (d & (a ^ b))) + blocks[14] - 1502002290;
  366. c = (c << 17 | c >>> 15) + d << 0;
  367. b += (a ^ (c & (d ^ a))) + blocks[15] + 1236535329;
  368. b = (b << 22 | b >>> 10) + c << 0;
  369. a += (c ^ (d & (b ^ c))) + blocks[1] - 165796510;
  370. a = (a << 5 | a >>> 27) + b << 0;
  371. d += (b ^ (c & (a ^ b))) + blocks[6] - 1069501632;
  372. d = (d << 9 | d >>> 23) + a << 0;
  373. c += (a ^ (b & (d ^ a))) + blocks[11] + 643717713;
  374. c = (c << 14 | c >>> 18) + d << 0;
  375. b += (d ^ (a & (c ^ d))) + blocks[0] - 373897302;
  376. b = (b << 20 | b >>> 12) + c << 0;
  377. a += (c ^ (d & (b ^ c))) + blocks[5] - 701558691;
  378. a = (a << 5 | a >>> 27) + b << 0;
  379. d += (b ^ (c & (a ^ b))) + blocks[10] + 38016083;
  380. d = (d << 9 | d >>> 23) + a << 0;
  381. c += (a ^ (b & (d ^ a))) + blocks[15] - 660478335;
  382. c = (c << 14 | c >>> 18) + d << 0;
  383. b += (d ^ (a & (c ^ d))) + blocks[4] - 405537848;
  384. b = (b << 20 | b >>> 12) + c << 0;
  385. a += (c ^ (d & (b ^ c))) + blocks[9] + 568446438;
  386. a = (a << 5 | a >>> 27) + b << 0;
  387. d += (b ^ (c & (a ^ b))) + blocks[14] - 1019803690;
  388. d = (d << 9 | d >>> 23) + a << 0;
  389. c += (a ^ (b & (d ^ a))) + blocks[3] - 187363961;
  390. c = (c << 14 | c >>> 18) + d << 0;
  391. b += (d ^ (a & (c ^ d))) + blocks[8] + 1163531501;
  392. b = (b << 20 | b >>> 12) + c << 0;
  393. a += (c ^ (d & (b ^ c))) + blocks[13] - 1444681467;
  394. a = (a << 5 | a >>> 27) + b << 0;
  395. d += (b ^ (c & (a ^ b))) + blocks[2] - 51403784;
  396. d = (d << 9 | d >>> 23) + a << 0;
  397. c += (a ^ (b & (d ^ a))) + blocks[7] + 1735328473;
  398. c = (c << 14 | c >>> 18) + d << 0;
  399. b += (d ^ (a & (c ^ d))) + blocks[12] - 1926607734;
  400. b = (b << 20 | b >>> 12) + c << 0;
  401. bc = b ^ c;
  402. a += (bc ^ d) + blocks[5] - 378558;
  403. a = (a << 4 | a >>> 28) + b << 0;
  404. d += (bc ^ a) + blocks[8] - 2022574463;
  405. d = (d << 11 | d >>> 21) + a << 0;
  406. da = d ^ a;
  407. c += (da ^ b) + blocks[11] + 1839030562;
  408. c = (c << 16 | c >>> 16) + d << 0;
  409. b += (da ^ c) + blocks[14] - 35309556;
  410. b = (b << 23 | b >>> 9) + c << 0;
  411. bc = b ^ c;
  412. a += (bc ^ d) + blocks[1] - 1530992060;
  413. a = (a << 4 | a >>> 28) + b << 0;
  414. d += (bc ^ a) + blocks[4] + 1272893353;
  415. d = (d << 11 | d >>> 21) + a << 0;
  416. da = d ^ a;
  417. c += (da ^ b) + blocks[7] - 155497632;
  418. c = (c << 16 | c >>> 16) + d << 0;
  419. b += (da ^ c) + blocks[10] - 1094730640;
  420. b = (b << 23 | b >>> 9) + c << 0;
  421. bc = b ^ c;
  422. a += (bc ^ d) + blocks[13] + 681279174;
  423. a = (a << 4 | a >>> 28) + b << 0;
  424. d += (bc ^ a) + blocks[0] - 358537222;
  425. d = (d << 11 | d >>> 21) + a << 0;
  426. da = d ^ a;
  427. c += (da ^ b) + blocks[3] - 722521979;
  428. c = (c << 16 | c >>> 16) + d << 0;
  429. b += (da ^ c) + blocks[6] + 76029189;
  430. b = (b << 23 | b >>> 9) + c << 0;
  431. bc = b ^ c;
  432. a += (bc ^ d) + blocks[9] - 640364487;
  433. a = (a << 4 | a >>> 28) + b << 0;
  434. d += (bc ^ a) + blocks[12] - 421815835;
  435. d = (d << 11 | d >>> 21) + a << 0;
  436. da = d ^ a;
  437. c += (da ^ b) + blocks[15] + 530742520;
  438. c = (c << 16 | c >>> 16) + d << 0;
  439. b += (da ^ c) + blocks[2] - 995338651;
  440. b = (b << 23 | b >>> 9) + c << 0;
  441. a += (c ^ (b | ~d)) + blocks[0] - 198630844;
  442. a = (a << 6 | a >>> 26) + b << 0;
  443. d += (b ^ (a | ~c)) + blocks[7] + 1126891415;
  444. d = (d << 10 | d >>> 22) + a << 0;
  445. c += (a ^ (d | ~b)) + blocks[14] - 1416354905;
  446. c = (c << 15 | c >>> 17) + d << 0;
  447. b += (d ^ (c | ~a)) + blocks[5] - 57434055;
  448. b = (b << 21 | b >>> 11) + c << 0;
  449. a += (c ^ (b | ~d)) + blocks[12] + 1700485571;
  450. a = (a << 6 | a >>> 26) + b << 0;
  451. d += (b ^ (a | ~c)) + blocks[3] - 1894986606;
  452. d = (d << 10 | d >>> 22) + a << 0;
  453. c += (a ^ (d | ~b)) + blocks[10] - 1051523;
  454. c = (c << 15 | c >>> 17) + d << 0;
  455. b += (d ^ (c | ~a)) + blocks[1] - 2054922799;
  456. b = (b << 21 | b >>> 11) + c << 0;
  457. a += (c ^ (b | ~d)) + blocks[8] + 1873313359;
  458. a = (a << 6 | a >>> 26) + b << 0;
  459. d += (b ^ (a | ~c)) + blocks[15] - 30611744;
  460. d = (d << 10 | d >>> 22) + a << 0;
  461. c += (a ^ (d | ~b)) + blocks[6] - 1560198380;
  462. c = (c << 15 | c >>> 17) + d << 0;
  463. b += (d ^ (c | ~a)) + blocks[13] + 1309151649;
  464. b = (b << 21 | b >>> 11) + c << 0;
  465. a += (c ^ (b | ~d)) + blocks[4] - 145523070;
  466. a = (a << 6 | a >>> 26) + b << 0;
  467. d += (b ^ (a | ~c)) + blocks[11] - 1120210379;
  468. d = (d << 10 | d >>> 22) + a << 0;
  469. c += (a ^ (d | ~b)) + blocks[2] + 718787259;
  470. c = (c << 15 | c >>> 17) + d << 0;
  471. b += (d ^ (c | ~a)) + blocks[9] - 343485551;
  472. b = (b << 21 | b >>> 11) + c << 0;
  473. if (this.first) {
  474. this.h0 = a + 1732584193 << 0;
  475. this.h1 = b - 271733879 << 0;
  476. this.h2 = c - 1732584194 << 0;
  477. this.h3 = d + 271733878 << 0;
  478. this.first = false;
  479. } else {
  480. this.h0 = this.h0 + a << 0;
  481. this.h1 = this.h1 + b << 0;
  482. this.h2 = this.h2 + c << 0;
  483. this.h3 = this.h3 + d << 0;
  484. }
  485. };
  486. /**
  487. * @method hex
  488. * @memberof Md5
  489. * @instance
  490. * @description Output hash as hex string
  491. * @returns {String} Hex string
  492. * @see {@link md5.hex}
  493. * @example
  494. * hash.hex();
  495. */
  496. Md5.prototype.hex = function () {
  497. this.finalize();
  498. var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
  499. return HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
  500. HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
  501. HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
  502. HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
  503. HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
  504. HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
  505. HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
  506. HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
  507. HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
  508. HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
  509. HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
  510. HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
  511. HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
  512. HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
  513. HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
  514. HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F];
  515. };
  516. /**
  517. * @method toString
  518. * @memberof Md5
  519. * @instance
  520. * @description Output hash as hex string
  521. * @returns {String} Hex string
  522. * @see {@link md5.hex}
  523. * @example
  524. * hash.toString();
  525. */
  526. Md5.prototype.toString = Md5.prototype.hex;
  527. /**
  528. * @method digest
  529. * @memberof Md5
  530. * @instance
  531. * @description Output hash as bytes array
  532. * @returns {Array} Bytes array
  533. * @see {@link md5.digest}
  534. * @example
  535. * hash.digest();
  536. */
  537. Md5.prototype.digest = function () {
  538. this.finalize();
  539. var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
  540. return [
  541. h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 24) & 0xFF,
  542. h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 24) & 0xFF,
  543. h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 24) & 0xFF,
  544. h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 24) & 0xFF
  545. ];
  546. };
  547. /**
  548. * @method array
  549. * @memberof Md5
  550. * @instance
  551. * @description Output hash as bytes array
  552. * @returns {Array} Bytes array
  553. * @see {@link md5.array}
  554. * @example
  555. * hash.array();
  556. */
  557. Md5.prototype.array = Md5.prototype.digest;
  558. /**
  559. * @method arrayBuffer
  560. * @memberof Md5
  561. * @instance
  562. * @description Output hash as ArrayBuffer
  563. * @returns {ArrayBuffer} ArrayBuffer
  564. * @see {@link md5.arrayBuffer}
  565. * @example
  566. * hash.arrayBuffer();
  567. */
  568. Md5.prototype.arrayBuffer = function () {
  569. this.finalize();
  570. var buffer = new ArrayBuffer(16);
  571. var blocks = new Uint32Array(buffer);
  572. blocks[0] = this.h0;
  573. blocks[1] = this.h1;
  574. blocks[2] = this.h2;
  575. blocks[3] = this.h3;
  576. return buffer;
  577. };
  578. /**
  579. * @method buffer
  580. * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
  581. * @memberof Md5
  582. * @instance
  583. * @description Output hash as ArrayBuffer
  584. * @returns {ArrayBuffer} ArrayBuffer
  585. * @see {@link md5.buffer}
  586. * @example
  587. * hash.buffer();
  588. */
  589. Md5.prototype.buffer = Md5.prototype.arrayBuffer;
  590. /**
  591. * @method base64
  592. * @memberof Md5
  593. * @instance
  594. * @description Output hash as base64 string
  595. * @returns {String} base64 string
  596. * @see {@link md5.base64}
  597. * @example
  598. * hash.base64();
  599. */
  600. Md5.prototype.base64 = function () {
  601. var v1, v2, v3, base64Str = '', bytes = this.array();
  602. for (var i = 0; i < 15;) {
  603. v1 = bytes[i++];
  604. v2 = bytes[i++];
  605. v3 = bytes[i++];
  606. base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
  607. BASE64_ENCODE_CHAR[(v1 << 4 | v2 >>> 4) & 63] +
  608. BASE64_ENCODE_CHAR[(v2 << 2 | v3 >>> 6) & 63] +
  609. BASE64_ENCODE_CHAR[v3 & 63];
  610. }
  611. v1 = bytes[i];
  612. base64Str += BASE64_ENCODE_CHAR[v1 >>> 2] +
  613. BASE64_ENCODE_CHAR[(v1 << 4) & 63] +
  614. '==';
  615. return base64Str;
  616. };
  617. var exports = createMethod();
  618. if (COMMON_JS) {
  619. module.exports = exports;
  620. } else {
  621. /**
  622. * @method md5
  623. * @description Md5 hash function, export to global in browsers.
  624. * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
  625. * @returns {String} md5 hashes
  626. * @example
  627. * md5(''); // d41d8cd98f00b204e9800998ecf8427e
  628. * md5('The quick brown fox jumps over the lazy dog'); // 9e107d9d372bb6826bd81d3542a419d6
  629. * md5('The quick brown fox jumps over the lazy dog.'); // e4d909c290d0fb1ca068ffaddf22cbd0
  630. *
  631. * // It also supports UTF-8 encoding
  632. * md5('中文'); // a7bac2239fcdcb3a067903d8077c4a07
  633. *
  634. * // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
  635. * md5([]); // d41d8cd98f00b204e9800998ecf8427e
  636. * md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e
  637. */
  638. root.md5 = exports;
  639. if (AMD) {
  640. define(function () {
  641. return exports;
  642. });
  643. }
  644. }
  645. })();