1/* 2 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message 3 * Digest Algorithm, as defined in RFC 1321. 4 * Copyright (C) Paul Johnston 1999 - 2000. 5 * Updated by Greg Holt 2000 - 2001. * See http://pajhome.org.uk/site/legal.html for details. 6 */ 7/* 8 * Convert a 32-bit number to a hex string with ls-byte first 9 */ 10var hex_chr = "0123456789abcdef"; 11function rhex(num) 12{ 13 var str = ""; 14 for(var j = 0; j <= 3; j++) 15 str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) + 16 hex_chr.charAt((num >> (j * 8)) & 0x0F); 17 return str; 18} 19 20 21/* 22 * Convert a string to a sequence of 16-word blocks, stored as an array. 23 * Append padding bits and the length, as described in the MD5 standard. 24 */ 25function str2blks_MD5(str) 26{ 27 var nblk = ((str.length + 8) >> 6) + 1; // number of 16-word blocks 28 var blks = new Array(nblk * 16); 29 for(var i = 0; i < nblk * 16; i++) blks[i] = 0; 30 for(var i = 0; i < str.length; i++) 31 blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8); 32 blks[i >> 2] |= 0x80 << ((i % 4) * 8); 33 blks[nblk * 16 - 2] = str.length * 8; 34 return blks; 35} 36 37 38/* 39 * Add integers, wrapping at 2^32. This uses 16-bit operations internally 40 * to work around bugs in some JS interpreters. 41 */ 42function safe_add(x, y) 43{ 44 var lsw = (x & 0xFFFF) + (y & 0xFFFF); 45 var msw = (x >> 16) + (y >> 16) + (lsw >> 16); 46 return (msw << 16) | (lsw & 0xFFFF); 47} 48 49 50/* 51 * Bitwise rotate a 32-bit number to the left 52 */ 53function rol(num, cnt) 54{ 55 return (num << cnt) | (num >>> (32 - cnt)); 56} 57 58 59/* 60 * These functions implement the basic operation for each round of the 61 * algorithm. 62 */ 63function cmn(q, a, b, x, s, t) 64{ 65 return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b); 66} 67function ff(a, b, c, d, x, s, t) 68{ 69 return cmn((b & c) | ((~b) & d), a, b, x, s, t); 70} 71function gg(a, b, c, d, x, s, t) 72{ 73 return cmn((b & d) | (c & (~d)), a, b, x, s, t); 74} 75function hh(a, b, c, d, x, s, t) 76{ 77 return cmn(b ^ c ^ d, a, b, x, s, t); 78} 79function ii(a, b, c, d, x, s, t) 80{ 81 return cmn(c ^ (b | (~d)), a, b, x, s, t); 82} 83 84 85/* 86 * Take a string and return the hex representation of its MD5. 87 */ 88function calcMD5(str) 89{ 90 var x = str2blks_MD5(str); 91 var a = 1732584193; 92 var b = -271733879; 93 var c = -1732584194; 94 var d = 271733878; 95 96 97 for(i = 0; i < x.length; i += 16) 98 { 99 var olda = a; 100 var oldb = b; 101 var oldc = c; 102 var oldd = d; 103 104 105 a = ff(a, b, c, d, x[i+ 0], 7 , -680876936); 106 d = ff(d, a, b, c, x[i+ 1], 12, -389564586); 107 c = ff(c, d, a, b, x[i+ 2], 17, 606105819); 108 b = ff(b, c, d, a, x[i+ 3], 22, -1044525330); 109 a = ff(a, b, c, d, x[i+ 4], 7 , -176418897); 110 d = ff(d, a, b, c, x[i+ 5], 12, 1200080426); 111 c = ff(c, d, a, b, x[i+ 6], 17, -1473231341); 112 b = ff(b, c, d, a, x[i+ 7], 22, -45705983); 113 a = ff(a, b, c, d, x[i+ 8], 7 , 1770035416); 114 d = ff(d, a, b, c, x[i+ 9], 12, -1958414417); 115 c = ff(c, d, a, b, x[i+10], 17, -42063); 116 b = ff(b, c, d, a, x[i+11], 22, -1990404162); 117 a = ff(a, b, c, d, x[i+12], 7 , 1804603682); 118 d = ff(d, a, b, c, x[i+13], 12, -40341101); 119 c = ff(c, d, a, b, x[i+14], 17, -1502002290); 120 b = ff(b, c, d, a, x[i+15], 22, 1236535329); 121 122 123 a = gg(a, b, c, d, x[i+ 1], 5 , -165796510); 124 d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632); 125 c = gg(c, d, a, b, x[i+11], 14, 643717713); 126 b = gg(b, c, d, a, x[i+ 0], 20, -373897302); 127 a = gg(a, b, c, d, x[i+ 5], 5 , -701558691); 128 d = gg(d, a, b, c, x[i+10], 9 , 38016083); 129 c = gg(c, d, a, b, x[i+15], 14, -660478335); 130 b = gg(b, c, d, a, x[i+ 4], 20, -405537848); 131 a = gg(a, b, c, d, x[i+ 9], 5 , 568446438); 132 d = gg(d, a, b, c, x[i+14], 9 , -1019803690); 133 c = gg(c, d, a, b, x[i+ 3], 14, -187363961); 134 b = gg(b, c, d, a, x[i+ 8], 20, 1163531501); 135 a = gg(a, b, c, d, x[i+13], 5 , -1444681467); 136 d = gg(d, a, b, c, x[i+ 2], 9 , -51403784); 137 c = gg(c, d, a, b, x[i+ 7], 14, 1735328473); 138 b = gg(b, c, d, a, x[i+12], 20, -1926607734); 139 140 141 a = hh(a, b, c, d, x[i+ 5], 4 , -378558); 142 d = hh(d, a, b, c, x[i+ 8], 11, -2022574463); 143 c = hh(c, d, a, b, x[i+11], 16, 1839030562); 144 b = hh(b, c, d, a, x[i+14], 23, -35309556); 145 a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060); 146 d = hh(d, a, b, c, x[i+ 4], 11, 1272893353); 147 c = hh(c, d, a, b, x[i+ 7], 16, -155497632); 148 b = hh(b, c, d, a, x[i+10], 23, -1094730640); 149 a = hh(a, b, c, d, x[i+13], 4 , 681279174); 150 d = hh(d, a, b, c, x[i+ 0], 11, -358537222); 151 c = hh(c, d, a, b, x[i+ 3], 16, -722521979); 152 b = hh(b, c, d, a, x[i+ 6], 23, 76029189); 153 a = hh(a, b, c, d, x[i+ 9], 4 , -640364487); 154 d = hh(d, a, b, c, x[i+12], 11, -421815835); 155 c = hh(c, d, a, b, x[i+15], 16, 530742520); 156 b = hh(b, c, d, a, x[i+ 2], 23, -995338651); 157 158 159 a = ii(a, b, c, d, x[i+ 0], 6 , -198630844); 160 d = ii(d, a, b, c, x[i+ 7], 10, 1126891415); 161 c = ii(c, d, a, b, x[i+14], 15, -1416354905); 162 b = ii(b, c, d, a, x[i+ 5], 21, -57434055); 163 a = ii(a, b, c, d, x[i+12], 6 , 1700485571); 164 d = ii(d, a, b, c, x[i+ 3], 10, -1894986606); 165 c = ii(c, d, a, b, x[i+10], 15, -1051523); 166 b = ii(b, c, d, a, x[i+ 1], 21, -2054922799); 167 a = ii(a, b, c, d, x[i+ 8], 6 , 1873313359); 168 d = ii(d, a, b, c, x[i+15], 10, -30611744); 169 c = ii(c, d, a, b, x[i+ 6], 15, -1560198380); 170 b = ii(b, c, d, a, x[i+13], 21, 1309151649); 171 a = ii(a, b, c, d, x[i+ 4], 6 , -145523070); 172 d = ii(d, a, b, c, x[i+11], 10, -1120210379); 173 c = ii(c, d, a, b, x[i+ 2], 15, 718787259); 174 b = ii(b, c, d, a, x[i+ 9], 21, -343485551); 175 176 177 a = safe_add(a, olda); 178 b = safe_add(b, oldb); 179 c = safe_add(c, oldc); 180 d = safe_add(d, oldd); 181 } 182 return rhex(a) + rhex(b) + rhex(c) + rhex(d); 183} 184function isIE() 185{ 186 var browser = new Object(); 187 browser.version = parseInt(navigator.appVersion); 188 browser.isNs = false; 189 browser.isIe = false; 190 if(navigator.appName.indexOf("Netscape") != -1) 191 browser.isNs = true; 192 else if(navigator.appName.indexOf("Microsoft") != -1) 193 browser.isIe = true; 194 if(browser.isNs) 195 return false; 196 else if (browser.isIe) 197 return true; 198} 199 200