MD5.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /** @format */
  2. export function md5(md5str: string) {
  3. var createMD5String = function (string: string) {
  4. var x = Array()
  5. var k, AA, BB, CC, DD, a, b, c, d
  6. var S11 = 7,
  7. S12 = 12,
  8. S13 = 17,
  9. S14 = 22
  10. var S21 = 5,
  11. S22 = 9,
  12. S23 = 14,
  13. S24 = 20
  14. var S31 = 4,
  15. S32 = 11,
  16. S33 = 16,
  17. S34 = 23
  18. var S41 = 6,
  19. S42 = 10,
  20. S43 = 15,
  21. S44 = 21
  22. string = uTF8Encode(string)
  23. x = convertToWordArray(string)
  24. a = 0x67452301
  25. b = 0xefcdab89
  26. c = 0x98badcfe
  27. d = 0x10325476
  28. for (k = 0; k < x.length; k += 16) {
  29. AA = a
  30. BB = b
  31. CC = c
  32. DD = d
  33. a = FF(a, b, c, d, x[k + 0], S11, 0xd76aa478)
  34. d = FF(d, a, b, c, x[k + 1], S12, 0xe8c7b756)
  35. c = FF(c, d, a, b, x[k + 2], S13, 0x242070db)
  36. b = FF(b, c, d, a, x[k + 3], S14, 0xc1bdceee)
  37. a = FF(a, b, c, d, x[k + 4], S11, 0xf57c0faf)
  38. d = FF(d, a, b, c, x[k + 5], S12, 0x4787c62a)
  39. c = FF(c, d, a, b, x[k + 6], S13, 0xa8304613)
  40. b = FF(b, c, d, a, x[k + 7], S14, 0xfd469501)
  41. a = FF(a, b, c, d, x[k + 8], S11, 0x698098d8)
  42. d = FF(d, a, b, c, x[k + 9], S12, 0x8b44f7af)
  43. c = FF(c, d, a, b, x[k + 10], S13, 0xffff5bb1)
  44. b = FF(b, c, d, a, x[k + 11], S14, 0x895cd7be)
  45. a = FF(a, b, c, d, x[k + 12], S11, 0x6b901122)
  46. d = FF(d, a, b, c, x[k + 13], S12, 0xfd987193)
  47. c = FF(c, d, a, b, x[k + 14], S13, 0xa679438e)
  48. b = FF(b, c, d, a, x[k + 15], S14, 0x49b40821)
  49. a = GG(a, b, c, d, x[k + 1], S21, 0xf61e2562)
  50. d = GG(d, a, b, c, x[k + 6], S22, 0xc040b340)
  51. c = GG(c, d, a, b, x[k + 11], S23, 0x265e5a51)
  52. b = GG(b, c, d, a, x[k + 0], S24, 0xe9b6c7aa)
  53. a = GG(a, b, c, d, x[k + 5], S21, 0xd62f105d)
  54. d = GG(d, a, b, c, x[k + 10], S22, 0x2441453)
  55. c = GG(c, d, a, b, x[k + 15], S23, 0xd8a1e681)
  56. b = GG(b, c, d, a, x[k + 4], S24, 0xe7d3fbc8)
  57. a = GG(a, b, c, d, x[k + 9], S21, 0x21e1cde6)
  58. d = GG(d, a, b, c, x[k + 14], S22, 0xc33707d6)
  59. c = GG(c, d, a, b, x[k + 3], S23, 0xf4d50d87)
  60. b = GG(b, c, d, a, x[k + 8], S24, 0x455a14ed)
  61. a = GG(a, b, c, d, x[k + 13], S21, 0xa9e3e905)
  62. d = GG(d, a, b, c, x[k + 2], S22, 0xfcefa3f8)
  63. c = GG(c, d, a, b, x[k + 7], S23, 0x676f02d9)
  64. b = GG(b, c, d, a, x[k + 12], S24, 0x8d2a4c8a)
  65. a = HH(a, b, c, d, x[k + 5], S31, 0xfffa3942)
  66. d = HH(d, a, b, c, x[k + 8], S32, 0x8771f681)
  67. c = HH(c, d, a, b, x[k + 11], S33, 0x6d9d6122)
  68. b = HH(b, c, d, a, x[k + 14], S34, 0xfde5380c)
  69. a = HH(a, b, c, d, x[k + 1], S31, 0xa4beea44)
  70. d = HH(d, a, b, c, x[k + 4], S32, 0x4bdecfa9)
  71. c = HH(c, d, a, b, x[k + 7], S33, 0xf6bb4b60)
  72. b = HH(b, c, d, a, x[k + 10], S34, 0xbebfbc70)
  73. a = HH(a, b, c, d, x[k + 13], S31, 0x289b7ec6)
  74. d = HH(d, a, b, c, x[k + 0], S32, 0xeaa127fa)
  75. c = HH(c, d, a, b, x[k + 3], S33, 0xd4ef3085)
  76. b = HH(b, c, d, a, x[k + 6], S34, 0x4881d05)
  77. a = HH(a, b, c, d, x[k + 9], S31, 0xd9d4d039)
  78. d = HH(d, a, b, c, x[k + 12], S32, 0xe6db99e5)
  79. c = HH(c, d, a, b, x[k + 15], S33, 0x1fa27cf8)
  80. b = HH(b, c, d, a, x[k + 2], S34, 0xc4ac5665)
  81. a = II(a, b, c, d, x[k + 0], S41, 0xf4292244)
  82. d = II(d, a, b, c, x[k + 7], S42, 0x432aff97)
  83. c = II(c, d, a, b, x[k + 14], S43, 0xab9423a7)
  84. b = II(b, c, d, a, x[k + 5], S44, 0xfc93a039)
  85. a = II(a, b, c, d, x[k + 12], S41, 0x655b59c3)
  86. d = II(d, a, b, c, x[k + 3], S42, 0x8f0ccc92)
  87. c = II(c, d, a, b, x[k + 10], S43, 0xffeff47d)
  88. b = II(b, c, d, a, x[k + 1], S44, 0x85845dd1)
  89. a = II(a, b, c, d, x[k + 8], S41, 0x6fa87e4f)
  90. d = II(d, a, b, c, x[k + 15], S42, 0xfe2ce6e0)
  91. c = II(c, d, a, b, x[k + 6], S43, 0xa3014314)
  92. b = II(b, c, d, a, x[k + 13], S44, 0x4e0811a1)
  93. a = II(a, b, c, d, x[k + 4], S41, 0xf7537e82)
  94. d = II(d, a, b, c, x[k + 11], S42, 0xbd3af235)
  95. c = II(c, d, a, b, x[k + 2], S43, 0x2ad7d2bb)
  96. b = II(b, c, d, a, x[k + 9], S44, 0xeb86d391)
  97. a = addUnsigned(a, AA)
  98. b = addUnsigned(b, BB)
  99. c = addUnsigned(c, CC)
  100. d = addUnsigned(d, DD)
  101. }
  102. var tempValue = wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d)
  103. return tempValue.toLowerCase()
  104. }
  105. var rotateLeft = function (lValue: number, iShiftBits: number) {
  106. return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits))
  107. }
  108. var addUnsigned = function (lX: number, lY: number) {
  109. var lX4, lY4, lX8, lY8, lResult
  110. lX8 = lX & 0x80000000
  111. lY8 = lY & 0x80000000
  112. lX4 = lX & 0x40000000
  113. lY4 = lY & 0x40000000
  114. lResult = (lX & 0x3fffffff) + (lY & 0x3fffffff)
  115. if (lX4 & lY4) return lResult ^ 0x80000000 ^ lX8 ^ lY8
  116. if (lX4 | lY4) {
  117. if (lResult & 0x40000000) return lResult ^ 0xc0000000 ^ lX8 ^ lY8
  118. else return lResult ^ 0x40000000 ^ lX8 ^ lY8
  119. } else {
  120. return lResult ^ lX8 ^ lY8
  121. }
  122. }
  123. var F = function (x: number, y: number, z: number) {
  124. return (x & y) | (~x & z)
  125. }
  126. var G = function (x: number, y: number, z: number) {
  127. return (x & z) | (y & ~z)
  128. }
  129. var H = function (x: number, y: number, z: number) {
  130. return x ^ y ^ z
  131. }
  132. var I = function (x: number, y: number, z: number) {
  133. return y ^ (x | ~z)
  134. }
  135. var FF = function (a: number, b: number, c: number, d: number, x: number, s: number, ac: number) {
  136. a = addUnsigned(a, addUnsigned(addUnsigned(F(b, c, d), x), ac))
  137. return addUnsigned(rotateLeft(a, s), b)
  138. }
  139. var GG = function (a: number, b: number, c: number, d: number, x: number, s: number, ac: number) {
  140. a = addUnsigned(a, addUnsigned(addUnsigned(G(b, c, d), x), ac))
  141. return addUnsigned(rotateLeft(a, s), b)
  142. }
  143. var HH = function (a: number, b: number, c: number, d: number, x: number, s: number, ac: number) {
  144. a = addUnsigned(a, addUnsigned(addUnsigned(H(b, c, d), x), ac))
  145. return addUnsigned(rotateLeft(a, s), b)
  146. }
  147. var II = function (a: number, b: number, c: number, d: number, x: number, s: number, ac: number) {
  148. a = addUnsigned(a, addUnsigned(addUnsigned(I(b, c, d), x), ac))
  149. return addUnsigned(rotateLeft(a, s), b)
  150. }
  151. var convertToWordArray = function (string: string) {
  152. var lWordCount
  153. var lMessageLength = string.length
  154. var lNumberOfWordsTempOne = lMessageLength + 8
  155. var lNumberOfWordsTempTwo = (lNumberOfWordsTempOne - (lNumberOfWordsTempOne % 64)) / 64
  156. var lNumberOfWords = (lNumberOfWordsTempTwo + 1) * 16
  157. var lWordArray = Array(lNumberOfWords - 1)
  158. var lBytePosition = 0
  159. var lByteCount = 0
  160. while (lByteCount < lMessageLength) {
  161. lWordCount = (lByteCount - (lByteCount % 4)) / 4
  162. lBytePosition = (lByteCount % 4) * 8
  163. lWordArray[lWordCount] = lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition)
  164. lByteCount++
  165. }
  166. lWordCount = (lByteCount - (lByteCount % 4)) / 4
  167. lBytePosition = (lByteCount % 4) * 8
  168. lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition)
  169. lWordArray[lNumberOfWords - 2] = lMessageLength << 3
  170. lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29
  171. return lWordArray
  172. }
  173. var wordToHex = function (lValue: number) {
  174. var WordToHexValue = '',
  175. WordToHexValueTemp = '',
  176. lByte,
  177. lCount
  178. for (lCount = 0; lCount <= 3; lCount++) {
  179. lByte = (lValue >>> (lCount * 8)) & 255
  180. WordToHexValueTemp = '0' + lByte.toString(16)
  181. WordToHexValue = WordToHexValue + WordToHexValueTemp.substr(WordToHexValueTemp.length - 2, 2)
  182. }
  183. return WordToHexValue
  184. }
  185. var uTF8Encode = function (string: string) {
  186. string = string.toString().replace(/\x0d\x0a/g, '\x0a')
  187. var output = ''
  188. for (var n = 0; n < string.length; n++) {
  189. var c = string.charCodeAt(n)
  190. if (c < 128) {
  191. output += String.fromCharCode(c)
  192. } else if (c > 127 && c < 2048) {
  193. output += String.fromCharCode((c >> 6) | 192)
  194. output += String.fromCharCode((c & 63) | 128)
  195. } else {
  196. output += String.fromCharCode((c >> 12) | 224)
  197. output += String.fromCharCode(((c >> 6) & 63) | 128)
  198. output += String.fromCharCode((c & 63) | 128)
  199. }
  200. }
  201. return output
  202. }
  203. return createMD5String(md5str)
  204. }