md5.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  3. * Digest Algorithm, as defined in RFC 1321.
  4. * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002.
  5. * Code also contributed by Greg Holt
  6. * See http://pajhome.org.uk/site/legal.html for details.
  7. */
  8. /*
  9. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  10. * to work around bugs in some JS interpreters.
  11. */
  12. function safe_add(x, y)
  13. {
  14. var lsw = (x & 0xFFFF) + (y & 0xFFFF)
  15. var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
  16. return (msw << 16) | (lsw & 0xFFFF)
  17. }
  18. /*
  19. * Bitwise rotate a 32-bit number to the left.
  20. */
  21. function rol(num, cnt)
  22. {
  23. return (num << cnt) | (num >>> (32 - cnt))
  24. }
  25. /*
  26. * These functions implement the four basic operations the algorithm uses.
  27. */
  28. function cmn(q, a, b, x, s, t)
  29. {
  30. return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)
  31. }
  32. function ff(a, b, c, d, x, s, t)
  33. {
  34. return cmn((b & c) | ((~b) & d), a, b, x, s, t)
  35. }
  36. function gg(a, b, c, d, x, s, t)
  37. {
  38. return cmn((b & d) | (c & (~d)), a, b, x, s, t)
  39. }
  40. function hh(a, b, c, d, x, s, t)
  41. {
  42. return cmn(b ^ c ^ d, a, b, x, s, t)
  43. }
  44. function ii(a, b, c, d, x, s, t)
  45. {
  46. return cmn(c ^ (b | (~d)), a, b, x, s, t)
  47. }
  48. /*
  49. * Calculate the MD5 of an array of little-endian words, producing an array
  50. * of little-endian words.
  51. */
  52. function coreMD5(x)
  53. {
  54. var a = 1732584193
  55. var b = -271733879
  56. var c = -1732584194
  57. var d = 271733878
  58. for(var i = 0; i < x.length; i += 16)
  59. {
  60. var olda = a
  61. var oldb = b
  62. var oldc = c
  63. var oldd = d
  64. a = ff(a, b, c, d, x[i+ 0], 7 , -680876936)
  65. d = ff(d, a, b, c, x[i+ 1], 12, -389564586)
  66. c = ff(c, d, a, b, x[i+ 2], 17, 606105819)
  67. b = ff(b, c, d, a, x[i+ 3], 22, -1044525330)
  68. a = ff(a, b, c, d, x[i+ 4], 7 , -176418897)
  69. d = ff(d, a, b, c, x[i+ 5], 12, 1200080426)
  70. c = ff(c, d, a, b, x[i+ 6], 17, -1473231341)
  71. b = ff(b, c, d, a, x[i+ 7], 22, -45705983)
  72. a = ff(a, b, c, d, x[i+ 8], 7 , 1770035416)
  73. d = ff(d, a, b, c, x[i+ 9], 12, -1958414417)
  74. c = ff(c, d, a, b, x[i+10], 17, -42063)
  75. b = ff(b, c, d, a, x[i+11], 22, -1990404162)
  76. a = ff(a, b, c, d, x[i+12], 7 , 1804603682)
  77. d = ff(d, a, b, c, x[i+13], 12, -40341101)
  78. c = ff(c, d, a, b, x[i+14], 17, -1502002290)
  79. b = ff(b, c, d, a, x[i+15], 22, 1236535329)
  80. a = gg(a, b, c, d, x[i+ 1], 5 , -165796510)
  81. d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632)
  82. c = gg(c, d, a, b, x[i+11], 14, 643717713)
  83. b = gg(b, c, d, a, x[i+ 0], 20, -373897302)
  84. a = gg(a, b, c, d, x[i+ 5], 5 , -701558691)
  85. d = gg(d, a, b, c, x[i+10], 9 , 38016083)
  86. c = gg(c, d, a, b, x[i+15], 14, -660478335)
  87. b = gg(b, c, d, a, x[i+ 4], 20, -405537848)
  88. a = gg(a, b, c, d, x[i+ 9], 5 , 568446438)
  89. d = gg(d, a, b, c, x[i+14], 9 , -1019803690)
  90. c = gg(c, d, a, b, x[i+ 3], 14, -187363961)
  91. b = gg(b, c, d, a, x[i+ 8], 20, 1163531501)
  92. a = gg(a, b, c, d, x[i+13], 5 , -1444681467)
  93. d = gg(d, a, b, c, x[i+ 2], 9 , -51403784)
  94. c = gg(c, d, a, b, x[i+ 7], 14, 1735328473)
  95. b = gg(b, c, d, a, x[i+12], 20, -1926607734)
  96. a = hh(a, b, c, d, x[i+ 5], 4 , -378558)
  97. d = hh(d, a, b, c, x[i+ 8], 11, -2022574463)
  98. c = hh(c, d, a, b, x[i+11], 16, 1839030562)
  99. b = hh(b, c, d, a, x[i+14], 23, -35309556)
  100. a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060)
  101. d = hh(d, a, b, c, x[i+ 4], 11, 1272893353)
  102. c = hh(c, d, a, b, x[i+ 7], 16, -155497632)
  103. b = hh(b, c, d, a, x[i+10], 23, -1094730640)
  104. a = hh(a, b, c, d, x[i+13], 4 , 681279174)
  105. d = hh(d, a, b, c, x[i+ 0], 11, -358537222)
  106. c = hh(c, d, a, b, x[i+ 3], 16, -722521979)
  107. b = hh(b, c, d, a, x[i+ 6], 23, 76029189)
  108. a = hh(a, b, c, d, x[i+ 9], 4 , -640364487)
  109. d = hh(d, a, b, c, x[i+12], 11, -421815835)
  110. c = hh(c, d, a, b, x[i+15], 16, 530742520)
  111. b = hh(b, c, d, a, x[i+ 2], 23, -995338651)
  112. a = ii(a, b, c, d, x[i+ 0], 6 , -198630844)
  113. d = ii(d, a, b, c, x[i+ 7], 10, 1126891415)
  114. c = ii(c, d, a, b, x[i+14], 15, -1416354905)
  115. b = ii(b, c, d, a, x[i+ 5], 21, -57434055)
  116. a = ii(a, b, c, d, x[i+12], 6 , 1700485571)
  117. d = ii(d, a, b, c, x[i+ 3], 10, -1894986606)
  118. c = ii(c, d, a, b, x[i+10], 15, -1051523)
  119. b = ii(b, c, d, a, x[i+ 1], 21, -2054922799)
  120. a = ii(a, b, c, d, x[i+ 8], 6 , 1873313359)
  121. d = ii(d, a, b, c, x[i+15], 10, -30611744)
  122. c = ii(c, d, a, b, x[i+ 6], 15, -1560198380)
  123. b = ii(b, c, d, a, x[i+13], 21, 1309151649)
  124. a = ii(a, b, c, d, x[i+ 4], 6 , -145523070)
  125. d = ii(d, a, b, c, x[i+11], 10, -1120210379)
  126. c = ii(c, d, a, b, x[i+ 2], 15, 718787259)
  127. b = ii(b, c, d, a, x[i+ 9], 21, -343485551)
  128. a = safe_add(a, olda)
  129. b = safe_add(b, oldb)
  130. c = safe_add(c, oldc)
  131. d = safe_add(d, oldd)
  132. }
  133. return [a, b, c, d]
  134. }
  135. /*
  136. * Convert an array of little-endian words to a hex string.
  137. */
  138. function binl2hex(binarray)
  139. {
  140. var hex_tab = "0123456789abcdef"
  141. var str = ""
  142. for(var i = 0; i < binarray.length * 4; i++)
  143. {
  144. str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
  145. hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF)
  146. }
  147. return str
  148. }
  149. /*
  150. * Convert an array of little-endian words to a base64 encoded string.
  151. */
  152. function binl2b64(binarray)
  153. {
  154. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  155. var str = ""
  156. for(var i = 0; i < binarray.length * 32; i += 6)
  157. {
  158. str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) |
  159. ((binarray[i>>5+1] >> (32-i%32)) & 0x3F))
  160. }
  161. return str
  162. }
  163. /*
  164. * Convert an 8-bit character string to a sequence of 16-word blocks, stored
  165. * as an array, and append appropriate padding for MD4/5 calculation.
  166. * If any of the characters are >255, the high byte is silently ignored.
  167. */
  168. function str2binl(str)
  169. {
  170. var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks
  171. var blks = new Array(nblk * 16)
  172. for(var i = 0; i < nblk * 16; i++) blks[i] = 0
  173. for(var i = 0; i < str.length; i++)
  174. blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8)
  175. blks[i>>2] |= 0x80 << ((i%4) * 8)
  176. blks[nblk*16-2] = str.length * 8
  177. return blks
  178. }
  179. /*
  180. * Convert a wide-character string to a sequence of 16-word blocks, stored as
  181. * an array, and append appropriate padding for MD4/5 calculation.
  182. */
  183. function strw2binl(str)
  184. {
  185. var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks
  186. var blks = new Array(nblk * 16)
  187. for(var i = 0; i < nblk * 16; i++) blks[i] = 0
  188. for(var i = 0; i < str.length; i++)
  189. blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16)
  190. blks[i>>1] |= 0x80 << ((i%2) * 16)
  191. blks[nblk*16-2] = str.length * 16
  192. return blks
  193. }
  194. function sha1(e) {
  195. function t(e, t) {
  196. var n = e << t | e >>> 32 - t;
  197. return n
  198. }
  199. function n(e) {
  200. var t = "",
  201. n, r, i;
  202. for (n = 0; n <= 6; n += 2) r = e >>> n * 4 + 4 & 15, i = e >>> n * 4 & 15, t += r.toString(16) + i.toString(16);
  203. return t
  204. }
  205. function r(e) {
  206. var t = "",
  207. n, r;
  208. for (n = 7; n >= 0; n--) r = e >>> n * 4 & 15, t += r.toString(16);
  209. return t
  210. }
  211. function i(e) {
  212. e = e.replace(/\r\n/g, "\n");
  213. var t = "";
  214. for (var n = 0; n < e.length; n++) {
  215. var r = e.charCodeAt(n);
  216. r < 128 ? t += String.fromCharCode(r) : r > 127 && r < 2048 ? (t += String.fromCharCode(r >> 6 | 192), t += String.fromCharCode(r & 63 | 128)) : (t += String.fromCharCode(r >> 12 | 224), t += String.fromCharCode(r >> 6 & 63 | 128), t += String.fromCharCode(r & 63 | 128))
  217. }
  218. return t
  219. }
  220. var s, o, u, a = new Array(80),
  221. f = 1732584193,
  222. l = 4023233417,
  223. c = 2562383102,
  224. h = 271733878,
  225. p = 3285377520,
  226. d, v, m, g, y, b;
  227. e = i(e);
  228. var w = e.length,
  229. E = new Array;
  230. for (o = 0; o < w - 3; o += 4) u = e.charCodeAt(o) << 24 | e.charCodeAt(o + 1) << 16 | e.charCodeAt(o + 2) << 8 | e.charCodeAt(o + 3), E.push(u);
  231. switch (w % 4) {
  232. case 0:
  233. o = 2147483648;
  234. break;
  235. case 1:
  236. o = e.charCodeAt(w - 1) << 24 | 8388608;
  237. break;
  238. case 2:
  239. o = e.charCodeAt(w - 2) << 24 | e.charCodeAt(w - 1) << 16 | 32768;
  240. break;
  241. case 3:
  242. o = e.charCodeAt(w - 3) << 24 | e.charCodeAt(w - 2) << 16 | e.charCodeAt(w - 1) << 8 | 128
  243. }
  244. E.push(o);
  245. while (E.length % 16 != 14) E.push(0);
  246. E.push(w >>> 29), E.push(w << 3 & 4294967295);
  247. for (s = 0; s < E.length; s += 16) {
  248. for (o = 0; o < 16; o++) a[o] = E[s + o];
  249. for (o = 16; o <= 79; o++) a[o] = t(a[o - 3] ^ a[o - 8] ^ a[o - 14] ^ a[o - 16], 1);
  250. d = f, v = l, m = c, g = h, y = p;
  251. for (o = 0; o <= 19; o++) b = t(d, 5) + (v & m | ~v & g) + y + a[o] + 1518500249 & 4294967295, y = g, g = m, m = t(v, 30), v = d, d = b;
  252. for (o = 20; o <= 39; o++) b = t(d, 5) + (v ^ m ^ g) + y + a[o] + 1859775393 & 4294967295, y = g, g = m, m = t(v, 30), v = d, d = b;
  253. for (o = 40; o <= 59; o++) b = t(d, 5) + (v & m | v & g | m & g) + y + a[o] + 2400959708 & 4294967295, y = g, g = m, m = t(v, 30), v = d, d = b;
  254. for (o = 60; o <= 79; o++) b = t(d, 5) + (v ^ m ^ g) + y + a[o] + 3395469782 & 4294967295, y = g, g = m, m = t(v, 30), v = d, d = b;
  255. f = f + d & 4294967295, l = l + v & 4294967295, c = c + m & 4294967295, h = h + g & 4294967295, p = p + y & 4294967295
  256. }
  257. var b = r(f) + r(l) + r(c) + r(h) + r(p);
  258. return b.toLowerCase()
  259. }
  260. function xor(e, t) {
  261. var n = "";
  262. for (var r = 0, i = e.length, s = t.length; r < i; r++) {
  263. if (r >= s) break;
  264. n += String.fromCharCode(e.charCodeAt(r) ^ t.charCodeAt(r))
  265. }
  266. return n
  267. }
  268. function base64encode(e) {
  269. var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  270. n, r, i, s, o, u;
  271. i = e.length, r = 0, n = "";
  272. while (r < i) {
  273. s = e.charCodeAt(r++) & 255;
  274. if (r == i) {
  275. n += t.charAt(s >> 2), n += t.charAt((s & 3) << 4), n += "==";
  276. break
  277. }
  278. o = e.charCodeAt(r++);
  279. if (r == i) {
  280. n += t.charAt(s >> 2), n += t.charAt((s & 3) << 4 | (o & 240) >> 4), n += t.charAt((o & 15) << 2), n += "=";
  281. break
  282. }
  283. u = e.charCodeAt(r++), n += t.charAt(s >> 2), n += t.charAt((s & 3) << 4 | (o & 240) >> 4), n += t.charAt((o & 15) << 2 | (u & 192) >> 6), n += t.charAt(u & 63)
  284. }
  285. return n
  286. }
  287. /*
  288. * External interface
  289. */
  290. function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) }
  291. function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }
  292. function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) }
  293. function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }
  294. /* Backward compatibility */
  295. function calcMD5(str) { return binl2hex(coreMD5( str2binl(str))) }
  296. module.exports = {
  297. hexMD5,
  298. hexMD5w,
  299. b64MD5,
  300. b64MD5w,
  301. sha1,
  302. xor,
  303. base64encode
  304. }