michael@0: /* michael@0: * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message michael@0: * Digest Algorithm, as defined in RFC 1321. michael@0: * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. michael@0: * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet michael@0: * Distributed under the BSD License michael@0: * See http://pajhome.org.uk/crypt/md5 for more info. michael@0: */ michael@0: michael@0: /* michael@0: * Configurable variables. You may need to tweak these to be compatible with michael@0: * the server-side, but the defaults work in most cases. michael@0: */ michael@0: var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ michael@0: var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ michael@0: var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ michael@0: michael@0: /* michael@0: * These are the functions you'll usually want to call michael@0: * They take string arguments and return either hex or base-64 encoded strings michael@0: */ michael@0: function hexmd5(s){ return binl2hex(coremd5(str2binl(s), s.length * chrsz));} michael@0: function b64md5(s){ return binl2b64(coremd5(str2binl(s), s.length * chrsz));} michael@0: function strmd5(s){ return binl2str(coremd5(str2binl(s), s.length * chrsz));} michael@0: function hexhmacmd5(key, data) { return binl2hex(corehmacmd5(key, data)); } michael@0: function b64hmacmd5(key, data) { return binl2b64(corehmacmd5(key, data)); } michael@0: function strhmacmd5(key, data) { return binl2str(corehmacmd5(key, data)); } michael@0: michael@0: /* michael@0: * Perform a simple self-test to see if the VM is working michael@0: */ michael@0: function md5vmtest() michael@0: { michael@0: return hexmd5("abc") == "900150983cd24fb0d6963f7d28e17f72"; michael@0: } michael@0: michael@0: /* michael@0: * Calculate the MD5 of an array of little-endian words, and a bit length michael@0: */ michael@0: function coremd5(x, len) michael@0: { michael@0: /* append padding */ michael@0: x[len >> 5] |= 0x80 << ((len) % 32); michael@0: x[(((len + 64) >>> 9) << 4) + 14] = len; michael@0: michael@0: var a = 1732584193; michael@0: var b = -271733879; michael@0: var c = -1732584194; michael@0: var d = 271733878; michael@0: michael@0: for(var i = 0; i < x.length; i += 16) michael@0: { michael@0: var olda = a; michael@0: var oldb = b; michael@0: var oldc = c; michael@0: var oldd = d; michael@0: michael@0: a = md5ff(a, b, c, d, x[i+ 0], 7 , -680876936); michael@0: d = md5ff(d, a, b, c, x[i+ 1], 12, -389564586); michael@0: c = md5ff(c, d, a, b, x[i+ 2], 17, 606105819); michael@0: b = md5ff(b, c, d, a, x[i+ 3], 22, -1044525330); michael@0: a = md5ff(a, b, c, d, x[i+ 4], 7 , -176418897); michael@0: d = md5ff(d, a, b, c, x[i+ 5], 12, 1200080426); michael@0: c = md5ff(c, d, a, b, x[i+ 6], 17, -1473231341); michael@0: b = md5ff(b, c, d, a, x[i+ 7], 22, -45705983); michael@0: a = md5ff(a, b, c, d, x[i+ 8], 7 , 1770035416); michael@0: d = md5ff(d, a, b, c, x[i+ 9], 12, -1958414417); michael@0: c = md5ff(c, d, a, b, x[i+10], 17, -42063); michael@0: b = md5ff(b, c, d, a, x[i+11], 22, -1990404162); michael@0: a = md5ff(a, b, c, d, x[i+12], 7 , 1804603682); michael@0: d = md5ff(d, a, b, c, x[i+13], 12, -40341101); michael@0: c = md5ff(c, d, a, b, x[i+14], 17, -1502002290); michael@0: b = md5ff(b, c, d, a, x[i+15], 22, 1236535329); michael@0: michael@0: a = md5gg(a, b, c, d, x[i+ 1], 5 , -165796510); michael@0: d = md5gg(d, a, b, c, x[i+ 6], 9 , -1069501632); michael@0: c = md5gg(c, d, a, b, x[i+11], 14, 643717713); michael@0: b = md5gg(b, c, d, a, x[i+ 0], 20, -373897302); michael@0: a = md5gg(a, b, c, d, x[i+ 5], 5 , -701558691); michael@0: d = md5gg(d, a, b, c, x[i+10], 9 , 38016083); michael@0: c = md5gg(c, d, a, b, x[i+15], 14, -660478335); michael@0: b = md5gg(b, c, d, a, x[i+ 4], 20, -405537848); michael@0: a = md5gg(a, b, c, d, x[i+ 9], 5 , 568446438); michael@0: d = md5gg(d, a, b, c, x[i+14], 9 , -1019803690); michael@0: c = md5gg(c, d, a, b, x[i+ 3], 14, -187363961); michael@0: b = md5gg(b, c, d, a, x[i+ 8], 20, 1163531501); michael@0: a = md5gg(a, b, c, d, x[i+13], 5 , -1444681467); michael@0: d = md5gg(d, a, b, c, x[i+ 2], 9 , -51403784); michael@0: c = md5gg(c, d, a, b, x[i+ 7], 14, 1735328473); michael@0: b = md5gg(b, c, d, a, x[i+12], 20, -1926607734); michael@0: michael@0: a = md5hh(a, b, c, d, x[i+ 5], 4 , -378558); michael@0: d = md5hh(d, a, b, c, x[i+ 8], 11, -2022574463); michael@0: c = md5hh(c, d, a, b, x[i+11], 16, 1839030562); michael@0: b = md5hh(b, c, d, a, x[i+14], 23, -35309556); michael@0: a = md5hh(a, b, c, d, x[i+ 1], 4 , -1530992060); michael@0: d = md5hh(d, a, b, c, x[i+ 4], 11, 1272893353); michael@0: c = md5hh(c, d, a, b, x[i+ 7], 16, -155497632); michael@0: b = md5hh(b, c, d, a, x[i+10], 23, -1094730640); michael@0: a = md5hh(a, b, c, d, x[i+13], 4 , 681279174); michael@0: d = md5hh(d, a, b, c, x[i+ 0], 11, -358537222); michael@0: c = md5hh(c, d, a, b, x[i+ 3], 16, -722521979); michael@0: b = md5hh(b, c, d, a, x[i+ 6], 23, 76029189); michael@0: a = md5hh(a, b, c, d, x[i+ 9], 4 , -640364487); michael@0: d = md5hh(d, a, b, c, x[i+12], 11, -421815835); michael@0: c = md5hh(c, d, a, b, x[i+15], 16, 530742520); michael@0: b = md5hh(b, c, d, a, x[i+ 2], 23, -995338651); michael@0: michael@0: a = md5ii(a, b, c, d, x[i+ 0], 6 , -198630844); michael@0: d = md5ii(d, a, b, c, x[i+ 7], 10, 1126891415); michael@0: c = md5ii(c, d, a, b, x[i+14], 15, -1416354905); michael@0: b = md5ii(b, c, d, a, x[i+ 5], 21, -57434055); michael@0: a = md5ii(a, b, c, d, x[i+12], 6 , 1700485571); michael@0: d = md5ii(d, a, b, c, x[i+ 3], 10, -1894986606); michael@0: c = md5ii(c, d, a, b, x[i+10], 15, -1051523); michael@0: b = md5ii(b, c, d, a, x[i+ 1], 21, -2054922799); michael@0: a = md5ii(a, b, c, d, x[i+ 8], 6 , 1873313359); michael@0: d = md5ii(d, a, b, c, x[i+15], 10, -30611744); michael@0: c = md5ii(c, d, a, b, x[i+ 6], 15, -1560198380); michael@0: b = md5ii(b, c, d, a, x[i+13], 21, 1309151649); michael@0: a = md5ii(a, b, c, d, x[i+ 4], 6 , -145523070); michael@0: d = md5ii(d, a, b, c, x[i+11], 10, -1120210379); michael@0: c = md5ii(c, d, a, b, x[i+ 2], 15, 718787259); michael@0: b = md5ii(b, c, d, a, x[i+ 9], 21, -343485551); michael@0: michael@0: a = safeadd(a, olda); michael@0: b = safeadd(b, oldb); michael@0: c = safeadd(c, oldc); michael@0: d = safeadd(d, oldd); michael@0: } michael@0: return Array(a, b, c, d); michael@0: michael@0: } michael@0: michael@0: /* michael@0: * These functions implement the four basic operations the algorithm uses. michael@0: */ michael@0: function md5cmn(q, a, b, x, s, t) michael@0: { michael@0: return safeadd(bitrol(safeadd(safeadd(a, q), safeadd(x, t)), s),b); michael@0: } michael@0: function md5ff(a, b, c, d, x, s, t) michael@0: { michael@0: return md5cmn((b & c) | ((~b) & d), a, b, x, s, t); michael@0: } michael@0: function md5gg(a, b, c, d, x, s, t) michael@0: { michael@0: return md5cmn((b & d) | (c & (~d)), a, b, x, s, t); michael@0: } michael@0: function md5hh(a, b, c, d, x, s, t) michael@0: { michael@0: return md5cmn(b ^ c ^ d, a, b, x, s, t); michael@0: } michael@0: function md5ii(a, b, c, d, x, s, t) michael@0: { michael@0: return md5cmn(c ^ (b | (~d)), a, b, x, s, t); michael@0: } michael@0: michael@0: /* michael@0: * Calculate the HMAC-MD5, of a key and some data michael@0: */ michael@0: function corehmacmd5(key, data) michael@0: { michael@0: var bkey = str2binl(key); michael@0: if(bkey.length > 16) bkey = coremd5(bkey, key.length * chrsz); michael@0: michael@0: var ipad = Array(16), opad = Array(16); michael@0: for(var i = 0; i < 16; i++) michael@0: { michael@0: ipad[i] = bkey[i] ^ 0x36363636; michael@0: opad[i] = bkey[i] ^ 0x5C5C5C5C; michael@0: } michael@0: michael@0: var hash = coremd5(ipad.concat(str2binl(data)), 512 + data.length * chrsz); michael@0: return coremd5(opad.concat(hash), 512 + 128); michael@0: } michael@0: michael@0: /* michael@0: * Add integers, wrapping at 2^32. This uses 16-bit operations internally michael@0: * to work around bugs in some JS interpreters. michael@0: */ michael@0: function safeadd(x, y) michael@0: { michael@0: var lsw = (x & 0xFFFF) + (y & 0xFFFF); michael@0: var msw = (x >> 16) + (y >> 16) + (lsw >> 16); michael@0: return (msw << 16) | (lsw & 0xFFFF); michael@0: } michael@0: michael@0: /* michael@0: * Bitwise rotate a 32-bit number to the left. michael@0: */ michael@0: function bitrol(num, cnt) michael@0: { michael@0: return (num << cnt) | (num >>> (32 - cnt)); michael@0: } michael@0: michael@0: /* michael@0: * Convert a string to an array of little-endian words michael@0: * If chrsz is ASCII, characters >255 have their hi-byte silently ignored. michael@0: */ michael@0: function str2binl(str) michael@0: { michael@0: var bin = Array(); michael@0: var mask = (1 << chrsz) - 1; michael@0: for(var i = 0; i < str.length * chrsz; i += chrsz) michael@0: bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32); michael@0: return bin; michael@0: } michael@0: michael@0: /* michael@0: * Convert an array of little-endian words to a string michael@0: */ michael@0: function binl2str(bin) michael@0: { michael@0: var str = ""; michael@0: var mask = (1 << chrsz) - 1; michael@0: for(var i = 0; i < bin.length * 32; i += chrsz) michael@0: str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask); michael@0: return str; michael@0: } michael@0: michael@0: /* michael@0: * Convert an array of little-endian words to a hex string. michael@0: */ michael@0: function binl2hex(binarray) michael@0: { michael@0: var hextab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; michael@0: var str = ""; michael@0: for(var i = 0; i < binarray.length * 4; i++) michael@0: { michael@0: str += hextab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + michael@0: hextab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF); michael@0: } michael@0: return str; michael@0: } michael@0: michael@0: /* michael@0: * Convert an array of little-endian words to a base-64 string michael@0: */ michael@0: function binl2b64(binarray) michael@0: { michael@0: var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; michael@0: var str = ""; michael@0: for(var i = 0; i < binarray.length * 4; i += 3) michael@0: { michael@0: var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16) michael@0: | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) michael@0: | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF); michael@0: for(var j = 0; j < 4; j++) michael@0: { michael@0: if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; michael@0: else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); michael@0: } michael@0: } michael@0: return str; michael@0: }