md5.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. var lsw = (x & 0xFFFF) + (y & 0xFFFF)
  14. var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
  15. return (msw << 16) | (lsw & 0xFFFF)
  16. }
  17. /*
  18. * Bitwise rotate a 32-bit number to the left.
  19. */
  20. function rol(num, cnt) {
  21. return (num << cnt) | (num >>> (32 - cnt))
  22. }
  23. /*
  24. * These functions implement the four basic operations the algorithm uses.
  25. */
  26. function cmn(q, a, b, x, s, t) {
  27. return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)
  28. }
  29. function ff(a, b, c, d, x, s, t) {
  30. return cmn((b & c) | ((~b) & d), a, b, x, s, t)
  31. }
  32. function gg(a, b, c, d, x, s, t) {
  33. return cmn((b & d) | (c & (~d)), a, b, x, s, t)
  34. }
  35. function hh(a, b, c, d, x, s, t) {
  36. return cmn(b ^ c ^ d, a, b, x, s, t)
  37. }
  38. function ii(a, b, c, d, x, s, t) {
  39. return cmn(c ^ (b | (~d)), a, b, x, s, t)
  40. }
  41. /*
  42. * Calculate the MD5 of an array of little-endian words, producing an array
  43. * of little-endian words.
  44. */
  45. function coreMD5(x) {
  46. var a = 1732584193
  47. var b = -271733879
  48. var c = -1732584194
  49. var d = 271733878
  50. for (var i = 0; i < x.length; i += 16) {
  51. var olda = a
  52. var oldb = b
  53. var oldc = c
  54. var oldd = d
  55. a = ff(a, b, c, d, x[i + 0], 7, -680876936)
  56. d = ff(d, a, b, c, x[i + 1], 12, -389564586)
  57. c = ff(c, d, a, b, x[i + 2], 17, 606105819)
  58. b = ff(b, c, d, a, x[i + 3], 22, -1044525330)
  59. a = ff(a, b, c, d, x[i + 4], 7, -176418897)
  60. d = ff(d, a, b, c, x[i + 5], 12, 1200080426)
  61. c = ff(c, d, a, b, x[i + 6], 17, -1473231341)
  62. b = ff(b, c, d, a, x[i + 7], 22, -45705983)
  63. a = ff(a, b, c, d, x[i + 8], 7, 1770035416)
  64. d = ff(d, a, b, c, x[i + 9], 12, -1958414417)
  65. c = ff(c, d, a, b, x[i + 10], 17, -42063)
  66. b = ff(b, c, d, a, x[i + 11], 22, -1990404162)
  67. a = ff(a, b, c, d, x[i + 12], 7, 1804603682)
  68. d = ff(d, a, b, c, x[i + 13], 12, -40341101)
  69. c = ff(c, d, a, b, x[i + 14], 17, -1502002290)
  70. b = ff(b, c, d, a, x[i + 15], 22, 1236535329)
  71. a = gg(a, b, c, d, x[i + 1], 5, -165796510)
  72. d = gg(d, a, b, c, x[i + 6], 9, -1069501632)
  73. c = gg(c, d, a, b, x[i + 11], 14, 643717713)
  74. b = gg(b, c, d, a, x[i + 0], 20, -373897302)
  75. a = gg(a, b, c, d, x[i + 5], 5, -701558691)
  76. d = gg(d, a, b, c, x[i + 10], 9, 38016083)
  77. c = gg(c, d, a, b, x[i + 15], 14, -660478335)
  78. b = gg(b, c, d, a, x[i + 4], 20, -405537848)
  79. a = gg(a, b, c, d, x[i + 9], 5, 568446438)
  80. d = gg(d, a, b, c, x[i + 14], 9, -1019803690)
  81. c = gg(c, d, a, b, x[i + 3], 14, -187363961)
  82. b = gg(b, c, d, a, x[i + 8], 20, 1163531501)
  83. a = gg(a, b, c, d, x[i + 13], 5, -1444681467)
  84. d = gg(d, a, b, c, x[i + 2], 9, -51403784)
  85. c = gg(c, d, a, b, x[i + 7], 14, 1735328473)
  86. b = gg(b, c, d, a, x[i + 12], 20, -1926607734)
  87. a = hh(a, b, c, d, x[i + 5], 4, -378558)
  88. d = hh(d, a, b, c, x[i + 8], 11, -2022574463)
  89. c = hh(c, d, a, b, x[i + 11], 16, 1839030562)
  90. b = hh(b, c, d, a, x[i + 14], 23, -35309556)
  91. a = hh(a, b, c, d, x[i + 1], 4, -1530992060)
  92. d = hh(d, a, b, c, x[i + 4], 11, 1272893353)
  93. c = hh(c, d, a, b, x[i + 7], 16, -155497632)
  94. b = hh(b, c, d, a, x[i + 10], 23, -1094730640)
  95. a = hh(a, b, c, d, x[i + 13], 4, 681279174)
  96. d = hh(d, a, b, c, x[i + 0], 11, -358537222)
  97. c = hh(c, d, a, b, x[i + 3], 16, -722521979)
  98. b = hh(b, c, d, a, x[i + 6], 23, 76029189)
  99. a = hh(a, b, c, d, x[i + 9], 4, -640364487)
  100. d = hh(d, a, b, c, x[i + 12], 11, -421815835)
  101. c = hh(c, d, a, b, x[i + 15], 16, 530742520)
  102. b = hh(b, c, d, a, x[i + 2], 23, -995338651)
  103. a = ii(a, b, c, d, x[i + 0], 6, -198630844)
  104. d = ii(d, a, b, c, x[i + 7], 10, 1126891415)
  105. c = ii(c, d, a, b, x[i + 14], 15, -1416354905)
  106. b = ii(b, c, d, a, x[i + 5], 21, -57434055)
  107. a = ii(a, b, c, d, x[i + 12], 6, 1700485571)
  108. d = ii(d, a, b, c, x[i + 3], 10, -1894986606)
  109. c = ii(c, d, a, b, x[i + 10], 15, -1051523)
  110. b = ii(b, c, d, a, x[i + 1], 21, -2054922799)
  111. a = ii(a, b, c, d, x[i + 8], 6, 1873313359)
  112. d = ii(d, a, b, c, x[i + 15], 10, -30611744)
  113. c = ii(c, d, a, b, x[i + 6], 15, -1560198380)
  114. b = ii(b, c, d, a, x[i + 13], 21, 1309151649)
  115. a = ii(a, b, c, d, x[i + 4], 6, -145523070)
  116. d = ii(d, a, b, c, x[i + 11], 10, -1120210379)
  117. c = ii(c, d, a, b, x[i + 2], 15, 718787259)
  118. b = ii(b, c, d, a, x[i + 9], 21, -343485551)
  119. a = safe_add(a, olda)
  120. b = safe_add(b, oldb)
  121. c = safe_add(c, oldc)
  122. d = safe_add(d, oldd)
  123. }
  124. return [a, b, c, d]
  125. }
  126. /*
  127. * Convert an array of little-endian words to a hex string.
  128. */
  129. function binl2hex(binarray) {
  130. var hex_tab = "0123456789abcdef"
  131. var str = ""
  132. for (var i = 0; i < binarray.length * 4; i++) {
  133. str += hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8 + 4)) & 0xF) +
  134. hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8)) & 0xF)
  135. }
  136. return str
  137. }
  138. /*
  139. * Convert an array of little-endian words to a base64 encoded string.
  140. */
  141. function binl2b64(binarray) {
  142. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  143. var str = ""
  144. for (var i = 0; i < binarray.length * 32; i += 6) {
  145. str += tab.charAt(((binarray[i >> 5] << (i % 32)) & 0x3F) |
  146. ((binarray[i >> 5 + 1] >> (32 - i % 32)) & 0x3F))
  147. }
  148. return str
  149. }
  150. /*
  151. * Convert an 8-bit character string to a sequence of 16-word blocks, stored
  152. * as an array, and append appropriate padding for MD4/5 calculation.
  153. * If any of the characters are >255, the high byte is silently ignored.
  154. */
  155. function str2binl(str) {
  156. var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks
  157. var blks = new Array(nblk * 16)
  158. for (var i = 0; i < nblk * 16; i++) blks[i] = 0
  159. for (var i = 0; i < str.length; i++)
  160. blks[i >> 2] |= (str.charCodeAt(i) & 0xFF) << ((i % 4) * 8)
  161. blks[i >> 2] |= 0x80 << ((i % 4) * 8)
  162. blks[nblk * 16 - 2] = str.length * 8
  163. return blks
  164. }
  165. /*
  166. * Convert a wide-character string to a sequence of 16-word blocks, stored as
  167. * an array, and append appropriate padding for MD4/5 calculation.
  168. */
  169. function strw2binl(str) {
  170. var nblk = ((str.length + 4) >> 5) + 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 >> 1] |= str.charCodeAt(i) << ((i % 2) * 16)
  175. blks[i >> 1] |= 0x80 << ((i % 2) * 16)
  176. blks[nblk * 16 - 2] = str.length * 16
  177. return blks
  178. }
  179. /*
  180. * External interface
  181. */
  182. function hexMD5(str) { return binl2hex(coreMD5(str2binl(str))) }
  183. function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }
  184. function b64MD5(str) { return binl2b64(coreMD5(str2binl(str))) }
  185. function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }
  186. /* Backward compatibility */
  187. function calcMD5(str) { return binl2hex(coreMD5(str2binl(str))) }
  188. module.exports = {
  189. hexMD5: hexMD5
  190. }