md5.js 5.1 KB

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