MD5.js 5.0 KB

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