md5.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*!
  2. * Crypto-JS v1.1.0
  3. * http://code.google.com/p/crypto-js/
  4. * Copyright (c) 2009, Jeff Mott. All rights reserved.
  5. * http://code.google.com/p/crypto-js/wiki/License
  6. */
  7. (function(){
  8. // Shortcut
  9. var util = Crypto.util;
  10. // Public API
  11. var MD5 = Crypto.MD5 = function (message, options) {
  12. var digestbytes = util.wordsToBytes(MD5._md5(message));
  13. return options && options.asBytes ? digestbytes :
  14. options && options.asString ? util.bytesToString(digestbytes) :
  15. util.bytesToHex(digestbytes);
  16. };
  17. // The core
  18. MD5._md5 = function (message) {
  19. var m = util.stringToWords(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. for (var i = 0; i < m.length; i += 16) {
  34. var aa = a,
  35. bb = b,
  36. cc = c,
  37. dd = d;
  38. a = MD5._ff(a, b, c, d, m[i+ 0], 7, -680876936);
  39. d = MD5._ff(d, a, b, c, m[i+ 1], 12, -389564586);
  40. c = MD5._ff(c, d, a, b, m[i+ 2], 17, 606105819);
  41. b = MD5._ff(b, c, d, a, m[i+ 3], 22, -1044525330);
  42. a = MD5._ff(a, b, c, d, m[i+ 4], 7, -176418897);
  43. d = MD5._ff(d, a, b, c, m[i+ 5], 12, 1200080426);
  44. c = MD5._ff(c, d, a, b, m[i+ 6], 17, -1473231341);
  45. b = MD5._ff(b, c, d, a, m[i+ 7], 22, -45705983);
  46. a = MD5._ff(a, b, c, d, m[i+ 8], 7, 1770035416);
  47. d = MD5._ff(d, a, b, c, m[i+ 9], 12, -1958414417);
  48. c = MD5._ff(c, d, a, b, m[i+10], 17, -42063);
  49. b = MD5._ff(b, c, d, a, m[i+11], 22, -1990404162);
  50. a = MD5._ff(a, b, c, d, m[i+12], 7, 1804603682);
  51. d = MD5._ff(d, a, b, c, m[i+13], 12, -40341101);
  52. c = MD5._ff(c, d, a, b, m[i+14], 17, -1502002290);
  53. b = MD5._ff(b, c, d, a, m[i+15], 22, 1236535329);
  54. a = MD5._gg(a, b, c, d, m[i+ 1], 5, -165796510);
  55. d = MD5._gg(d, a, b, c, m[i+ 6], 9, -1069501632);
  56. c = MD5._gg(c, d, a, b, m[i+11], 14, 643717713);
  57. b = MD5._gg(b, c, d, a, m[i+ 0], 20, -373897302);
  58. a = MD5._gg(a, b, c, d, m[i+ 5], 5, -701558691);
  59. d = MD5._gg(d, a, b, c, m[i+10], 9, 38016083);
  60. c = MD5._gg(c, d, a, b, m[i+15], 14, -660478335);
  61. b = MD5._gg(b, c, d, a, m[i+ 4], 20, -405537848);
  62. a = MD5._gg(a, b, c, d, m[i+ 9], 5, 568446438);
  63. d = MD5._gg(d, a, b, c, m[i+14], 9, -1019803690);
  64. c = MD5._gg(c, d, a, b, m[i+ 3], 14, -187363961);
  65. b = MD5._gg(b, c, d, a, m[i+ 8], 20, 1163531501);
  66. a = MD5._gg(a, b, c, d, m[i+13], 5, -1444681467);
  67. d = MD5._gg(d, a, b, c, m[i+ 2], 9, -51403784);
  68. c = MD5._gg(c, d, a, b, m[i+ 7], 14, 1735328473);
  69. b = MD5._gg(b, c, d, a, m[i+12], 20, -1926607734);
  70. a = MD5._hh(a, b, c, d, m[i+ 5], 4, -378558);
  71. d = MD5._hh(d, a, b, c, m[i+ 8], 11, -2022574463);
  72. c = MD5._hh(c, d, a, b, m[i+11], 16, 1839030562);
  73. b = MD5._hh(b, c, d, a, m[i+14], 23, -35309556);
  74. a = MD5._hh(a, b, c, d, m[i+ 1], 4, -1530992060);
  75. d = MD5._hh(d, a, b, c, m[i+ 4], 11, 1272893353);
  76. c = MD5._hh(c, d, a, b, m[i+ 7], 16, -155497632);
  77. b = MD5._hh(b, c, d, a, m[i+10], 23, -1094730640);
  78. a = MD5._hh(a, b, c, d, m[i+13], 4, 681279174);
  79. d = MD5._hh(d, a, b, c, m[i+ 0], 11, -358537222);
  80. c = MD5._hh(c, d, a, b, m[i+ 3], 16, -722521979);
  81. b = MD5._hh(b, c, d, a, m[i+ 6], 23, 76029189);
  82. a = MD5._hh(a, b, c, d, m[i+ 9], 4, -640364487);
  83. d = MD5._hh(d, a, b, c, m[i+12], 11, -421815835);
  84. c = MD5._hh(c, d, a, b, m[i+15], 16, 530742520);
  85. b = MD5._hh(b, c, d, a, m[i+ 2], 23, -995338651);
  86. a = MD5._ii(a, b, c, d, m[i+ 0], 6, -198630844);
  87. d = MD5._ii(d, a, b, c, m[i+ 7], 10, 1126891415);
  88. c = MD5._ii(c, d, a, b, m[i+14], 15, -1416354905);
  89. b = MD5._ii(b, c, d, a, m[i+ 5], 21, -57434055);
  90. a = MD5._ii(a, b, c, d, m[i+12], 6, 1700485571);
  91. d = MD5._ii(d, a, b, c, m[i+ 3], 10, -1894986606);
  92. c = MD5._ii(c, d, a, b, m[i+10], 15, -1051523);
  93. b = MD5._ii(b, c, d, a, m[i+ 1], 21, -2054922799);
  94. a = MD5._ii(a, b, c, d, m[i+ 8], 6, 1873313359);
  95. d = MD5._ii(d, a, b, c, m[i+15], 10, -30611744);
  96. c = MD5._ii(c, d, a, b, m[i+ 6], 15, -1560198380);
  97. b = MD5._ii(b, c, d, a, m[i+13], 21, 1309151649);
  98. a = MD5._ii(a, b, c, d, m[i+ 4], 6, -145523070);
  99. d = MD5._ii(d, a, b, c, m[i+11], 10, -1120210379);
  100. c = MD5._ii(c, d, a, b, m[i+ 2], 15, 718787259);
  101. b = MD5._ii(b, c, d, a, m[i+ 9], 21, -343485551);
  102. a += aa;
  103. b += bb;
  104. c += cc;
  105. d += dd;
  106. }
  107. return util.endian([a, b, c, d]);
  108. };
  109. // Auxiliary functions
  110. MD5._ff = function (a, b, c, d, x, s, t) {
  111. var n = a + (b & c | ~b & d) + (x >>> 0) + t;
  112. return ((n << s) | (n >>> (32 - s))) + b;
  113. };
  114. MD5._gg = function (a, b, c, d, x, s, t) {
  115. var n = a + (b & d | c & ~d) + (x >>> 0) + t;
  116. return ((n << s) | (n >>> (32 - s))) + b;
  117. };
  118. MD5._hh = function (a, b, c, d, x, s, t) {
  119. var n = a + (b ^ c ^ d) + (x >>> 0) + t;
  120. return ((n << s) | (n >>> (32 - s))) + b;
  121. };
  122. MD5._ii = function (a, b, c, d, x, s, t) {
  123. var n = a + (c ^ (b | ~d)) + (x >>> 0) + t;
  124. return ((n << s) | (n >>> (32 - s))) + b;
  125. };
  126. // Package private blocksize
  127. MD5._blocksize = 16;
  128. })();