md5.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. (function(){
  2. var crypt = require('crypt'),
  3. utf8 = require('charenc').utf8,
  4. isBuffer = require('is-buffer'),
  5. bin = require('charenc').bin,
  6. // The core
  7. md5 = function (message, options) {
  8. // Convert to byte array
  9. if (message.constructor == String)
  10. if (options && options.encoding === 'binary')
  11. message = bin.stringToBytes(message);
  12. else
  13. message = utf8.stringToBytes(message);
  14. else if (isBuffer(message))
  15. message = Array.prototype.slice.call(message, 0);
  16. else if (!Array.isArray(message) && message.constructor !== Uint8Array)
  17. message = message.toString();
  18. // else, assume byte array already
  19. var m = crypt.bytesToWords(message),
  20. l = message.length * 8,
  21. a = 1732584193,
  22. b = -271733879,
  23. c = -1732584194,
  24. d = 271733878;
  25. // Swap endian
  26. for (var i = 0; i < m.length; i++) {
  27. m[i] = ((m[i] << 8) | (m[i] >>> 24)) & 0x00FF00FF |
  28. ((m[i] << 24) | (m[i] >>> 8)) & 0xFF00FF00;
  29. }
  30. // Padding
  31. m[l >>> 5] |= 0x80 << (l % 32);
  32. m[(((l + 64) >>> 9) << 4) + 14] = l;
  33. // Method shortcuts
  34. var FF = md5._ff,
  35. GG = md5._gg,
  36. HH = md5._hh,
  37. II = md5._ii;
  38. for (var i = 0; i < m.length; i += 16) {
  39. var aa = a,
  40. bb = b,
  41. cc = c,
  42. dd = d;
  43. a = FF(a, b, c, d, m[i+ 0], 7, -680876936);
  44. d = FF(d, a, b, c, m[i+ 1], 12, -389564586);
  45. c = FF(c, d, a, b, m[i+ 2], 17, 606105819);
  46. b = FF(b, c, d, a, m[i+ 3], 22, -1044525330);
  47. a = FF(a, b, c, d, m[i+ 4], 7, -176418897);
  48. d = FF(d, a, b, c, m[i+ 5], 12, 1200080426);
  49. c = FF(c, d, a, b, m[i+ 6], 17, -1473231341);
  50. b = FF(b, c, d, a, m[i+ 7], 22, -45705983);
  51. a = FF(a, b, c, d, m[i+ 8], 7, 1770035416);
  52. d = FF(d, a, b, c, m[i+ 9], 12, -1958414417);
  53. c = FF(c, d, a, b, m[i+10], 17, -42063);
  54. b = FF(b, c, d, a, m[i+11], 22, -1990404162);
  55. a = FF(a, b, c, d, m[i+12], 7, 1804603682);
  56. d = FF(d, a, b, c, m[i+13], 12, -40341101);
  57. c = FF(c, d, a, b, m[i+14], 17, -1502002290);
  58. b = FF(b, c, d, a, m[i+15], 22, 1236535329);
  59. a = GG(a, b, c, d, m[i+ 1], 5, -165796510);
  60. d = GG(d, a, b, c, m[i+ 6], 9, -1069501632);
  61. c = GG(c, d, a, b, m[i+11], 14, 643717713);
  62. b = GG(b, c, d, a, m[i+ 0], 20, -373897302);
  63. a = GG(a, b, c, d, m[i+ 5], 5, -701558691);
  64. d = GG(d, a, b, c, m[i+10], 9, 38016083);
  65. c = GG(c, d, a, b, m[i+15], 14, -660478335);
  66. b = GG(b, c, d, a, m[i+ 4], 20, -405537848);
  67. a = GG(a, b, c, d, m[i+ 9], 5, 568446438);
  68. d = GG(d, a, b, c, m[i+14], 9, -1019803690);
  69. c = GG(c, d, a, b, m[i+ 3], 14, -187363961);
  70. b = GG(b, c, d, a, m[i+ 8], 20, 1163531501);
  71. a = GG(a, b, c, d, m[i+13], 5, -1444681467);
  72. d = GG(d, a, b, c, m[i+ 2], 9, -51403784);
  73. c = GG(c, d, a, b, m[i+ 7], 14, 1735328473);
  74. b = GG(b, c, d, a, m[i+12], 20, -1926607734);
  75. a = HH(a, b, c, d, m[i+ 5], 4, -378558);
  76. d = HH(d, a, b, c, m[i+ 8], 11, -2022574463);
  77. c = HH(c, d, a, b, m[i+11], 16, 1839030562);
  78. b = HH(b, c, d, a, m[i+14], 23, -35309556);
  79. a = HH(a, b, c, d, m[i+ 1], 4, -1530992060);
  80. d = HH(d, a, b, c, m[i+ 4], 11, 1272893353);
  81. c = HH(c, d, a, b, m[i+ 7], 16, -155497632);
  82. b = HH(b, c, d, a, m[i+10], 23, -1094730640);
  83. a = HH(a, b, c, d, m[i+13], 4, 681279174);
  84. d = HH(d, a, b, c, m[i+ 0], 11, -358537222);
  85. c = HH(c, d, a, b, m[i+ 3], 16, -722521979);
  86. b = HH(b, c, d, a, m[i+ 6], 23, 76029189);
  87. a = HH(a, b, c, d, m[i+ 9], 4, -640364487);
  88. d = HH(d, a, b, c, m[i+12], 11, -421815835);
  89. c = HH(c, d, a, b, m[i+15], 16, 530742520);
  90. b = HH(b, c, d, a, m[i+ 2], 23, -995338651);
  91. a = II(a, b, c, d, m[i+ 0], 6, -198630844);
  92. d = II(d, a, b, c, m[i+ 7], 10, 1126891415);
  93. c = II(c, d, a, b, m[i+14], 15, -1416354905);
  94. b = II(b, c, d, a, m[i+ 5], 21, -57434055);
  95. a = II(a, b, c, d, m[i+12], 6, 1700485571);
  96. d = II(d, a, b, c, m[i+ 3], 10, -1894986606);
  97. c = II(c, d, a, b, m[i+10], 15, -1051523);
  98. b = II(b, c, d, a, m[i+ 1], 21, -2054922799);
  99. a = II(a, b, c, d, m[i+ 8], 6, 1873313359);
  100. d = II(d, a, b, c, m[i+15], 10, -30611744);
  101. c = II(c, d, a, b, m[i+ 6], 15, -1560198380);
  102. b = II(b, c, d, a, m[i+13], 21, 1309151649);
  103. a = II(a, b, c, d, m[i+ 4], 6, -145523070);
  104. d = II(d, a, b, c, m[i+11], 10, -1120210379);
  105. c = II(c, d, a, b, m[i+ 2], 15, 718787259);
  106. b = II(b, c, d, a, m[i+ 9], 21, -343485551);
  107. a = (a + aa) >>> 0;
  108. b = (b + bb) >>> 0;
  109. c = (c + cc) >>> 0;
  110. d = (d + dd) >>> 0;
  111. }
  112. return crypt.endian([a, b, c, d]);
  113. };
  114. // Auxiliary functions
  115. md5._ff = function (a, b, c, d, x, s, t) {
  116. var n = a + (b & c | ~b & d) + (x >>> 0) + t;
  117. return ((n << s) | (n >>> (32 - s))) + b;
  118. };
  119. md5._gg = function (a, b, c, d, x, s, t) {
  120. var n = a + (b & d | c & ~d) + (x >>> 0) + t;
  121. return ((n << s) | (n >>> (32 - s))) + b;
  122. };
  123. md5._hh = function (a, b, c, d, x, s, t) {
  124. var n = a + (b ^ c ^ d) + (x >>> 0) + t;
  125. return ((n << s) | (n >>> (32 - s))) + b;
  126. };
  127. md5._ii = function (a, b, c, d, x, s, t) {
  128. var n = a + (c ^ (b | ~d)) + (x >>> 0) + t;
  129. return ((n << s) | (n >>> (32 - s))) + b;
  130. };
  131. // Package private blocksize
  132. md5._blocksize = 16;
  133. md5._digestsize = 16;
  134. module.exports = function (message, options) {
  135. if (message === undefined || message === null)
  136. throw new Error('Illegal argument ' + message);
  137. var digestbytes = crypt.wordsToBytes(md5(message, options));
  138. return options && options.asBytes ? digestbytes :
  139. options && options.asString ? bin.bytesToString(digestbytes) :
  140. crypt.bytesToHex(digestbytes);
  141. };
  142. })();