md5.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*!
  2. * md5-js v0.0.2
  3. * (c) 2016 hapjs
  4. * Released under the MIT License.
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. (global.md5 = factory());
  10. }(this, function () { 'use strict';
  11. /*
  12. http://www.npmjs.org/packages/crypt
  13. */
  14. var base64map
  15. = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  16. var crypt = {
  17. // Bit-wise rotation left
  18. rotl: function(n, b) {
  19. return (n << b) | (n >>> (32 - b));
  20. },
  21. // Bit-wise rotation right
  22. rotr: function(n, b) {
  23. return (n << (32 - b)) | (n >>> b);
  24. },
  25. // Swap big-endian to little-endian and vice versa
  26. endian: function(n) {
  27. // If number given, swap endian
  28. if (n.constructor == Number) {
  29. return crypt.rotl(n, 8) & 0x00FF00FF | crypt.rotl(n, 24) & 0xFF00FF00;
  30. }
  31. // Else, assume array and swap all items
  32. for (var i = 0; i < n.length; i++)
  33. n[i] = crypt.endian(n[i]);
  34. return n;
  35. },
  36. // Generate an array of any length of random bytes
  37. randomBytes: function(n) {
  38. for (var bytes = []; n > 0; n--)
  39. bytes.push(Math.floor(Math.random() * 256));
  40. return bytes;
  41. },
  42. // Convert a byte array to big-endian 32-bit words
  43. bytesToWords: function(bytes) {
  44. for (var words = [], i = 0, b = 0; i < bytes.length; i++, b += 8)
  45. words[b >>> 5] |= bytes[i] << (24 - b % 32);
  46. return words;
  47. },
  48. // Convert big-endian 32-bit words to a byte array
  49. wordsToBytes: function(words) {
  50. for (var bytes = [], b = 0; b < words.length * 32; b += 8)
  51. bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
  52. return bytes;
  53. },
  54. // Convert a byte array to a hex string
  55. bytesToHex: function(bytes) {
  56. for (var hex = [], i = 0; i < bytes.length; i++) {
  57. hex.push((bytes[i] >>> 4).toString(16));
  58. hex.push((bytes[i] & 0xF).toString(16));
  59. }
  60. return hex.join('');
  61. },
  62. // Convert a hex string to a byte array
  63. hexToBytes: function(hex) {
  64. for (var bytes = [], c = 0; c < hex.length; c += 2)
  65. bytes.push(parseInt(hex.substr(c, 2), 16));
  66. return bytes;
  67. },
  68. // Convert a byte array to a base-64 string
  69. bytesToBase64: function(bytes) {
  70. for (var base64 = [], i = 0; i < bytes.length; i += 3) {
  71. var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
  72. for (var j = 0; j < 4; j++)
  73. if (i * 8 + j * 6 <= bytes.length * 8)
  74. base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F));
  75. else
  76. base64.push('=');
  77. }
  78. return base64.join('');
  79. },
  80. // Convert a base-64 string to a byte array
  81. base64ToBytes: function(base64) {
  82. // Remove non-base-64 characters
  83. base64 = base64.replace(/[^A-Z0-9+\/]/ig, '');
  84. for (var bytes = [], i = 0, imod4 = 0; i < base64.length;
  85. imod4 = ++i % 4) {
  86. if (imod4 == 0) continue;
  87. bytes.push(((base64map.indexOf(base64.charAt(i - 1))
  88. & (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2))
  89. | (base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2)));
  90. }
  91. return bytes;
  92. }
  93. };
  94. /*
  95. http://www.npmjs.org/packages/charenc
  96. */
  97. // UTF-8 encoding
  98. var utf8 = {
  99. // Convert a string to a byte array
  100. stringToBytes: function(str) {
  101. return bin.stringToBytes(unescape(encodeURIComponent(str)));
  102. },
  103. // Convert a byte array to a string
  104. bytesToString: function(bytes) {
  105. return decodeURIComponent(escape(bin.bytesToString(bytes)));
  106. }
  107. }
  108. // Binary encoding
  109. var bin = {
  110. // Convert a string to a byte array
  111. stringToBytes: function(str) {
  112. for (var bytes = [], i = 0; i < str.length; i++)
  113. bytes.push(str.charCodeAt(i) & 0xFF);
  114. return bytes;
  115. },
  116. // Convert a byte array to a string
  117. bytesToString: function(bytes) {
  118. for (var str = [], i = 0; i < bytes.length; i++)
  119. str.push(String.fromCharCode(bytes[i]));
  120. return str.join('');
  121. }
  122. }
  123. var md5;
  124. /*
  125. http://www.npmjs.org/packages/md5
  126. */
  127. md5 = function (message, options) {
  128. // Convert to byte array
  129. if (message.constructor == String)
  130. if (options && options.encoding === 'binary')
  131. message = bin.stringToBytes(message);
  132. else
  133. message = utf8.stringToBytes(message);
  134. else if (message.constructor === Buffer)
  135. message = Array.prototype.slice.call(message, 0);
  136. else if (!Array.isArray(message))
  137. message = message.toString();
  138. // else, assume byte array already
  139. var m = crypt.bytesToWords(message),
  140. l = message.length * 8,
  141. a = 1732584193,
  142. b = -271733879,
  143. c = -1732584194,
  144. d = 271733878;
  145. // Swap endian
  146. for (var i = 0; i < m.length; i++) {
  147. m[i] = ((m[i] << 8) | (m[i] >>> 24)) & 0x00FF00FF |
  148. ((m[i] << 24) | (m[i] >>> 8)) & 0xFF00FF00;
  149. }
  150. // Padding
  151. m[l >>> 5] |= 0x80 << (l % 32);
  152. m[(((l + 64) >>> 9) << 4) + 14] = l;
  153. // Method shortcuts
  154. var FF = md5._ff,
  155. GG = md5._gg,
  156. HH = md5._hh,
  157. II = md5._ii;
  158. for (var i = 0; i < m.length; i += 16) {
  159. var aa = a,
  160. bb = b,
  161. cc = c,
  162. dd = d;
  163. a = FF(a, b, c, d, m[i+ 0], 7, -680876936);
  164. d = FF(d, a, b, c, m[i+ 1], 12, -389564586);
  165. c = FF(c, d, a, b, m[i+ 2], 17, 606105819);
  166. b = FF(b, c, d, a, m[i+ 3], 22, -1044525330);
  167. a = FF(a, b, c, d, m[i+ 4], 7, -176418897);
  168. d = FF(d, a, b, c, m[i+ 5], 12, 1200080426);
  169. c = FF(c, d, a, b, m[i+ 6], 17, -1473231341);
  170. b = FF(b, c, d, a, m[i+ 7], 22, -45705983);
  171. a = FF(a, b, c, d, m[i+ 8], 7, 1770035416);
  172. d = FF(d, a, b, c, m[i+ 9], 12, -1958414417);
  173. c = FF(c, d, a, b, m[i+10], 17, -42063);
  174. b = FF(b, c, d, a, m[i+11], 22, -1990404162);
  175. a = FF(a, b, c, d, m[i+12], 7, 1804603682);
  176. d = FF(d, a, b, c, m[i+13], 12, -40341101);
  177. c = FF(c, d, a, b, m[i+14], 17, -1502002290);
  178. b = FF(b, c, d, a, m[i+15], 22, 1236535329);
  179. a = GG(a, b, c, d, m[i+ 1], 5, -165796510);
  180. d = GG(d, a, b, c, m[i+ 6], 9, -1069501632);
  181. c = GG(c, d, a, b, m[i+11], 14, 643717713);
  182. b = GG(b, c, d, a, m[i+ 0], 20, -373897302);
  183. a = GG(a, b, c, d, m[i+ 5], 5, -701558691);
  184. d = GG(d, a, b, c, m[i+10], 9, 38016083);
  185. c = GG(c, d, a, b, m[i+15], 14, -660478335);
  186. b = GG(b, c, d, a, m[i+ 4], 20, -405537848);
  187. a = GG(a, b, c, d, m[i+ 9], 5, 568446438);
  188. d = GG(d, a, b, c, m[i+14], 9, -1019803690);
  189. c = GG(c, d, a, b, m[i+ 3], 14, -187363961);
  190. b = GG(b, c, d, a, m[i+ 8], 20, 1163531501);
  191. a = GG(a, b, c, d, m[i+13], 5, -1444681467);
  192. d = GG(d, a, b, c, m[i+ 2], 9, -51403784);
  193. c = GG(c, d, a, b, m[i+ 7], 14, 1735328473);
  194. b = GG(b, c, d, a, m[i+12], 20, -1926607734);
  195. a = HH(a, b, c, d, m[i+ 5], 4, -378558);
  196. d = HH(d, a, b, c, m[i+ 8], 11, -2022574463);
  197. c = HH(c, d, a, b, m[i+11], 16, 1839030562);
  198. b = HH(b, c, d, a, m[i+14], 23, -35309556);
  199. a = HH(a, b, c, d, m[i+ 1], 4, -1530992060);
  200. d = HH(d, a, b, c, m[i+ 4], 11, 1272893353);
  201. c = HH(c, d, a, b, m[i+ 7], 16, -155497632);
  202. b = HH(b, c, d, a, m[i+10], 23, -1094730640);
  203. a = HH(a, b, c, d, m[i+13], 4, 681279174);
  204. d = HH(d, a, b, c, m[i+ 0], 11, -358537222);
  205. c = HH(c, d, a, b, m[i+ 3], 16, -722521979);
  206. b = HH(b, c, d, a, m[i+ 6], 23, 76029189);
  207. a = HH(a, b, c, d, m[i+ 9], 4, -640364487);
  208. d = HH(d, a, b, c, m[i+12], 11, -421815835);
  209. c = HH(c, d, a, b, m[i+15], 16, 530742520);
  210. b = HH(b, c, d, a, m[i+ 2], 23, -995338651);
  211. a = II(a, b, c, d, m[i+ 0], 6, -198630844);
  212. d = II(d, a, b, c, m[i+ 7], 10, 1126891415);
  213. c = II(c, d, a, b, m[i+14], 15, -1416354905);
  214. b = II(b, c, d, a, m[i+ 5], 21, -57434055);
  215. a = II(a, b, c, d, m[i+12], 6, 1700485571);
  216. d = II(d, a, b, c, m[i+ 3], 10, -1894986606);
  217. c = II(c, d, a, b, m[i+10], 15, -1051523);
  218. b = II(b, c, d, a, m[i+ 1], 21, -2054922799);
  219. a = II(a, b, c, d, m[i+ 8], 6, 1873313359);
  220. d = II(d, a, b, c, m[i+15], 10, -30611744);
  221. c = II(c, d, a, b, m[i+ 6], 15, -1560198380);
  222. b = II(b, c, d, a, m[i+13], 21, 1309151649);
  223. a = II(a, b, c, d, m[i+ 4], 6, -145523070);
  224. d = II(d, a, b, c, m[i+11], 10, -1120210379);
  225. c = II(c, d, a, b, m[i+ 2], 15, 718787259);
  226. b = II(b, c, d, a, m[i+ 9], 21, -343485551);
  227. a = (a + aa) >>> 0;
  228. b = (b + bb) >>> 0;
  229. c = (c + cc) >>> 0;
  230. d = (d + dd) >>> 0;
  231. }
  232. return crypt.endian([a, b, c, d]);
  233. };
  234. // Auxiliary functions
  235. md5._ff = function (a, b, c, d, x, s, t) {
  236. var n = a + (b & c | ~b & d) + (x >>> 0) + t;
  237. return ((n << s) | (n >>> (32 - s))) + b;
  238. };
  239. md5._gg = function (a, b, c, d, x, s, t) {
  240. var n = a + (b & d | c & ~d) + (x >>> 0) + t;
  241. return ((n << s) | (n >>> (32 - s))) + b;
  242. };
  243. md5._hh = function (a, b, c, d, x, s, t) {
  244. var n = a + (b ^ c ^ d) + (x >>> 0) + t;
  245. return ((n << s) | (n >>> (32 - s))) + b;
  246. };
  247. md5._ii = function (a, b, c, d, x, s, t) {
  248. var n = a + (c ^ (b | ~d)) + (x >>> 0) + t;
  249. return ((n << s) | (n >>> (32 - s))) + b;
  250. };
  251. // Package private blocksize
  252. md5._blocksize = 16;
  253. md5._digestsize = 16;
  254. function md5$1 (message, options) {
  255. if(typeof message == 'undefined')
  256. return;
  257. var digestbytes = crypt.wordsToBytes(md5(message, options));
  258. return options && options.asBytes ? digestbytes :
  259. options && options.asString ? bin.bytesToString(digestbytes) :
  260. crypt.bytesToHex(digestbytes);
  261. };
  262. return md5$1;
  263. }));