1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/tizen/hashes/md4.js Wed Jul 31 19:48:00 2013 +0200 1.3 @@ -0,0 +1,236 @@ 1.4 +/* 1.5 + * A JavaScript implementation of the RSA Data Security, Inc. MD4 Message 1.6 + * Digest Algorithm, as defined in RFC 1320. 1.7 + * Version 2.1 Copyright (C) Jerrad Pierce, 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 + */ 1.24 +function hexmd4(s){ return binl2hex(coremd4(str2binl(s), s.length * chrsz));} 1.25 +function b64md4(s){ return binl2b64(coremd4(str2binl(s), s.length * chrsz));} 1.26 +function strmd4(s){ return binl2str(coremd4(str2binl(s), s.length * chrsz));} 1.27 +function hexhmacmd4(key, data) { return binl2hex(corehmacmd4(key, data)); } 1.28 +function b64hmacmd4(key, data) { return binl2b64(corehmacmd4(key, data)); } 1.29 +function strhmacmd4(key, data) { return binl2str(corehmacmd4(key, data)); } 1.30 + 1.31 +/* 1.32 + * Perform a simple self-test to see if the VM is working 1.33 + */ 1.34 +function md4vmtest() 1.35 +{ 1.36 + return hexmd4("abc") == "a448017aaf21d8525fc10ae87aa6729d"; 1.37 +} 1.38 + 1.39 +/* 1.40 + * Calculate the MD4 of an array of little-endian words, and a bit length 1.41 + */ 1.42 +function coremd4(x, len) 1.43 +{ 1.44 + /* append padding */ 1.45 + x[len >> 5] |= 0x80 << (len % 32); 1.46 + x[(((len + 64) >>> 9) << 4) + 14] = len; 1.47 + 1.48 + var a = 1732584193; 1.49 + var b = -271733879; 1.50 + var c = -1732584194; 1.51 + var d = 271733878; 1.52 + 1.53 + for(var i = 0; i < x.length; i += 16) 1.54 + { 1.55 + var olda = a; 1.56 + var oldb = b; 1.57 + var oldc = c; 1.58 + var oldd = d; 1.59 + 1.60 + a = md4ff(a, b, c, d, x[i+ 0], 3 ); 1.61 + d = md4ff(d, a, b, c, x[i+ 1], 7 ); 1.62 + c = md4ff(c, d, a, b, x[i+ 2], 11); 1.63 + b = md4ff(b, c, d, a, x[i+ 3], 19); 1.64 + a = md4ff(a, b, c, d, x[i+ 4], 3 ); 1.65 + d = md4ff(d, a, b, c, x[i+ 5], 7 ); 1.66 + c = md4ff(c, d, a, b, x[i+ 6], 11); 1.67 + b = md4ff(b, c, d, a, x[i+ 7], 19); 1.68 + a = md4ff(a, b, c, d, x[i+ 8], 3 ); 1.69 + d = md4ff(d, a, b, c, x[i+ 9], 7 ); 1.70 + c = md4ff(c, d, a, b, x[i+10], 11); 1.71 + b = md4ff(b, c, d, a, x[i+11], 19); 1.72 + a = md4ff(a, b, c, d, x[i+12], 3 ); 1.73 + d = md4ff(d, a, b, c, x[i+13], 7 ); 1.74 + c = md4ff(c, d, a, b, x[i+14], 11); 1.75 + b = md4ff(b, c, d, a, x[i+15], 19); 1.76 + 1.77 + a = md4gg(a, b, c, d, x[i+ 0], 3 ); 1.78 + d = md4gg(d, a, b, c, x[i+ 4], 5 ); 1.79 + c = md4gg(c, d, a, b, x[i+ 8], 9 ); 1.80 + b = md4gg(b, c, d, a, x[i+12], 13); 1.81 + a = md4gg(a, b, c, d, x[i+ 1], 3 ); 1.82 + d = md4gg(d, a, b, c, x[i+ 5], 5 ); 1.83 + c = md4gg(c, d, a, b, x[i+ 9], 9 ); 1.84 + b = md4gg(b, c, d, a, x[i+13], 13); 1.85 + a = md4gg(a, b, c, d, x[i+ 2], 3 ); 1.86 + d = md4gg(d, a, b, c, x[i+ 6], 5 ); 1.87 + c = md4gg(c, d, a, b, x[i+10], 9 ); 1.88 + b = md4gg(b, c, d, a, x[i+14], 13); 1.89 + a = md4gg(a, b, c, d, x[i+ 3], 3 ); 1.90 + d = md4gg(d, a, b, c, x[i+ 7], 5 ); 1.91 + c = md4gg(c, d, a, b, x[i+11], 9 ); 1.92 + b = md4gg(b, c, d, a, x[i+15], 13); 1.93 + 1.94 + a = md4hh(a, b, c, d, x[i+ 0], 3 ); 1.95 + d = md4hh(d, a, b, c, x[i+ 8], 9 ); 1.96 + c = md4hh(c, d, a, b, x[i+ 4], 11); 1.97 + b = md4hh(b, c, d, a, x[i+12], 15); 1.98 + a = md4hh(a, b, c, d, x[i+ 2], 3 ); 1.99 + d = md4hh(d, a, b, c, x[i+10], 9 ); 1.100 + c = md4hh(c, d, a, b, x[i+ 6], 11); 1.101 + b = md4hh(b, c, d, a, x[i+14], 15); 1.102 + a = md4hh(a, b, c, d, x[i+ 1], 3 ); 1.103 + d = md4hh(d, a, b, c, x[i+ 9], 9 ); 1.104 + c = md4hh(c, d, a, b, x[i+ 5], 11); 1.105 + b = md4hh(b, c, d, a, x[i+13], 15); 1.106 + a = md4hh(a, b, c, d, x[i+ 3], 3 ); 1.107 + d = md4hh(d, a, b, c, x[i+11], 9 ); 1.108 + c = md4hh(c, d, a, b, x[i+ 7], 11); 1.109 + b = md4hh(b, c, d, a, x[i+15], 15); 1.110 + 1.111 + a = safeadd(a, olda); 1.112 + b = safeadd(b, oldb); 1.113 + c = safeadd(c, oldc); 1.114 + d = safeadd(d, oldd); 1.115 + 1.116 + } 1.117 + return Array(a, b, c, d); 1.118 + 1.119 +} 1.120 + 1.121 +/* 1.122 + * These functions implement the basic operation for each round of the 1.123 + * algorithm. 1.124 + */ 1.125 +function md4cmn(q, a, b, x, s, t) 1.126 +{ 1.127 + return safeadd(rol(safeadd(safeadd(a, q), safeadd(x, t)), s), b); 1.128 +} 1.129 +function md4ff(a, b, c, d, x, s) 1.130 +{ 1.131 + return md4cmn((b & c) | ((~b) & d), a, 0, x, s, 0); 1.132 +} 1.133 +function md4gg(a, b, c, d, x, s) 1.134 +{ 1.135 + return md4cmn((b & c) | (b & d) | (c & d), a, 0, x, s, 1518500249); 1.136 +} 1.137 +function md4hh(a, b, c, d, x, s) 1.138 +{ 1.139 + return md4cmn(b ^ c ^ d, a, 0, x, s, 1859775393); 1.140 +} 1.141 + 1.142 +/* 1.143 + * Calculate the HMAC-MD4, of a key and some data 1.144 + */ 1.145 +function corehmacmd4(key, data) 1.146 +{ 1.147 + var bkey = str2binl(key); 1.148 + if(bkey.length > 16) bkey = coremd4(bkey, key.length * chrsz); 1.149 + 1.150 + var ipad = Array(16), opad = Array(16); 1.151 + for(var i = 0; i < 16; i++) 1.152 + { 1.153 + ipad[i] = bkey[i] ^ 0x36363636; 1.154 + opad[i] = bkey[i] ^ 0x5C5C5C5C; 1.155 + } 1.156 + 1.157 + var hash = coremd4(ipad.concat(str2binl(data)), 512 + data.length * chrsz); 1.158 + return coremd4(opad.concat(hash), 512 + 128); 1.159 +} 1.160 + 1.161 +/* 1.162 + * Add integers, wrapping at 2^32. This uses 16-bit operations internally 1.163 + * to work around bugs in some JS interpreters. 1.164 + */ 1.165 +function safeadd(x, y) 1.166 +{ 1.167 + var lsw = (x & 0xFFFF) + (y & 0xFFFF); 1.168 + var msw = (x >> 16) + (y >> 16) + (lsw >> 16); 1.169 + return (msw << 16) | (lsw & 0xFFFF); 1.170 +} 1.171 + 1.172 +/* 1.173 + * Bitwise rotate a 32-bit number to the left. 1.174 + */ 1.175 +function rol(num, cnt) 1.176 +{ 1.177 + return (num << cnt) | (num >>> (32 - cnt)); 1.178 +} 1.179 + 1.180 +/* 1.181 + * Convert a string to an array of little-endian words 1.182 + * If chrsz is ASCII, characters >255 have their hi-byte silently ignored. 1.183 + */ 1.184 +function str2binl(str) 1.185 +{ 1.186 + var bin = Array(); 1.187 + var mask = (1 << chrsz) - 1; 1.188 + for(var i = 0; i < str.length * chrsz; i += chrsz) 1.189 + bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32); 1.190 + return bin; 1.191 +} 1.192 + 1.193 +/* 1.194 + * Convert an array of little-endian words to a string 1.195 + */ 1.196 +function binl2str(bin) 1.197 +{ 1.198 + var str = ""; 1.199 + var mask = (1 << chrsz) - 1; 1.200 + for(var i = 0; i < bin.length * 32; i += chrsz) 1.201 + str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask); 1.202 + return str; 1.203 +} 1.204 + 1.205 +/* 1.206 + * Convert an array of little-endian words to a hex string. 1.207 + */ 1.208 +function binl2hex(binarray) 1.209 +{ 1.210 + var hextab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; 1.211 + var str = ""; 1.212 + for(var i = 0; i < binarray.length * 4; i++) 1.213 + { 1.214 + str += hextab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + 1.215 + hextab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF); 1.216 + } 1.217 + return str; 1.218 +} 1.219 + 1.220 +/* 1.221 + * Convert an array of little-endian words to a base-64 string 1.222 + */ 1.223 +function binl2b64(binarray) 1.224 +{ 1.225 + var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 1.226 + var str = ""; 1.227 + for(var i = 0; i < binarray.length * 4; i += 3) 1.228 + { 1.229 + var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16) 1.230 + | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) 1.231 + | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF); 1.232 + for(var j = 0; j < 4; j++) 1.233 + { 1.234 + if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; 1.235 + else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); 1.236 + } 1.237 + } 1.238 + return str; 1.239 +}