md5.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. "use strict";
  2. /*
  3. TypeScript Md5
  4. ==============
  5. Based on work by
  6. * Joseph Myers: http://www.myersdaily.org/joseph/javascript/md5-text.html
  7. * André Cruz: https://github.com/satazor/SparkMD5
  8. * Raymond Hill: https://github.com/gorhill/yamd5.js
  9. Effectively a TypeScrypt re-write of Raymond Hill JS Library
  10. The MIT License (MIT)
  11. Copyright (C) 2014 Raymond Hill
  12. Permission is hereby granted, free of charge, to any person obtaining a copy
  13. of this software and associated documentation files (the "Software"), to deal
  14. in the Software without restriction, including without limitation the rights
  15. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. copies of the Software, and to permit persons to whom the Software is
  17. furnished to do so, subject to the following conditions:
  18. The above copyright notice and this permission notice shall be included in
  19. all copies or substantial portions of the Software.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. THE SOFTWARE.
  27. DO WHAT YOU WANT TO PUBLIC LICENSE
  28. Version 2, December 2004
  29. Copyright (C) 2015 André Cruz <amdfcruz@gmail.com>
  30. Everyone is permitted to copy and distribute verbatim or modified
  31. copies of this license document, and changing it is allowed as long
  32. as the name is changed.
  33. DO WHAT YOU WANT TO PUBLIC LICENSE
  34. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  35. 0. You just DO WHAT YOU WANT TO.
  36. */
  37. Object.defineProperty(exports, "__esModule", { value: true });
  38. exports.Md5 = void 0;
  39. ;
  40. var Md5 = /** @class */ (function () {
  41. function Md5() {
  42. this._dataLength = 0;
  43. this._bufferLength = 0;
  44. this._state = new Int32Array(4);
  45. this._buffer = new ArrayBuffer(68);
  46. this._buffer8 = new Uint8Array(this._buffer, 0, 68);
  47. this._buffer32 = new Uint32Array(this._buffer, 0, 17);
  48. this.start();
  49. }
  50. Md5.hashStr = function (str, raw) {
  51. if (raw === void 0) { raw = false; }
  52. return this.onePassHasher
  53. .start()
  54. .appendStr(str)
  55. .end(raw);
  56. };
  57. Md5.hashAsciiStr = function (str, raw) {
  58. if (raw === void 0) { raw = false; }
  59. return this.onePassHasher
  60. .start()
  61. .appendAsciiStr(str)
  62. .end(raw);
  63. };
  64. Md5._hex = function (x) {
  65. var hc = Md5.hexChars;
  66. var ho = Md5.hexOut;
  67. var n;
  68. var offset;
  69. var j;
  70. var i;
  71. for (i = 0; i < 4; i += 1) {
  72. offset = i * 8;
  73. n = x[i];
  74. for (j = 0; j < 8; j += 2) {
  75. ho[offset + 1 + j] = hc.charAt(n & 0x0F);
  76. n >>>= 4;
  77. ho[offset + 0 + j] = hc.charAt(n & 0x0F);
  78. n >>>= 4;
  79. }
  80. }
  81. return ho.join('');
  82. };
  83. Md5._md5cycle = function (x, k) {
  84. var a = x[0];
  85. var b = x[1];
  86. var c = x[2];
  87. var d = x[3];
  88. // ff()
  89. a += (b & c | ~b & d) + k[0] - 680876936 | 0;
  90. a = (a << 7 | a >>> 25) + b | 0;
  91. d += (a & b | ~a & c) + k[1] - 389564586 | 0;
  92. d = (d << 12 | d >>> 20) + a | 0;
  93. c += (d & a | ~d & b) + k[2] + 606105819 | 0;
  94. c = (c << 17 | c >>> 15) + d | 0;
  95. b += (c & d | ~c & a) + k[3] - 1044525330 | 0;
  96. b = (b << 22 | b >>> 10) + c | 0;
  97. a += (b & c | ~b & d) + k[4] - 176418897 | 0;
  98. a = (a << 7 | a >>> 25) + b | 0;
  99. d += (a & b | ~a & c) + k[5] + 1200080426 | 0;
  100. d = (d << 12 | d >>> 20) + a | 0;
  101. c += (d & a | ~d & b) + k[6] - 1473231341 | 0;
  102. c = (c << 17 | c >>> 15) + d | 0;
  103. b += (c & d | ~c & a) + k[7] - 45705983 | 0;
  104. b = (b << 22 | b >>> 10) + c | 0;
  105. a += (b & c | ~b & d) + k[8] + 1770035416 | 0;
  106. a = (a << 7 | a >>> 25) + b | 0;
  107. d += (a & b | ~a & c) + k[9] - 1958414417 | 0;
  108. d = (d << 12 | d >>> 20) + a | 0;
  109. c += (d & a | ~d & b) + k[10] - 42063 | 0;
  110. c = (c << 17 | c >>> 15) + d | 0;
  111. b += (c & d | ~c & a) + k[11] - 1990404162 | 0;
  112. b = (b << 22 | b >>> 10) + c | 0;
  113. a += (b & c | ~b & d) + k[12] + 1804603682 | 0;
  114. a = (a << 7 | a >>> 25) + b | 0;
  115. d += (a & b | ~a & c) + k[13] - 40341101 | 0;
  116. d = (d << 12 | d >>> 20) + a | 0;
  117. c += (d & a | ~d & b) + k[14] - 1502002290 | 0;
  118. c = (c << 17 | c >>> 15) + d | 0;
  119. b += (c & d | ~c & a) + k[15] + 1236535329 | 0;
  120. b = (b << 22 | b >>> 10) + c | 0;
  121. // gg()
  122. a += (b & d | c & ~d) + k[1] - 165796510 | 0;
  123. a = (a << 5 | a >>> 27) + b | 0;
  124. d += (a & c | b & ~c) + k[6] - 1069501632 | 0;
  125. d = (d << 9 | d >>> 23) + a | 0;
  126. c += (d & b | a & ~b) + k[11] + 643717713 | 0;
  127. c = (c << 14 | c >>> 18) + d | 0;
  128. b += (c & a | d & ~a) + k[0] - 373897302 | 0;
  129. b = (b << 20 | b >>> 12) + c | 0;
  130. a += (b & d | c & ~d) + k[5] - 701558691 | 0;
  131. a = (a << 5 | a >>> 27) + b | 0;
  132. d += (a & c | b & ~c) + k[10] + 38016083 | 0;
  133. d = (d << 9 | d >>> 23) + a | 0;
  134. c += (d & b | a & ~b) + k[15] - 660478335 | 0;
  135. c = (c << 14 | c >>> 18) + d | 0;
  136. b += (c & a | d & ~a) + k[4] - 405537848 | 0;
  137. b = (b << 20 | b >>> 12) + c | 0;
  138. a += (b & d | c & ~d) + k[9] + 568446438 | 0;
  139. a = (a << 5 | a >>> 27) + b | 0;
  140. d += (a & c | b & ~c) + k[14] - 1019803690 | 0;
  141. d = (d << 9 | d >>> 23) + a | 0;
  142. c += (d & b | a & ~b) + k[3] - 187363961 | 0;
  143. c = (c << 14 | c >>> 18) + d | 0;
  144. b += (c & a | d & ~a) + k[8] + 1163531501 | 0;
  145. b = (b << 20 | b >>> 12) + c | 0;
  146. a += (b & d | c & ~d) + k[13] - 1444681467 | 0;
  147. a = (a << 5 | a >>> 27) + b | 0;
  148. d += (a & c | b & ~c) + k[2] - 51403784 | 0;
  149. d = (d << 9 | d >>> 23) + a | 0;
  150. c += (d & b | a & ~b) + k[7] + 1735328473 | 0;
  151. c = (c << 14 | c >>> 18) + d | 0;
  152. b += (c & a | d & ~a) + k[12] - 1926607734 | 0;
  153. b = (b << 20 | b >>> 12) + c | 0;
  154. // hh()
  155. a += (b ^ c ^ d) + k[5] - 378558 | 0;
  156. a = (a << 4 | a >>> 28) + b | 0;
  157. d += (a ^ b ^ c) + k[8] - 2022574463 | 0;
  158. d = (d << 11 | d >>> 21) + a | 0;
  159. c += (d ^ a ^ b) + k[11] + 1839030562 | 0;
  160. c = (c << 16 | c >>> 16) + d | 0;
  161. b += (c ^ d ^ a) + k[14] - 35309556 | 0;
  162. b = (b << 23 | b >>> 9) + c | 0;
  163. a += (b ^ c ^ d) + k[1] - 1530992060 | 0;
  164. a = (a << 4 | a >>> 28) + b | 0;
  165. d += (a ^ b ^ c) + k[4] + 1272893353 | 0;
  166. d = (d << 11 | d >>> 21) + a | 0;
  167. c += (d ^ a ^ b) + k[7] - 155497632 | 0;
  168. c = (c << 16 | c >>> 16) + d | 0;
  169. b += (c ^ d ^ a) + k[10] - 1094730640 | 0;
  170. b = (b << 23 | b >>> 9) + c | 0;
  171. a += (b ^ c ^ d) + k[13] + 681279174 | 0;
  172. a = (a << 4 | a >>> 28) + b | 0;
  173. d += (a ^ b ^ c) + k[0] - 358537222 | 0;
  174. d = (d << 11 | d >>> 21) + a | 0;
  175. c += (d ^ a ^ b) + k[3] - 722521979 | 0;
  176. c = (c << 16 | c >>> 16) + d | 0;
  177. b += (c ^ d ^ a) + k[6] + 76029189 | 0;
  178. b = (b << 23 | b >>> 9) + c | 0;
  179. a += (b ^ c ^ d) + k[9] - 640364487 | 0;
  180. a = (a << 4 | a >>> 28) + b | 0;
  181. d += (a ^ b ^ c) + k[12] - 421815835 | 0;
  182. d = (d << 11 | d >>> 21) + a | 0;
  183. c += (d ^ a ^ b) + k[15] + 530742520 | 0;
  184. c = (c << 16 | c >>> 16) + d | 0;
  185. b += (c ^ d ^ a) + k[2] - 995338651 | 0;
  186. b = (b << 23 | b >>> 9) + c | 0;
  187. // ii()
  188. a += (c ^ (b | ~d)) + k[0] - 198630844 | 0;
  189. a = (a << 6 | a >>> 26) + b | 0;
  190. d += (b ^ (a | ~c)) + k[7] + 1126891415 | 0;
  191. d = (d << 10 | d >>> 22) + a | 0;
  192. c += (a ^ (d | ~b)) + k[14] - 1416354905 | 0;
  193. c = (c << 15 | c >>> 17) + d | 0;
  194. b += (d ^ (c | ~a)) + k[5] - 57434055 | 0;
  195. b = (b << 21 | b >>> 11) + c | 0;
  196. a += (c ^ (b | ~d)) + k[12] + 1700485571 | 0;
  197. a = (a << 6 | a >>> 26) + b | 0;
  198. d += (b ^ (a | ~c)) + k[3] - 1894986606 | 0;
  199. d = (d << 10 | d >>> 22) + a | 0;
  200. c += (a ^ (d | ~b)) + k[10] - 1051523 | 0;
  201. c = (c << 15 | c >>> 17) + d | 0;
  202. b += (d ^ (c | ~a)) + k[1] - 2054922799 | 0;
  203. b = (b << 21 | b >>> 11) + c | 0;
  204. a += (c ^ (b | ~d)) + k[8] + 1873313359 | 0;
  205. a = (a << 6 | a >>> 26) + b | 0;
  206. d += (b ^ (a | ~c)) + k[15] - 30611744 | 0;
  207. d = (d << 10 | d >>> 22) + a | 0;
  208. c += (a ^ (d | ~b)) + k[6] - 1560198380 | 0;
  209. c = (c << 15 | c >>> 17) + d | 0;
  210. b += (d ^ (c | ~a)) + k[13] + 1309151649 | 0;
  211. b = (b << 21 | b >>> 11) + c | 0;
  212. a += (c ^ (b | ~d)) + k[4] - 145523070 | 0;
  213. a = (a << 6 | a >>> 26) + b | 0;
  214. d += (b ^ (a | ~c)) + k[11] - 1120210379 | 0;
  215. d = (d << 10 | d >>> 22) + a | 0;
  216. c += (a ^ (d | ~b)) + k[2] + 718787259 | 0;
  217. c = (c << 15 | c >>> 17) + d | 0;
  218. b += (d ^ (c | ~a)) + k[9] - 343485551 | 0;
  219. b = (b << 21 | b >>> 11) + c | 0;
  220. x[0] = a + x[0] | 0;
  221. x[1] = b + x[1] | 0;
  222. x[2] = c + x[2] | 0;
  223. x[3] = d + x[3] | 0;
  224. };
  225. /**
  226. * Initialise buffer to be hashed
  227. */
  228. Md5.prototype.start = function () {
  229. this._dataLength = 0;
  230. this._bufferLength = 0;
  231. this._state.set(Md5.stateIdentity);
  232. return this;
  233. };
  234. // Char to code point to to array conversion:
  235. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
  236. // #Example.3A_Fixing_charCodeAt_to_handle_non-Basic-Multilingual-Plane_characters_if_their_presence_earlier_in_the_string_is_unknown
  237. /**
  238. * Append a UTF-8 string to the hash buffer
  239. * @param str String to append
  240. */
  241. Md5.prototype.appendStr = function (str) {
  242. var buf8 = this._buffer8;
  243. var buf32 = this._buffer32;
  244. var bufLen = this._bufferLength;
  245. var code;
  246. var i;
  247. for (i = 0; i < str.length; i += 1) {
  248. code = str.charCodeAt(i);
  249. if (code < 128) {
  250. buf8[bufLen++] = code;
  251. }
  252. else if (code < 0x800) {
  253. buf8[bufLen++] = (code >>> 6) + 0xC0;
  254. buf8[bufLen++] = code & 0x3F | 0x80;
  255. }
  256. else if (code < 0xD800 || code > 0xDBFF) {
  257. buf8[bufLen++] = (code >>> 12) + 0xE0;
  258. buf8[bufLen++] = (code >>> 6 & 0x3F) | 0x80;
  259. buf8[bufLen++] = (code & 0x3F) | 0x80;
  260. }
  261. else {
  262. code = ((code - 0xD800) * 0x400) + (str.charCodeAt(++i) - 0xDC00) + 0x10000;
  263. if (code > 0x10FFFF) {
  264. throw new Error('Unicode standard supports code points up to U+10FFFF');
  265. }
  266. buf8[bufLen++] = (code >>> 18) + 0xF0;
  267. buf8[bufLen++] = (code >>> 12 & 0x3F) | 0x80;
  268. buf8[bufLen++] = (code >>> 6 & 0x3F) | 0x80;
  269. buf8[bufLen++] = (code & 0x3F) | 0x80;
  270. }
  271. if (bufLen >= 64) {
  272. this._dataLength += 64;
  273. Md5._md5cycle(this._state, buf32);
  274. bufLen -= 64;
  275. buf32[0] = buf32[16];
  276. }
  277. }
  278. this._bufferLength = bufLen;
  279. return this;
  280. };
  281. /**
  282. * Append an ASCII string to the hash buffer
  283. * @param str String to append
  284. */
  285. Md5.prototype.appendAsciiStr = function (str) {
  286. var buf8 = this._buffer8;
  287. var buf32 = this._buffer32;
  288. var bufLen = this._bufferLength;
  289. var i;
  290. var j = 0;
  291. for (;;) {
  292. i = Math.min(str.length - j, 64 - bufLen);
  293. while (i--) {
  294. buf8[bufLen++] = str.charCodeAt(j++);
  295. }
  296. if (bufLen < 64) {
  297. break;
  298. }
  299. this._dataLength += 64;
  300. Md5._md5cycle(this._state, buf32);
  301. bufLen = 0;
  302. }
  303. this._bufferLength = bufLen;
  304. return this;
  305. };
  306. /**
  307. * Append a byte array to the hash buffer
  308. * @param input array to append
  309. */
  310. Md5.prototype.appendByteArray = function (input) {
  311. var buf8 = this._buffer8;
  312. var buf32 = this._buffer32;
  313. var bufLen = this._bufferLength;
  314. var i;
  315. var j = 0;
  316. for (;;) {
  317. i = Math.min(input.length - j, 64 - bufLen);
  318. while (i--) {
  319. buf8[bufLen++] = input[j++];
  320. }
  321. if (bufLen < 64) {
  322. break;
  323. }
  324. this._dataLength += 64;
  325. Md5._md5cycle(this._state, buf32);
  326. bufLen = 0;
  327. }
  328. this._bufferLength = bufLen;
  329. return this;
  330. };
  331. /**
  332. * Get the state of the hash buffer
  333. */
  334. Md5.prototype.getState = function () {
  335. var s = this._state;
  336. return {
  337. buffer: String.fromCharCode.apply(null, Array.from(this._buffer8)),
  338. buflen: this._bufferLength,
  339. length: this._dataLength,
  340. state: [s[0], s[1], s[2], s[3]]
  341. };
  342. };
  343. /**
  344. * Override the current state of the hash buffer
  345. * @param state New hash buffer state
  346. */
  347. Md5.prototype.setState = function (state) {
  348. var buf = state.buffer;
  349. var x = state.state;
  350. var s = this._state;
  351. var i;
  352. this._dataLength = state.length;
  353. this._bufferLength = state.buflen;
  354. s[0] = x[0];
  355. s[1] = x[1];
  356. s[2] = x[2];
  357. s[3] = x[3];
  358. for (i = 0; i < buf.length; i += 1) {
  359. this._buffer8[i] = buf.charCodeAt(i);
  360. }
  361. };
  362. /**
  363. * Hash the current state of the hash buffer and return the result
  364. * @param raw Whether to return the value as an `Int32Array`
  365. */
  366. Md5.prototype.end = function (raw) {
  367. if (raw === void 0) { raw = false; }
  368. var bufLen = this._bufferLength;
  369. var buf8 = this._buffer8;
  370. var buf32 = this._buffer32;
  371. var i = (bufLen >> 2) + 1;
  372. this._dataLength += bufLen;
  373. var dataBitsLen = this._dataLength * 8;
  374. buf8[bufLen] = 0x80;
  375. buf8[bufLen + 1] = buf8[bufLen + 2] = buf8[bufLen + 3] = 0;
  376. buf32.set(Md5.buffer32Identity.subarray(i), i);
  377. if (bufLen > 55) {
  378. Md5._md5cycle(this._state, buf32);
  379. buf32.set(Md5.buffer32Identity);
  380. }
  381. // Do the final computation based on the tail and length
  382. // Beware that the final length may not fit in 32 bits so we take care of that
  383. if (dataBitsLen <= 0xFFFFFFFF) {
  384. buf32[14] = dataBitsLen;
  385. }
  386. else {
  387. var matches = dataBitsLen.toString(16).match(/(.*?)(.{0,8})$/);
  388. if (matches === null) {
  389. return;
  390. }
  391. var lo = parseInt(matches[2], 16);
  392. var hi = parseInt(matches[1], 16) || 0;
  393. buf32[14] = lo;
  394. buf32[15] = hi;
  395. }
  396. Md5._md5cycle(this._state, buf32);
  397. return raw ? this._state : Md5._hex(this._state);
  398. };
  399. // Private Static Variables
  400. Md5.stateIdentity = new Int32Array([1732584193, -271733879, -1732584194, 271733878]);
  401. Md5.buffer32Identity = new Int32Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
  402. Md5.hexChars = '0123456789abcdef';
  403. Md5.hexOut = [];
  404. // Permanent instance is to use for one-call hashing
  405. Md5.onePassHasher = new Md5();
  406. return Md5;
  407. }());
  408. exports.Md5 = Md5;
  409. if (Md5.hashStr('hello') !== '5d41402abc4b2a76b9719d911017c592') {
  410. throw new Error('Md5 self test failed.');
  411. }
  412. //# sourceMappingURL=md5.js.map