File indexing completed on 2024-12-22 05:35:18

0001 function md5cycle(x, k) {
0002 var a = x[0], b = x[1], c = x[2], d = x[3];
0003 
0004 a = ff(a, b, c, d, k[0], 7, -680876936);
0005 d = ff(d, a, b, c, k[1], 12, -389564586);
0006 c = ff(c, d, a, b, k[2], 17,  606105819);
0007 b = ff(b, c, d, a, k[3], 22, -1044525330);
0008 a = ff(a, b, c, d, k[4], 7, -176418897);
0009 d = ff(d, a, b, c, k[5], 12,  1200080426);
0010 c = ff(c, d, a, b, k[6], 17, -1473231341);
0011 b = ff(b, c, d, a, k[7], 22, -45705983);
0012 a = ff(a, b, c, d, k[8], 7,  1770035416);
0013 d = ff(d, a, b, c, k[9], 12, -1958414417);
0014 c = ff(c, d, a, b, k[10], 17, -42063);
0015 b = ff(b, c, d, a, k[11], 22, -1990404162);
0016 a = ff(a, b, c, d, k[12], 7,  1804603682);
0017 d = ff(d, a, b, c, k[13], 12, -40341101);
0018 c = ff(c, d, a, b, k[14], 17, -1502002290);
0019 b = ff(b, c, d, a, k[15], 22,  1236535329);
0020 
0021 a = gg(a, b, c, d, k[1], 5, -165796510);
0022 d = gg(d, a, b, c, k[6], 9, -1069501632);
0023 c = gg(c, d, a, b, k[11], 14,  643717713);
0024 b = gg(b, c, d, a, k[0], 20, -373897302);
0025 a = gg(a, b, c, d, k[5], 5, -701558691);
0026 d = gg(d, a, b, c, k[10], 9,  38016083);
0027 c = gg(c, d, a, b, k[15], 14, -660478335);
0028 b = gg(b, c, d, a, k[4], 20, -405537848);
0029 a = gg(a, b, c, d, k[9], 5,  568446438);
0030 d = gg(d, a, b, c, k[14], 9, -1019803690);
0031 c = gg(c, d, a, b, k[3], 14, -187363961);
0032 b = gg(b, c, d, a, k[8], 20,  1163531501);
0033 a = gg(a, b, c, d, k[13], 5, -1444681467);
0034 d = gg(d, a, b, c, k[2], 9, -51403784);
0035 c = gg(c, d, a, b, k[7], 14,  1735328473);
0036 b = gg(b, c, d, a, k[12], 20, -1926607734);
0037 
0038 a = hh(a, b, c, d, k[5], 4, -378558);
0039 d = hh(d, a, b, c, k[8], 11, -2022574463);
0040 c = hh(c, d, a, b, k[11], 16,  1839030562);
0041 b = hh(b, c, d, a, k[14], 23, -35309556);
0042 a = hh(a, b, c, d, k[1], 4, -1530992060);
0043 d = hh(d, a, b, c, k[4], 11,  1272893353);
0044 c = hh(c, d, a, b, k[7], 16, -155497632);
0045 b = hh(b, c, d, a, k[10], 23, -1094730640);
0046 a = hh(a, b, c, d, k[13], 4,  681279174);
0047 d = hh(d, a, b, c, k[0], 11, -358537222);
0048 c = hh(c, d, a, b, k[3], 16, -722521979);
0049 b = hh(b, c, d, a, k[6], 23,  76029189);
0050 a = hh(a, b, c, d, k[9], 4, -640364487);
0051 d = hh(d, a, b, c, k[12], 11, -421815835);
0052 c = hh(c, d, a, b, k[15], 16,  530742520);
0053 b = hh(b, c, d, a, k[2], 23, -995338651);
0054 
0055 a = ii(a, b, c, d, k[0], 6, -198630844);
0056 d = ii(d, a, b, c, k[7], 10,  1126891415);
0057 c = ii(c, d, a, b, k[14], 15, -1416354905);
0058 b = ii(b, c, d, a, k[5], 21, -57434055);
0059 a = ii(a, b, c, d, k[12], 6,  1700485571);
0060 d = ii(d, a, b, c, k[3], 10, -1894986606);
0061 c = ii(c, d, a, b, k[10], 15, -1051523);
0062 b = ii(b, c, d, a, k[1], 21, -2054922799);
0063 a = ii(a, b, c, d, k[8], 6,  1873313359);
0064 d = ii(d, a, b, c, k[15], 10, -30611744);
0065 c = ii(c, d, a, b, k[6], 15, -1560198380);
0066 b = ii(b, c, d, a, k[13], 21,  1309151649);
0067 a = ii(a, b, c, d, k[4], 6, -145523070);
0068 d = ii(d, a, b, c, k[11], 10, -1120210379);
0069 c = ii(c, d, a, b, k[2], 15,  718787259);
0070 b = ii(b, c, d, a, k[9], 21, -343485551);
0071 
0072 x[0] = add32(a, x[0]);
0073 x[1] = add32(b, x[1]);
0074 x[2] = add32(c, x[2]);
0075 x[3] = add32(d, x[3]);
0076 
0077 }
0078 
0079 function cmn(q, a, b, x, s, t) {
0080 a = add32(add32(a, q), add32(x, t));
0081 return add32((a << s) | (a >>> (32 - s)), b);
0082 }
0083 
0084 function ff(a, b, c, d, x, s, t) {
0085 return cmn((b & c) | ((~b) & d), a, b, x, s, t);
0086 }
0087 
0088 function gg(a, b, c, d, x, s, t) {
0089 return cmn((b & d) | (c & (~d)), a, b, x, s, t);
0090 }
0091 
0092 function hh(a, b, c, d, x, s, t) {
0093 return cmn(b ^ c ^ d, a, b, x, s, t);
0094 }
0095 
0096 function ii(a, b, c, d, x, s, t) {
0097 return cmn(c ^ (b | (~d)), a, b, x, s, t);
0098 }
0099 
0100 function md51(s) {
0101 txt = '';
0102 var n = s.length,
0103 state = [1732584193, -271733879, -1732584194, 271733878], i;
0104 for (i=64; i<=s.length; i+=64) {
0105 md5cycle(state, md5blk(s.substring(i-64, i)));
0106 }
0107 s = s.substring(i-64);
0108 var tail = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0];
0109 for (i=0; i<s.length; i++)
0110 tail[i>>2] |= s.charCodeAt(i) << ((i%4) << 3);
0111 tail[i>>2] |= 0x80 << ((i%4) << 3);
0112 if (i > 55) {
0113 md5cycle(state, tail);
0114 for (i=0; i<16; i++) tail[i] = 0;
0115 }
0116 tail[14] = n*8;
0117 md5cycle(state, tail);
0118 return state;
0119 }
0120 
0121 /* there needs to be support for Unicode here,
0122  * unless we pretend that we can redefine the MD-5
0123  * algorithm for multi-byte characters (perhaps
0124  * by adding every four 16-bit characters and
0125  * shortening the sum to 32 bits). Otherwise
0126  * I suggest performing MD-5 as if every character
0127  * was two bytes--e.g., 0040 0025 = @%--but then
0128  * how will an ordinary MD-5 sum be matched?
0129  * There is no way to standardize text to something
0130  * like UTF-8 before transformation; speed cost is
0131  * utterly prohibitive. The JavaScript standard
0132  * itself needs to look at this: it should start
0133  * providing access to strings as preformed UTF-8
0134  * 8-bit unsigned value arrays.
0135  */
0136 function md5blk(s) { /* I figured global was faster.   */
0137 var md5blks = [], i; /* Andy King said do it this way. */
0138 for (i=0; i<64; i+=4) {
0139 md5blks[i>>2] = s.charCodeAt(i)
0140 + (s.charCodeAt(i+1) << 8)
0141 + (s.charCodeAt(i+2) << 16)
0142 + (s.charCodeAt(i+3) << 24);
0143 }
0144 return md5blks;
0145 }
0146 
0147 var hex_chr = '0123456789abcdef'.split('');
0148 
0149 function rhex(n)
0150 {
0151 var s='', j=0;
0152 for(; j<4; j++)
0153 s += hex_chr[(n >> (j * 8 + 4)) & 0x0F]
0154 + hex_chr[(n >> (j * 8)) & 0x0F];
0155 return s;
0156 }
0157 
0158 function hex(x) {
0159 for (var i=0; i<x.length; i++)
0160 x[i] = rhex(x[i]);
0161 return x.join('');
0162 }
0163 
0164 function md5(s) {
0165 return hex(md51(s));
0166 }
0167 
0168 /* this function is much faster,
0169 so if possible we use it. Some IEs
0170 are the only ones I know of that
0171 need the idiotic second function,
0172 generated by an if clause.  */
0173 
0174 function add32(a, b) {
0175 return (a + b) & 0xFFFFFFFF;
0176 }
0177 
0178 if (md5('hello') != '5d41402abc4b2a76b9719d911017c592') {
0179 function add32(x, y) {
0180 var lsw = (x & 0xFFFF) + (y & 0xFFFF),
0181 msw = (x >> 16) + (y >> 16) + (lsw >> 16);
0182 return (msw << 16) | (lsw & 0xFFFF);
0183 }
0184 }