MD5.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. export function md5 (md5str) {
  2. var createMD5String = function (string) {
  3. var x = []
  4. var k, AA, BB, CC, DD, a, b, c, d
  5. var S11 = 7
  6. var S12 = 12
  7. var S13 = 17
  8. var S14 = 22
  9. var S21 = 5
  10. var S22 = 9
  11. var S23 = 14
  12. var S24 = 20
  13. var S31 = 4
  14. var S32 = 11
  15. var S33 = 16
  16. var S34 = 23
  17. var S41 = 6
  18. var S42 = 10
  19. var S43 = 15
  20. var S44 = 21
  21. string = uTF8Encode(string)
  22. x = convertToWordArray(string)
  23. a = 0x67452301
  24. b = 0xEFCDAB89
  25. c = 0x98BADCFE
  26. d = 0x10325476
  27. for (k = 0; k < x.length; k += 16) {
  28. AA = a
  29. BB = b
  30. CC = c
  31. DD = d
  32. a = FF(a, b, c, d, x[k + 0], S11, 0xD76AA478)
  33. d = FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756)
  34. c = FF(c, d, a, b, x[k + 2], S13, 0x242070DB)
  35. b = FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE)
  36. a = FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF)
  37. d = FF(d, a, b, c, x[k + 5], S12, 0x4787C62A)
  38. c = FF(c, d, a, b, x[k + 6], S13, 0xA8304613)
  39. b = FF(b, c, d, a, x[k + 7], S14, 0xFD469501)
  40. a = FF(a, b, c, d, x[k + 8], S11, 0x698098D8)
  41. d = FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF)
  42. c = FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1)
  43. b = FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE)
  44. a = FF(a, b, c, d, x[k + 12], S11, 0x6B901122)
  45. d = FF(d, a, b, c, x[k + 13], S12, 0xFD987193)
  46. c = FF(c, d, a, b, x[k + 14], S13, 0xA679438E)
  47. b = FF(b, c, d, a, x[k + 15], S14, 0x49B40821)
  48. a = GG(a, b, c, d, x[k + 1], S21, 0xF61E2562)
  49. d = GG(d, a, b, c, x[k + 6], S22, 0xC040B340)
  50. c = GG(c, d, a, b, x[k + 11], S23, 0x265E5A51)
  51. b = GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA)
  52. a = GG(a, b, c, d, x[k + 5], S21, 0xD62F105D)
  53. d = GG(d, a, b, c, x[k + 10], S22, 0x2441453)
  54. c = GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681)
  55. b = GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8)
  56. a = GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6)
  57. d = GG(d, a, b, c, x[k + 14], S22, 0xC33707D6)
  58. c = GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87)
  59. b = GG(b, c, d, a, x[k + 8], S24, 0x455A14ED)
  60. a = GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905)
  61. d = GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8)
  62. c = GG(c, d, a, b, x[k + 7], S23, 0x676F02D9)
  63. b = GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A)
  64. a = HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942)
  65. d = HH(d, a, b, c, x[k + 8], S32, 0x8771F681)
  66. c = HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122)
  67. b = HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C)
  68. a = HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44)
  69. d = HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9)
  70. c = HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60)
  71. b = HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70)
  72. a = HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6)
  73. d = HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA)
  74. c = HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085)
  75. b = HH(b, c, d, a, x[k + 6], S34, 0x4881D05)
  76. a = HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039)
  77. d = HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5)
  78. c = HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8)
  79. b = HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665)
  80. a = II(a, b, c, d, x[k + 0], S41, 0xF4292244)
  81. d = II(d, a, b, c, x[k + 7], S42, 0x432AFF97)
  82. c = II(c, d, a, b, x[k + 14], S43, 0xAB9423A7)
  83. b = II(b, c, d, a, x[k + 5], S44, 0xFC93A039)
  84. a = II(a, b, c, d, x[k + 12], S41, 0x655B59C3)
  85. d = II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92)
  86. c = II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D)
  87. b = II(b, c, d, a, x[k + 1], S44, 0x85845DD1)
  88. a = II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F)
  89. d = II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0)
  90. c = II(c, d, a, b, x[k + 6], S43, 0xA3014314)
  91. b = II(b, c, d, a, x[k + 13], S44, 0x4E0811A1)
  92. a = II(a, b, c, d, x[k + 4], S41, 0xF7537E82)
  93. d = II(d, a, b, c, x[k + 11], S42, 0xBD3AF235)
  94. c = II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB)
  95. b = II(b, c, d, a, x[k + 9], S44, 0xEB86D391)
  96. a = addUnsigned(a, AA)
  97. b = addUnsigned(b, BB)
  98. c = addUnsigned(c, CC)
  99. d = addUnsigned(d, DD)
  100. }
  101. var tempValue = wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d)
  102. return tempValue.toLowerCase()
  103. }
  104. var rotateLeft = function (lValue, iShiftBits) {
  105. return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits))
  106. }
  107. var addUnsigned = function (lX, lY) {
  108. var lX4, lY4, lX8, lY8, lResult
  109. lX8 = (lX & 0x80000000)
  110. lY8 = (lY & 0x80000000)
  111. lX4 = (lX & 0x40000000)
  112. lY4 = (lY & 0x40000000)
  113. lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF)
  114. if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8)
  115. if (lX4 | lY4) {
  116. if (lResult & 0x40000000) return (lResult ^ 0xC0000000 ^ lX8 ^ lY8)
  117. else return (lResult ^ 0x40000000 ^ lX8 ^ lY8)
  118. } else {
  119. return (lResult ^ lX8 ^ lY8)
  120. }
  121. }
  122. var F = function (x, y, z) {
  123. return (x & y) | ((~x) & z)
  124. }
  125. var G = function (x, y, z) {
  126. return (x & z) | (y & (~z))
  127. }
  128. var H = function (x, y, z) {
  129. return (x ^ y ^ z)
  130. }
  131. var I = function (x, y, z) {
  132. return (y ^ (x | (~z)))
  133. }
  134. var FF = function (a, b, c, d, x, s, ac) {
  135. a = addUnsigned(a, addUnsigned(addUnsigned(F(b, c, d), x), ac))
  136. return addUnsigned(rotateLeft(a, s), b)
  137. }
  138. var GG = function (a, b, c, d, x, s, ac) {
  139. a = addUnsigned(a, addUnsigned(addUnsigned(G(b, c, d), x), ac))
  140. return addUnsigned(rotateLeft(a, s), b)
  141. }
  142. var HH = function (a, b, c, d, x, s, ac) {
  143. a = addUnsigned(a, addUnsigned(addUnsigned(H(b, c, d), x), ac))
  144. return addUnsigned(rotateLeft(a, s), b)
  145. }
  146. var II = function (a, b, c, d, x, s, ac) {
  147. a = addUnsigned(a, addUnsigned(addUnsigned(I(b, c, d), x), ac))
  148. return addUnsigned(rotateLeft(a, s), b)
  149. }
  150. var convertToWordArray = function (string) {
  151. var lWordCount
  152. var lMessageLength = string.length
  153. var lNumberOfWordsTempOne = lMessageLength + 8
  154. var lNumberOfWordsTempTwo = (lNumberOfWordsTempOne - (lNumberOfWordsTempOne % 64)) / 64
  155. var lNumberOfWords = (lNumberOfWordsTempTwo + 1) * 16
  156. var lWordArray = Array(lNumberOfWords - 1)
  157. var lBytePosition = 0
  158. var lByteCount = 0
  159. while (lByteCount < lMessageLength) {
  160. lWordCount = (lByteCount - (lByteCount % 4)) / 4
  161. lBytePosition = (lByteCount % 4) * 8
  162. lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition))
  163. lByteCount++
  164. }
  165. lWordCount = (lByteCount - (lByteCount % 4)) / 4
  166. lBytePosition = (lByteCount % 4) * 8
  167. lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition)
  168. lWordArray[lNumberOfWords - 2] = lMessageLength << 3
  169. lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29
  170. return lWordArray
  171. }
  172. var wordToHex = function (lValue) {
  173. var WordToHexValue = ''
  174. var WordToHexValueTemp = ''
  175. var lByte, lCount
  176. for (lCount = 0; lCount <= 3; lCount++) {
  177. lByte = (lValue >>> (lCount * 8)) & 255
  178. WordToHexValueTemp = '0' + lByte.toString(16)
  179. WordToHexValue = WordToHexValue + WordToHexValueTemp.substr(WordToHexValueTemp.length - 2, 2)
  180. }
  181. return WordToHexValue
  182. }
  183. var uTF8Encode = function (string) {
  184. // eslint-disable-next-line no-control-regex
  185. string = string.toString().replace(/\x0d\x0a/g, '\x0a')
  186. var output = ''
  187. for (var n = 0; n < string.length; n++) {
  188. var c = string.charCodeAt(n)
  189. if (c < 128) {
  190. output += String.fromCharCode(c)
  191. } else if ((c > 127) && (c < 2048)) {
  192. output += String.fromCharCode((c >> 6) | 192)
  193. output += String.fromCharCode((c & 63) | 128)
  194. } else {
  195. output += String.fromCharCode((c >> 12) | 224)
  196. output += String.fromCharCode(((c >> 6) & 63) | 128)
  197. output += String.fromCharCode((c & 63) | 128)
  198. }
  199. }
  200. return output
  201. }
  202. return createMD5String(md5str)
  203. }
  204. export function signVal(obj) {
  205. const array = []
  206. for (var key in obj) {
  207. array.push(key + '=' + obj[key])
  208. }
  209. array.push('secret=lzyd-sign-7c423d19cb2b')
  210. array.sort()
  211. var sign = md5(array.join('&'))
  212. obj.sign = sign
  213. return obj
  214. }