Md5.ts 15 KB

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