You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
7.8 KiB

3 years ago
  1. /**
  2. * Copyright (c) 2014 Meizu bigertech, All rights reserved.
  3. * http://www.bigertech.com/
  4. * @author liuxing
  5. * @date 14-12-1
  6. * @description
  7. *
  8. */
  9. /*
  10. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  11. * to work around bugs in some JS interpreters.
  12. */
  13. function safe_add(x, y) {
  14. var lsw = (x & 0xFFFF) + (y & 0xFFFF),
  15. 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 bit_rol(num, cnt) {
  22. return (num << cnt) | (num >>> (32 - cnt));
  23. }
  24. /*
  25. * These functions implement the four basic operations the algorithm uses.
  26. */
  27. function md5_cmn(q, a, b, x, s, t) {
  28. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
  29. }
  30. function md5_ff(a, b, c, d, x, s, t) {
  31. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  32. }
  33. function md5_gg(a, b, c, d, x, s, t) {
  34. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  35. }
  36. function md5_hh(a, b, c, d, x, s, t) {
  37. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  38. }
  39. function md5_ii(a, b, c, d, x, s, t) {
  40. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  41. }
  42. /*
  43. * Calculate the MD5 of an array of little-endian words, and a bit length.
  44. */
  45. function binl_md5(x, len) {
  46. /* append padding */
  47. x[len >> 5] |= 0x80 << (len % 32);
  48. x[(((len + 64) >>> 9) << 4) + 14] = len;
  49. var i, olda, oldb, oldc, oldd,
  50. a = 1732584193,
  51. b = -271733879,
  52. c = -1732584194,
  53. d = 271733878;
  54. for (i = 0; i < x.length; i += 16) {
  55. olda = a;
  56. oldb = b;
  57. oldc = c;
  58. oldd = d;
  59. a = md5_ff(a, b, c, d, x[i], 7, -680876936);
  60. d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
  61. c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
  62. b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
  63. a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
  64. d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
  65. c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
  66. b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
  67. a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
  68. d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
  69. c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
  70. b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
  71. a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
  72. d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
  73. c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
  74. b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
  75. a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
  76. d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
  77. c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
  78. b = md5_gg(b, c, d, a, x[i], 20, -373897302);
  79. a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
  80. d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
  81. c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
  82. b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
  83. a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
  84. d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
  85. c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
  86. b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
  87. a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
  88. d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
  89. c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
  90. b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
  91. a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
  92. d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
  93. c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
  94. b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
  95. a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
  96. d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
  97. c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
  98. b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
  99. a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
  100. d = md5_hh(d, a, b, c, x[i], 11, -358537222);
  101. c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
  102. b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
  103. a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
  104. d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
  105. c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
  106. b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
  107. a = md5_ii(a, b, c, d, x[i], 6, -198630844);
  108. d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
  109. c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
  110. b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
  111. a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
  112. d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
  113. c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
  114. b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
  115. a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
  116. d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
  117. c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
  118. b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
  119. a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
  120. d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
  121. c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
  122. b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
  123. a = safe_add(a, olda);
  124. b = safe_add(b, oldb);
  125. c = safe_add(c, oldc);
  126. d = safe_add(d, oldd);
  127. }
  128. return [a, b, c, d];
  129. }
  130. /*
  131. * Convert an array of little-endian words to a string
  132. */
  133. function binl2rstr(input) {
  134. var i,
  135. output = '';
  136. for (i = 0; i < input.length * 32; i += 8) {
  137. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
  138. }
  139. return output;
  140. }
  141. /*
  142. * Convert a raw string to an array of little-endian words
  143. * Characters >255 have their high-byte silently ignored.
  144. */
  145. function rstr2binl(input) {
  146. var i,
  147. output = [];
  148. output[(input.length >> 2) - 1] = undefined;
  149. for (i = 0; i < output.length; i += 1) {
  150. output[i] = 0;
  151. }
  152. for (i = 0; i < input.length * 8; i += 8) {
  153. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
  154. }
  155. return output;
  156. }
  157. /*
  158. * Calculate the MD5 of a raw string
  159. */
  160. function rstr_md5(s) {
  161. return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
  162. }
  163. /*
  164. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  165. */
  166. function rstr_hmac_md5(key, data) {
  167. var i,
  168. bkey = rstr2binl(key),
  169. ipad = [],
  170. opad = [],
  171. hash;
  172. ipad[15] = opad[15] = undefined;
  173. if (bkey.length > 16) {
  174. bkey = binl_md5(bkey, key.length * 8);
  175. }
  176. for (i = 0; i < 16; i += 1) {
  177. ipad[i] = bkey[i] ^ 0x36363636;
  178. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  179. }
  180. hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  181. return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
  182. }
  183. /*
  184. * Convert a raw string to a hex string
  185. */
  186. function rstr2hex(input) {
  187. var hex_tab = '0123456789abcdef',
  188. output = '',
  189. x,
  190. i;
  191. for (i = 0; i < input.length; i += 1) {
  192. x = input.charCodeAt(i);
  193. output += hex_tab.charAt((x >>> 4) & 0x0F) +
  194. hex_tab.charAt(x & 0x0F);
  195. }
  196. return output;
  197. }
  198. /*
  199. * Encode a string as utf-8
  200. */
  201. function str2rstr_utf8(input) {
  202. return unescape(encodeURIComponent(input));
  203. }
  204. /*
  205. * Take string arguments and return either raw or hex encoded strings
  206. */
  207. function raw_md5(s) {
  208. return rstr_md5(str2rstr_utf8(s));
  209. }
  210. function hex_md5(s) {
  211. return rstr2hex(raw_md5(s));
  212. }
  213. function raw_hmac_md5(k, d) {
  214. return rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d));
  215. }
  216. function hex_hmac_md5(k, d) {
  217. return rstr2hex(raw_hmac_md5(k, d));
  218. }
  219. function md5(string, key, raw) {
  220. if (!key) {
  221. if (!raw) {
  222. return hex_md5(string);
  223. }
  224. return raw_md5(string);
  225. }
  226. if (!raw) {
  227. return hex_hmac_md5(key, string);
  228. }
  229. return raw_hmac_md5(key, string);
  230. }
  231. module.exports = md5;