md5.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /*
  195. * External interface
  196. */
  197. function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) }
  198. function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }
  199. function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) }
  200. function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }
  201. /* Backward compatibility */
  202. function calcMD5(str) { return binl2hex(coreMD5( str2binl(str))) }
  203. module.exports = {
  204. hexMD5,
  205. hexMD5w,
  206. b64MD5,
  207. b64MD5w
  208. }