md5.js 6.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. var hexcase = 0, b64pad = '', chrsz = 8;
  3. function hex_md5(d) {
  4. return binl2hex(core_md5(str2binl(d), d.length * chrsz));
  5. }
  6. function b64_md5(d) {
  7. return binl2b64(core_md5(str2binl(d), d.length * chrsz));
  8. }
  9. function str_md5(d) {
  10. return binl2str(core_md5(str2binl(d), d.length * chrsz));
  11. }
  12. function hex_hmac_md5(d, r) {
  13. return binl2hex(core_hmac_md5(d, r));
  14. }
  15. function b64_hmac_md5(d, r) {
  16. return binl2b64(core_hmac_md5(d, r));
  17. }
  18. function str_hmac_md5(d, r) {
  19. return binl2str(core_hmac_md5(d, r));
  20. }
  21. function md5_vm_test() {
  22. return '900150983cd24fb0d6963f7d28e17f72' == hex_md5('abc');
  23. }
  24. function core_md5(d, r) {
  25. d[r >> 5] |= 128 << r % 32, d[14 + (r + 64 >>> 9 << 4)] = r;
  26. for (var _ = 1732584193, m = -271733879, n = -1732584194, h = 271733878, t = 0; t < d.length; t += 16) {
  27. var f = _, i = m, c = n, e = h;
  28. _ = md5_ff(_, m, n, h, d[t + 0], 7, -680876936), h = md5_ff(h, _, m, n, d[t + 1], 12, -389564586), n = md5_ff(n, h, _, m, d[t + 2], 17, 606105819), m = md5_ff(m, n, h, _, d[t + 3], 22, -1044525330), _ = md5_ff(_, m, n, h, d[t + 4], 7, -176418897), h = md5_ff(h, _, m, n, d[t + 5], 12, 1200080426), n = md5_ff(n, h, _, m, d[t + 6], 17, -1473231341), m = md5_ff(m, n, h, _, d[t + 7], 22, -45705983), _ = md5_ff(_, m, n, h, d[t + 8], 7, 1770035416), h = md5_ff(h, _, m, n, d[t + 9], 12, -1958414417), n = md5_ff(n, h, _, m, d[t + 10], 17, -42063), m = md5_ff(m, n, h, _, d[t + 11], 22, -1990404162), _ = md5_ff(_, m, n, h, d[t + 12], 7, 1804603682), h = md5_ff(h, _, m, n, d[t + 13], 12, -40341101), n = md5_ff(n, h, _, m, d[t + 14], 17, -1502002290), _ = md5_gg(_, m = md5_ff(m, n, h, _, d[t + 15], 22, 1236535329), n, h, d[t + 1], 5, -165796510), h = md5_gg(h, _, m, n, d[t + 6], 9, -1069501632), n = md5_gg(n, h, _, m, d[t + 11], 14, 643717713), m = md5_gg(m, n, h, _, d[t + 0], 20, -373897302), _ = md5_gg(_, m, n, h, d[t + 5], 5, -701558691), h = md5_gg(h, _, m, n, d[t + 10], 9, 38016083), n = md5_gg(n, h, _, m, d[t + 15], 14, -660478335), m = md5_gg(m, n, h, _, d[t + 4], 20, -405537848), _ = md5_gg(_, m, n, h, d[t + 9], 5, 568446438), h = md5_gg(h, _, m, n, d[t + 14], 9, -1019803690), n = md5_gg(n, h, _, m, d[t + 3], 14, -187363961), m = md5_gg(m, n, h, _, d[t + 8], 20, 1163531501), _ = md5_gg(_, m, n, h, d[t + 13], 5, -1444681467), h = md5_gg(h, _, m, n, d[t + 2], 9, -51403784), n = md5_gg(n, h, _, m, d[t + 7], 14, 1735328473), _ = md5_hh(_, m = md5_gg(m, n, h, _, d[t + 12], 20, -1926607734), n, h, d[t + 5], 4, -378558), h = md5_hh(h, _, m, n, d[t + 8], 11, -2022574463), n = md5_hh(n, h, _, m, d[t + 11], 16, 1839030562), m = md5_hh(m, n, h, _, d[t + 14], 23, -35309556), _ = md5_hh(_, m, n, h, d[t + 1], 4, -1530992060), h = md5_hh(h, _, m, n, d[t + 4], 11, 1272893353), n = md5_hh(n, h, _, m, d[t + 7], 16, -155497632), m = md5_hh(m, n, h, _, d[t + 10], 23, -1094730640), _ = md5_hh(_, m, n, h, d[t + 13], 4, 681279174), h = md5_hh(h, _, m, n, d[t + 0], 11, -358537222), n = md5_hh(n, h, _, m, d[t + 3], 16, -722521979), m = md5_hh(m, n, h, _, d[t + 6], 23, 76029189), _ = md5_hh(_, m, n, h, d[t + 9], 4, -640364487), h = md5_hh(h, _, m, n, d[t + 12], 11, -421815835), n = md5_hh(n, h, _, m, d[t + 15], 16, 530742520), _ = md5_ii(_, m = md5_hh(m, n, h, _, d[t + 2], 23, -995338651), n, h, d[t + 0], 6, -198630844), h = md5_ii(h, _, m, n, d[t + 7], 10, 1126891415), n = md5_ii(n, h, _, m, d[t + 14], 15, -1416354905), m = md5_ii(m, n, h, _, d[t + 5], 21, -57434055), _ = md5_ii(_, m, n, h, d[t + 12], 6, 1700485571), h = md5_ii(h, _, m, n, d[t + 3], 10, -1894986606), n = md5_ii(n, h, _, m, d[t + 10], 15, -1051523), m = md5_ii(m, n, h, _, d[t + 1], 21, -2054922799), _ = md5_ii(_, m, n, h, d[t + 8], 6, 1873313359), h = md5_ii(h, _, m, n, d[t + 15], 10, -30611744), n = md5_ii(n, h, _, m, d[t + 6], 15, -1560198380), m = md5_ii(m, n, h, _, d[t + 13], 21, 1309151649), _ = md5_ii(_, m, n, h, d[t + 4], 6, -145523070), h = md5_ii(h, _, m, n, d[t + 11], 10, -1120210379), n = md5_ii(n, h, _, m, d[t + 2], 15, 718787259), m = md5_ii(m, n, h, _, d[t + 9], 21, -343485551), _ = safe_add(_, f), m = safe_add(m, i), n = safe_add(n, c), h = safe_add(h, e);
  29. }
  30. return Array(_, m, n, h);
  31. }
  32. function md5_cmn(d, r, _, m, n, h) {
  33. return safe_add(bit_rol(safe_add(safe_add(r, d), safe_add(m, h)), n), _);
  34. }
  35. function md5_ff(d, r, _, m, n, h, t) {
  36. return md5_cmn(r & _ | ~r & m, d, r, n, h, t);
  37. }
  38. function md5_gg(d, r, _, m, n, h, t) {
  39. return md5_cmn(r & m | _ & ~m, d, r, n, h, t);
  40. }
  41. function md5_hh(d, r, _, m, n, h, t) {
  42. return md5_cmn(r ^ _ ^ m, d, r, n, h, t);
  43. }
  44. function md5_ii(d, r, _, m, n, h, t) {
  45. return md5_cmn(_ ^ (r | ~m), d, r, n, h, t);
  46. }
  47. function core_hmac_md5(d, r) {
  48. var _ = str2binl(d);
  49. 16 < _.length && (_ = core_md5(_, d.length * chrsz));
  50. for (var m = Array(16), n = Array(16), h = 0; h < 16; h++)
  51. m[h] = 909522486 ^ _[h], n[h] = 1549556828 ^ _[h];
  52. var t = core_md5(m.concat(str2binl(r)), 512 + r.length * chrsz);
  53. return core_md5(n.concat(t), 640);
  54. }
  55. function safe_add(d, r) {
  56. var _ = (65535 & d) + (65535 & r);
  57. return (d >> 16) + (r >> 16) + (_ >> 16) << 16 | 65535 & _;
  58. }
  59. function bit_rol(d, r) {
  60. return d << r | d >>> 32 - r;
  61. }
  62. function str2binl(d) {
  63. for (var r = Array(), _ = (1 << chrsz) - 1, m = 0; m < d.length * chrsz; m += chrsz)
  64. r[m >> 5] |= (d.charCodeAt(m / chrsz) & _) << m % 32;
  65. return r;
  66. }
  67. function binl2str(d) {
  68. for (var r = '', _ = (1 << chrsz) - 1, m = 0; m < 32 * d.length; m += chrsz)
  69. r += String.fromCharCode(d[m >> 5] >>> m % 32 & _);
  70. return r;
  71. }
  72. function binl2hex(d) {
  73. for (var r = hexcase ? '0123456789ABCDEF' : '0123456789abcdef', _ = '', m = 0; m < 4 * d.length; m++)
  74. _ += r.charAt(d[m >> 2] >> m % 4 * 8 + 4 & 15) + r.charAt(d[m >> 2] >> m % 4 * 8 & 15);
  75. return _;
  76. }
  77. function binl2b64(d) {
  78. for (var r = '', _ = 0; _ < 4 * d.length; _ += 3)
  79. for (var m = (d[_ >> 2] >> _ % 4 * 8 & 255) << 16 | (d[_ + 1 >> 2] >> (_ + 1) % 4 * 8 & 255) << 8 | d[_ + 2 >> 2] >> (_ + 2) % 4 * 8 & 255, n = 0; n < 4; n++)
  80. 8 * _ + 6 * n > 32 * d.length ? r += b64pad : r += 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.charAt(m >> 6 * (3 - n) & 63);
  81. return r;
  82. }