1/* 2* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message 3* Digest Algorithm, as defined in RFC 1321. 4* Version 1.1 Copyright (C) Paul Johnston 1999 - 2002. 5* Code also contributed by Greg Holt 6* See http://pajhome.org.uk/site/legal.html for details. 7*/ 8/* 9* Add integers, wrapping at 2^32. This uses 16-bit operations internally 10* to work around bugs in some JS interpreters. 11*/ 12function safe_add(x, y) 13{var lsw = (x & 0xFFFF) + (y & 0xFFFF) 14var msw = (x >> 16) + (y >> 16) + (lsw >> 16) 15return (msw << 16) | (lsw & 0xFFFF) 16} 17/* 18* Bitwise rotate a 32-bit number to the left. 19*/ 20function rol(num, cnt) 21{return (num << cnt) | (num >>> (32 - cnt)) 22} 23/* 24* These functions implement the four basic operations the algorithm uses. 25*/ 26function cmn(q, a, b, x, s, t) 27{return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b) 28} 29function ff(a, b, c, d, x, s, t) 30{return cmn((b & c) | ((~b) & d), a, b, x, s, t) 31} 32function gg(a, b, c, d, x, s, t) 33{return cmn((b & d) | (c & (~d)), a, b, x, s, t) 34} 35function hh(a, b, c, d, x, s, t) 36{return cmn(b ^ c ^ d, a, b, x, s, t) 37} 38function ii(a, b, c, d, x, s, t) 39{return cmn(c ^ (b | (~d)), a, b, x, s, t) 40} 41/* 42* Calculate the MD5 of an array of little-endian words, producing an array 43* of little-endian words. 44*/ 45function coreMD5(x) 46{var a = 1732584193 47var b = -271733879 48var c = -1732584194 49var d = 271733878 50for(i = 0; i < x.length; i += 16) 51{var olda = a 52var oldb = b 53var oldc = c 54var oldd = d 55a = ff(a, b, c, d, x[i+ 0], 7 , -680876936) 56d = ff(d, a, b, c, x[i+ 1], 12, -389564586) 57c = ff(c, d, a, b, x[i+ 2], 17, 606105819) 58b = ff(b, c, d, a, x[i+ 3], 22, -1044525330) 59a = ff(a, b, c, d, x[i+ 4], 7 , -176418897) 60d = ff(d, a, b, c, x[i+ 5], 12, 1200080426) 61c = ff(c, d, a, b, x[i+ 6], 17, -1473231341) 62b = ff(b, c, d, a, x[i+ 7], 22, -45705983) 63a = ff(a, b, c, d, x[i+ 8], 7 , 1770035416) 64d = ff(d, a, b, c, x[i+ 9], 12, -1958414417) 65c = ff(c, d, a, b, x[i+10], 17, -42063) 66b = ff(b, c, d, a, x[i+11], 22, -1990404162) 67a = ff(a, b, c, d, x[i+12], 7 , 1804603682) 68d = ff(d, a, b, c, x[i+13], 12, -40341101) 69c = ff(c, d, a, b, x[i+14], 17, -1502002290) 70b = ff(b, c, d, a, x[i+15], 22, 1236535329) 71a = gg(a, b, c, d, x[i+ 1], 5 , -165796510) 72d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632) 73c = gg(c, d, a, b, x[i+11], 14, 643717713) 74b = gg(b, c, d, a, x[i+ 0], 20, -373897302) 75a = gg(a, b, c, d, x[i+ 5], 5 , -701558691) 76d = gg(d, a, b, c, x[i+10], 9 , 38016083) 77c = gg(c, d, a, b, x[i+15], 14, -660478335) 78b = gg(b, c, d, a, x[i+ 4], 20, -405537848) 79a = gg(a, b, c, d, x[i+ 9], 5 , 568446438) 80d = gg(d, a, b, c, x[i+14], 9 , -1019803690) 81c = gg(c, d, a, b, x[i+ 3], 14, -187363961) 82b = gg(b, c, d, a, x[i+ 8], 20, 1163531501) 83a = gg(a, b, c, d, x[i+13], 5 , -1444681467) 84d = gg(d, a, b, c, x[i+ 2], 9 , -51403784) 85c = gg(c, d, a, b, x[i+ 7], 14, 1735328473) 86b = gg(b, c, d, a, x[i+12], 20, -1926607734) 87a = hh(a, b, c, d, x[i+ 5], 4 , -378558) 88d = hh(d, a, b, c, x[i+ 8], 11, -2022574463) 89c = hh(c, d, a, b, x[i+11], 16, 1839030562) 90b = hh(b, c, d, a, x[i+14], 23, -35309556) 91a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060) 92d = hh(d, a, b, c, x[i+ 4], 11, 1272893353) 93c = hh(c, d, a, b, x[i+ 7], 16, -155497632) 94b = hh(b, c, d, a, x[i+10], 23, -1094730640) 95a = hh(a, b, c, d, x[i+13], 4 , 681279174) 96d = hh(d, a, b, c, x[i+ 0], 11, -358537222) 97c = hh(c, d, a, b, x[i+ 3], 16, -722521979) 98b = hh(b, c, d, a, x[i+ 6], 23, 76029189) 99a = hh(a, b, c, d, x[i+ 9], 4 , -640364487) 100d = hh(d, a, b, c, x[i+12], 11, -421815835) 101c = hh(c, d, a, b, x[i+15], 16, 530742520) 102b = hh(b, c, d, a, x[i+ 2], 23, -995338651) 103a = ii(a, b, c, d, x[i+ 0], 6 , -198630844) 104d = ii(d, a, b, c, x[i+ 7], 10, 1126891415) 105c = ii(c, d, a, b, x[i+14], 15, -1416354905) 106b = ii(b, c, d, a, x[i+ 5], 21, -57434055) 107a = ii(a, b, c, d, x[i+12], 6 , 1700485571) 108d = ii(d, a, b, c, x[i+ 3], 10, -1894986606) 109c = ii(c, d, a, b, x[i+10], 15, -1051523) 110b = ii(b, c, d, a, x[i+ 1], 21, -2054922799) 111a = ii(a, b, c, d, x[i+ 8], 6 , 1873313359) 112d = ii(d, a, b, c, x[i+15], 10, -30611744) 113c = ii(c, d, a, b, x[i+ 6], 15, -1560198380) 114b = ii(b, c, d, a, x[i+13], 21, 1309151649) 115a = ii(a, b, c, d, x[i+ 4], 6 , -145523070) 116d = ii(d, a, b, c, x[i+11], 10, -1120210379) 117c = ii(c, d, a, b, x[i+ 2], 15, 718787259) 118b = ii(b, c, d, a, x[i+ 9], 21, -343485551) 119a = safe_add(a, olda) 120b = safe_add(b, oldb) 121c = safe_add(c, oldc) 122d = safe_add(d, oldd) 123} 124return [a, b, c, d] 125} 126/* 127* Convert an array of little-endian words to a hex string. 128*/ 129function binl2hex(binarray) 130{var hex_tab = "0123456789ABCDEF" 131var str = "" 132for(var i = 0; i < binarray.length * 4; i++) 133{str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + 134hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF) 135} 136return str 137} 138/* 139* Convert an array of little-endian words to a hex string. 140*/ 141function binl2hex_c(binarray) 142{var hex_tab = "0123456789ABCDEF" 143var str = "" 144for(var i = 0; i < binarray.length; i++) 145{str += hex_tab.charAt((binarray[i] >> 4) & 0xF) + 146hex_tab.charAt((binarray[i]) & 0xF) 147} 148return str 149} 150/* 151* Convert an array of little-endian words to a base64 encoded string. 152*/ 153function binl2b64(binarray) 154{var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 155var str = "" 156for(var i = 0; i < binarray.length * 32; i += 6) 157{str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) | 158((binarray[i>>5+1] >> (32-i%32)) & 0x3F)) 159} 160return str 161} 162/* 163* Convert an 8-bit character string to a sequence of 16-word blocks, stored 164* as an array, and append appropriate padding for MD4/5 calculation. 165* If any of the characters are >255, the high byte is silently ignored. 166*/ 167function str2binl(str) 168{var nblk = ((str.length + 8) >> 6) + 1 169var blks = new Array(nblk * 16) 170for(var i = 0; i < nblk * 16; i++) blks[i] = 0 171for(var i = 0; i < str.length; i++) 172blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8) 173blks[i>>2] |= 0x80 << ((i%4) * 8) 174blks[nblk*16-2] = str.length * 8 175return blks 176} 177function str2binl0() 178{var nblk = ((64 + 8) >> 6) + 1 179var blks = new Array(nblk * 16) 180for(var i = 0; i < nblk * 16; i++) blks[i] = 0 181for(var i = 0; i < 64; i++) 182blks[i>>2] |= (0x0 & 0xFF) << ((i%4) * 8) 183blks[i>>2] |= 0x80 << ((i%4) * 8) 184blks[nblk*16-2] = 64 * 8 185return blks 186} 187/* 188* Convert a wide-character string to a sequence of 16-word blocks, stored as 189* an array, and append appropriate padding for MD4/5 calculation. 190*/ 191function strw2binl(str) 192{var nblk = ((str.length + 4) >> 5) + 1 193var blks = new Array(nblk * 16) 194for(var i = 0; i < nblk * 16; i++) blks[i] = 0 195for(var i = 0; i < str.length; i++) 196blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16) 197blks[i>>1] |= 0x80 << ((i%2) * 16) 198blks[nblk*16-2] = str.length * 16 199return blks 200} 201/* 202* External interface 203*/ 204function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) } 205function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) } 206function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) } 207function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) } 208/* Backward compatibility */ 209function calcMD5(str) 210{if (str.length==0) 211return binl2hex(coreMD5(str2binl0())) 212else 213return binl2hex(coreMD5( str2binl(str))) 214} 215