Mercurial > boosterpack
diff logintets/md5.js @ 0:477258d09353 boosterpack
[svn r1] initial import
author | robert |
---|---|
date | Sun, 30 Aug 2009 02:19:26 -0400 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/logintets/md5.js Sun Aug 30 02:19:26 2009 -0400 1.3 @@ -0,0 +1,256 @@ 1.4 +/* 1.5 + * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message 1.6 + * Digest Algorithm, as defined in RFC 1321. 1.7 + * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. 1.8 + * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet 1.9 + * Distributed under the BSD License 1.10 + * See http://pajhome.org.uk/crypt/md5 for more info. 1.11 + */ 1.12 + 1.13 +/* 1.14 + * Configurable variables. You may need to tweak these to be compatible with 1.15 + * the server-side, but the defaults work in most cases. 1.16 + */ 1.17 +var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ 1.18 +var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ 1.19 +var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ 1.20 + 1.21 +/* 1.22 + * These are the functions you'll usually want to call 1.23 + * They take string arguments and return either hex or base-64 encoded strings 1.24 + */ 1.25 +function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));} 1.26 +function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));} 1.27 +function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));} 1.28 +function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); } 1.29 +function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); } 1.30 +function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); } 1.31 + 1.32 +/* 1.33 + * Perform a simple self-test to see if the VM is working 1.34 + */ 1.35 +function md5_vm_test() 1.36 +{ 1.37 + return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72"; 1.38 +} 1.39 + 1.40 +/* 1.41 + * Calculate the MD5 of an array of little-endian words, and a bit length 1.42 + */ 1.43 +function core_md5(x, len) 1.44 +{ 1.45 + /* append padding */ 1.46 + x[len >> 5] |= 0x80 << ((len) % 32); 1.47 + x[(((len + 64) >>> 9) << 4) + 14] = len; 1.48 + 1.49 + var a = 1732584193; 1.50 + var b = -271733879; 1.51 + var c = -1732584194; 1.52 + var d = 271733878; 1.53 + 1.54 + for(var i = 0; i < x.length; i += 16) 1.55 + { 1.56 + var olda = a; 1.57 + var oldb = b; 1.58 + var oldc = c; 1.59 + var oldd = d; 1.60 + 1.61 + a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936); 1.62 + d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586); 1.63 + c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819); 1.64 + b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330); 1.65 + a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897); 1.66 + d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426); 1.67 + c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341); 1.68 + b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983); 1.69 + a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416); 1.70 + d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417); 1.71 + c = md5_ff(c, d, a, b, x[i+10], 17, -42063); 1.72 + b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162); 1.73 + a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682); 1.74 + d = md5_ff(d, a, b, c, x[i+13], 12, -40341101); 1.75 + c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290); 1.76 + b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329); 1.77 + 1.78 + a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510); 1.79 + d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632); 1.80 + c = md5_gg(c, d, a, b, x[i+11], 14, 643717713); 1.81 + b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302); 1.82 + a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691); 1.83 + d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083); 1.84 + c = md5_gg(c, d, a, b, x[i+15], 14, -660478335); 1.85 + b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848); 1.86 + a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438); 1.87 + d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690); 1.88 + c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961); 1.89 + b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501); 1.90 + a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467); 1.91 + d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784); 1.92 + c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473); 1.93 + b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734); 1.94 + 1.95 + a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558); 1.96 + d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463); 1.97 + c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562); 1.98 + b = md5_hh(b, c, d, a, x[i+14], 23, -35309556); 1.99 + a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060); 1.100 + d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353); 1.101 + c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632); 1.102 + b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640); 1.103 + a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174); 1.104 + d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222); 1.105 + c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979); 1.106 + b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189); 1.107 + a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487); 1.108 + d = md5_hh(d, a, b, c, x[i+12], 11, -421815835); 1.109 + c = md5_hh(c, d, a, b, x[i+15], 16, 530742520); 1.110 + b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651); 1.111 + 1.112 + a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844); 1.113 + d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415); 1.114 + c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905); 1.115 + b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055); 1.116 + a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571); 1.117 + d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606); 1.118 + c = md5_ii(c, d, a, b, x[i+10], 15, -1051523); 1.119 + b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799); 1.120 + a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359); 1.121 + d = md5_ii(d, a, b, c, x[i+15], 10, -30611744); 1.122 + c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380); 1.123 + b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649); 1.124 + a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070); 1.125 + d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379); 1.126 + c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259); 1.127 + b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551); 1.128 + 1.129 + a = safe_add(a, olda); 1.130 + b = safe_add(b, oldb); 1.131 + c = safe_add(c, oldc); 1.132 + d = safe_add(d, oldd); 1.133 + } 1.134 + return Array(a, b, c, d); 1.135 + 1.136 +} 1.137 + 1.138 +/* 1.139 + * These functions implement the four basic operations the algorithm uses. 1.140 + */ 1.141 +function md5_cmn(q, a, b, x, s, t) 1.142 +{ 1.143 + return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b); 1.144 +} 1.145 +function md5_ff(a, b, c, d, x, s, t) 1.146 +{ 1.147 + return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t); 1.148 +} 1.149 +function md5_gg(a, b, c, d, x, s, t) 1.150 +{ 1.151 + return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t); 1.152 +} 1.153 +function md5_hh(a, b, c, d, x, s, t) 1.154 +{ 1.155 + return md5_cmn(b ^ c ^ d, a, b, x, s, t); 1.156 +} 1.157 +function md5_ii(a, b, c, d, x, s, t) 1.158 +{ 1.159 + return md5_cmn(c ^ (b | (~d)), a, b, x, s, t); 1.160 +} 1.161 + 1.162 +/* 1.163 + * Calculate the HMAC-MD5, of a key and some data 1.164 + */ 1.165 +function core_hmac_md5(key, data) 1.166 +{ 1.167 + var bkey = str2binl(key); 1.168 + if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz); 1.169 + 1.170 + var ipad = Array(16), opad = Array(16); 1.171 + for(var i = 0; i < 16; i++) 1.172 + { 1.173 + ipad[i] = bkey[i] ^ 0x36363636; 1.174 + opad[i] = bkey[i] ^ 0x5C5C5C5C; 1.175 + } 1.176 + 1.177 + var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz); 1.178 + return core_md5(opad.concat(hash), 512 + 128); 1.179 +} 1.180 + 1.181 +/* 1.182 + * Add integers, wrapping at 2^32. This uses 16-bit operations internally 1.183 + * to work around bugs in some JS interpreters. 1.184 + */ 1.185 +function safe_add(x, y) 1.186 +{ 1.187 + var lsw = (x & 0xFFFF) + (y & 0xFFFF); 1.188 + var msw = (x >> 16) + (y >> 16) + (lsw >> 16); 1.189 + return (msw << 16) | (lsw & 0xFFFF); 1.190 +} 1.191 + 1.192 +/* 1.193 + * Bitwise rotate a 32-bit number to the left. 1.194 + */ 1.195 +function bit_rol(num, cnt) 1.196 +{ 1.197 + return (num << cnt) | (num >>> (32 - cnt)); 1.198 +} 1.199 + 1.200 +/* 1.201 + * Convert a string to an array of little-endian words 1.202 + * If chrsz is ASCII, characters >255 have their hi-byte silently ignored. 1.203 + */ 1.204 +function str2binl(str) 1.205 +{ 1.206 + var bin = Array(); 1.207 + var mask = (1 << chrsz) - 1; 1.208 + for(var i = 0; i < str.length * chrsz; i += chrsz) 1.209 + bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32); 1.210 + return bin; 1.211 +} 1.212 + 1.213 +/* 1.214 + * Convert an array of little-endian words to a string 1.215 + */ 1.216 +function binl2str(bin) 1.217 +{ 1.218 + var str = ""; 1.219 + var mask = (1 << chrsz) - 1; 1.220 + for(var i = 0; i < bin.length * 32; i += chrsz) 1.221 + str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask); 1.222 + return str; 1.223 +} 1.224 + 1.225 +/* 1.226 + * Convert an array of little-endian words to a hex string. 1.227 + */ 1.228 +function binl2hex(binarray) 1.229 +{ 1.230 + var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; 1.231 + var str = ""; 1.232 + for(var i = 0; i < binarray.length * 4; i++) 1.233 + { 1.234 + str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + 1.235 + hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF); 1.236 + } 1.237 + return str; 1.238 +} 1.239 + 1.240 +/* 1.241 + * Convert an array of little-endian words to a base-64 string 1.242 + */ 1.243 +function binl2b64(binarray) 1.244 +{ 1.245 + var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 1.246 + var str = ""; 1.247 + for(var i = 0; i < binarray.length * 4; i += 3) 1.248 + { 1.249 + var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16) 1.250 + | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) 1.251 + | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF); 1.252 + for(var j = 0; j < 4; j++) 1.253 + { 1.254 + if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; 1.255 + else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); 1.256 + } 1.257 + } 1.258 + return str; 1.259 +}