md5.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  3. * Digest Algorithm, as defined in RFC 1321.
  4. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
  5. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  6. * Distributed under the BSD License
  7. * See http://pajhome.org.uk/crypt/md5 for more info.
  8. */
  9. /*
  10. * Configurable variables. You may need to tweak these to be compatible with
  11. * the server-side, but the defaults work in most cases.
  12. */
  13. var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  14. var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
  15. var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
  16. /*
  17. * These are the functions you'll usually want to call
  18. * They take string arguments and return either hex or base-64 encoded strings
  19. */
  20. function hex_md5(s) {
  21. return binl2hex(core_md5(str2binl(s), s.length * chrsz));
  22. }
  23. function b64_md5(s) {
  24. return binl2b64(core_md5(str2binl(s), s.length * chrsz));
  25. }
  26. function str_md5(s) {
  27. return binl2str(core_md5(str2binl(s), s.length * chrsz));
  28. }
  29. function hex_hmac_md5(key, data) {
  30. return binl2hex(core_hmac_md5(key, data));
  31. }
  32. function b64_hmac_md5(key, data) {
  33. return binl2b64(core_hmac_md5(key, data));
  34. }
  35. function str_hmac_md5(key, data) {
  36. return binl2str(core_hmac_md5(key, data));
  37. }
  38. function core_md5(c, g) {
  39. c[g >> 5] |= 128 << g % 32;
  40. c[(g + 64 >>> 9 << 4) + 14] = g;
  41. for (var a = 1732584193, b = -271733879, d = -1732584194, e = 271733878, f = 0; f < c.length; f += 16) var h = a, i = b, j = d, k = e, a = md5_ff(a, b, d, e, c[f + 0], 7, -680876936), e = md5_ff(e, a, b, d, c[f + 1], 12, -389564586), d = md5_ff(d, e, a, b, c[f + 2], 17, 606105819), b = md5_ff(b, d, e, a, c[f + 3], 22, -1044525330), a = md5_ff(a, b, d, e, c[f + 4], 7, -176418897), e = md5_ff(e, a, b, d, c[f + 5], 12, 1200080426), d = md5_ff(d, e, a, b, c[f + 6], 17, -1473231341), b = md5_ff(b, d, e, a, c[f + 7], 22, -45705983), a = md5_ff(a, b, d, e, c[f + 8], 7, 1770035416), e = md5_ff(e, a, b, d, c[f + 9], 12, -1958414417), d = md5_ff(d, e, a, b, c[f + 10], 17, -42063), b = md5_ff(b, d, e, a, c[f + 11], 22, -1990404162), a = md5_ff(a, b, d, e, c[f + 12], 7, 1804603682), e = md5_ff(e, a, b, d, c[f + 13], 12, -40341101), d = md5_ff(d, e, a, b, c[f + 14], 17, -1502002290), b = md5_ff(b, d, e, a, c[f + 15], 22, 1236535329), a = md5_gg(a, b, d, e, c[f + 1], 5, -165796510), e = md5_gg(e, a, b, d, c[f + 6], 9, -1069501632), d = md5_gg(d, e, a, b, c[f + 11], 14, 643717713), b = md5_gg(b, d, e, a, c[f + 0], 20, -373897302), a = md5_gg(a, b, d, e, c[f + 5], 5, -701558691), e = md5_gg(e, a, b, d, c[f + 10], 9, 38016083), d = md5_gg(d, e, a, b, c[f + 15], 14, -660478335), b = md5_gg(b, d, e, a, c[f + 4], 20, -405537848), a = md5_gg(a, b, d, e, c[f + 9], 5, 568446438), e = md5_gg(e, a, b, d, c[f + 14], 9, -1019803690), d = md5_gg(d, e, a, b, c[f + 3], 14, -187363961), b = md5_gg(b, d, e, a, c[f + 8], 20, 1163531501), a = md5_gg(a, b, d, e, c[f + 13], 5, -1444681467), e = md5_gg(e, a, b, d, c[f + 2], 9, -51403784), d = md5_gg(d, e, a, b, c[f + 7], 14, 1735328473), b = md5_gg(b, d, e, a, c[f + 12], 20, -1926607734), a = md5_hh(a, b, d, e, c[f + 5], 4, -378558), e = md5_hh(e, a, b, d, c[f + 8], 11, -2022574463), d = md5_hh(d, e, a, b, c[f + 11], 16, 1839030562), b = md5_hh(b, d, e, a, c[f + 14], 23, -35309556), a = md5_hh(a, b, d, e, c[f + 1], 4, -1530992060), e = md5_hh(e, a, b, d, c[f + 4], 11, 1272893353), d = md5_hh(d, e, a, b, c[f + 7], 16, -155497632), b = md5_hh(b, d, e, a, c[f + 10], 23, -1094730640), a = md5_hh(a, b, d, e, c[f + 13], 4, 681279174), e = md5_hh(e, a, b, d, c[f + 0], 11, -358537222), d = md5_hh(d, e, a, b, c[f + 3], 16, -722521979), b = md5_hh(b, d, e, a, c[f + 6], 23, 76029189), a = md5_hh(a, b, d, e, c[f + 9], 4, -640364487), e = md5_hh(e, a, b, d, c[f + 12], 11, -421815835), d = md5_hh(d, e, a, b, c[f + 15], 16, 530742520), b = md5_hh(b, d, e, a, c[f + 2], 23, -995338651), a = md5_ii(a, b, d, e, c[f + 0], 6, -198630844), e = md5_ii(e, a, b, d, c[f + 7], 10, 1126891415), d = md5_ii(d, e, a, b, c[f + 14], 15, -1416354905), b = md5_ii(b, d, e, a, c[f + 5], 21, -57434055), a = md5_ii(a, b, d, e, c[f + 12], 6, 1700485571), e = md5_ii(e, a, b, d, c[f + 3], 10, -1894986606), d = md5_ii(d, e, a, b, c[f + 10], 15, -1051523), b = md5_ii(b, d, e, a, c[f + 1], 21, -2054922799), a = md5_ii(a, b, d, e, c[f + 8], 6, 1873313359), e = md5_ii(e, a, b, d, c[f + 15], 10, -30611744), d = md5_ii(d, e, a, b, c[f + 6], 15, -1560198380), b = md5_ii(b, d, e, a, c[f + 13], 21, 1309151649), a = md5_ii(a, b, d, e, c[f + 4], 6, -145523070), e = md5_ii(e, a, b, d, c[f + 11], 10, -1120210379), d = md5_ii(d, e, a, b, c[f + 2], 15, 718787259), b = md5_ii(b, d, e, a, c[f + 9], 21, -343485551), a = safe_add(a, h), b = safe_add(b, i), d = safe_add(d, j), e = safe_add(e, k);
  42. return [a, b, d, e]
  43. }
  44. function md5_cmn(c, g, a, b, d, e) {
  45. return safe_add(bit_rol(safe_add(safe_add(g, c), safe_add(b, e)), d), a)
  46. }
  47. function md5_ff(c, g, a, b, d, e, f) {
  48. return md5_cmn(g & a | ~g & b, c, g, d, e, f)
  49. }
  50. function md5_gg(c, g, a, b, d, e, f) {
  51. return md5_cmn(g & b | a & ~b, c, g, d, e, f)
  52. }
  53. function md5_hh(c, g, a, b, d, e, f) {
  54. return md5_cmn(g ^ a ^ b, c, g, d, e, f)
  55. }
  56. function md5_ii(c, g, a, b, d, e, f) {
  57. return md5_cmn(a ^ (g | ~b), c, g, d, e, f)
  58. }
  59. function core_hmac_md5(c, g) {
  60. var a = str2binl(c);
  61. 16 < a.length && (a = core_md5(a, c.length * chrsz));
  62. for (var b = Array(16), d = Array(16), e = 0; 16 > e; e++) b[e] = a[e] ^ 909522486, d[e] = a[e] ^ 1549556828;
  63. a = core_md5(b.concat(str2binl(g)), 512 + g.length * chrsz);
  64. return core_md5(d.concat(a), 640)
  65. }
  66. function safe_add(c, g) {
  67. var a = (c & 65535) + (g & 65535);
  68. return (c >> 16) + (g >> 16) + (a >> 16) << 16 | a & 65535
  69. }
  70. function bit_rol(c, g) {
  71. return c << g | c >>> 32 - g
  72. }
  73. function str2binl(c) {
  74. for (var g = [], a = (1 << chrsz) - 1, b = 0; b < c.length * chrsz; b += chrsz) g[b >> 5] |= (c.charCodeAt(b / chrsz) & a) << b % 32;
  75. return g
  76. }
  77. function binl2str(c) {
  78. for (var g = "", a = (1 << chrsz) - 1, b = 0; b < 32 * c.length; b += chrsz) g += String.fromCharCode(c[b >> 5] >>> b % 32 & a);
  79. return g
  80. }
  81. function binl2hex(c) {
  82. for (var g = hexcase ? "0123456789ABCDEF" : "0123456789abcdef", a = "", b = 0; b < 4 * c.length; b++) a += g.charAt(c[b >> 2] >> 8 * (b % 4) + 4 & 15) + g.charAt(c[b >> 2] >> 8 * (b % 4) & 15);
  83. return a
  84. }
  85. function binl2b64(c) {
  86. for (var g = "", a = 0; a < 4 * c.length; a += 3) for (var b = (c[a >> 2] >> 8 * (a % 4) & 255) << 16 | (c[a + 1 >> 2] >> 8 * ((a + 1) % 4) & 255) << 8 | c[a + 2 >> 2] >> 8 * ((a + 2) % 4) & 255, d = 0; 4 > d; d++) g = 8 * a + 6 * d > 32 * c.length ? g + b64pad : g + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(b >> 6 * (3 - d) & 63);
  87. return g
  88. }
  89. function str_to_ent(c) {
  90. var g = "", a;
  91. for (a = 0; a < c.length; a++) {
  92. var b = c.charCodeAt(a), d = "";
  93. if (255 < b) {
  94. for (; 1 <= b;) d = "0123456789".charAt(b % 10) + d, b /= 10;
  95. "" == d && (d = "0");
  96. d = "#" + d;
  97. d = "&" + d;
  98. d += ";";
  99. g += d
  100. } else g += c.charAt(a)
  101. }
  102. return g
  103. }
  104. function trim(c) {
  105. for (; " " == c.substring(0, 1);) c = c.substring(1, c.length);
  106. for (; " " == c.substring(c.length - 1, c.length);) c = c.substring(0, c.length - 1);
  107. return c
  108. };