1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/tizen/hashes/sha1.js Wed Jul 31 19:48:00 2013 +0200 1.3 @@ -0,0 +1,202 @@ 1.4 +/* 1.5 + * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined 1.6 + * in FIPS PUB 180-1 1.7 + * Version 2.1 Copyright Paul Johnston 2000 - 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 details. 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 hexsha1(s){return binb2hex(coresha1(str2binb(s),s.length * chrsz));} 1.26 +function b64sha1(s){return binb2b64(coresha1(str2binb(s),s.length * chrsz));} 1.27 +function strsha1(s){return binb2str(coresha1(str2binb(s),s.length * chrsz));} 1.28 +function hexhmacsha1(key, data){ return binb2hex(corehmacsha1(key, data));} 1.29 +function b64hmacsha1(key, data){ return binb2b64(corehmacsha1(key, data));} 1.30 +function strhmacsha1(key, data){ return binb2str(corehmacsha1(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 sha1vmtest() 1.36 +{ 1.37 + return hexsha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d"; 1.38 +} 1.39 + 1.40 +/* 1.41 + * Calculate the SHA-1 of an array of big-endian words, and a bit length 1.42 + */ 1.43 +function coresha1(x, len) 1.44 +{ 1.45 + /* append padding */ 1.46 + x[len >> 5] |= 0x80 << (24 - len % 32); 1.47 + x[((len + 64 >> 9) << 4) + 15] = len; 1.48 + 1.49 + var w = Array(80); 1.50 + var a = 1732584193; 1.51 + var b = -271733879; 1.52 + var c = -1732584194; 1.53 + var d = 271733878; 1.54 + var e = -1009589776; 1.55 + 1.56 + for(var i = 0; i < x.length; i += 16) 1.57 + { 1.58 + var olda = a; 1.59 + var oldb = b; 1.60 + var oldc = c; 1.61 + var oldd = d; 1.62 + var olde = e; 1.63 + 1.64 + for(var j = 0; j < 80; j++) 1.65 + { 1.66 + if(j < 16) w[j] = x[i + j]; 1.67 + else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); 1.68 + var t = safeadd(safeadd(rol(a, 5), sha1ft(j, b, c, d)), 1.69 + safeadd(safeadd(e, w[j]), sha1kt(j))); 1.70 + e = d; 1.71 + d = c; 1.72 + c = rol(b, 30); 1.73 + b = a; 1.74 + a = t; 1.75 + } 1.76 + 1.77 + a = safeadd(a, olda); 1.78 + b = safeadd(b, oldb); 1.79 + c = safeadd(c, oldc); 1.80 + d = safeadd(d, oldd); 1.81 + e = safeadd(e, olde); 1.82 + } 1.83 + return Array(a, b, c, d, e); 1.84 + 1.85 +} 1.86 + 1.87 +/* 1.88 + * Perform the appropriate triplet combination function for the current 1.89 + * iteration 1.90 + */ 1.91 +function sha1ft(t, b, c, d) 1.92 +{ 1.93 + if(t < 20) return (b & c) | ((~b) & d); 1.94 + if(t < 40) return b ^ c ^ d; 1.95 + if(t < 60) return (b & c) | (b & d) | (c & d); 1.96 + return b ^ c ^ d; 1.97 +} 1.98 + 1.99 +/* 1.100 + * Determine the appropriate additive constant for the current iteration 1.101 + */ 1.102 +function sha1kt(t) 1.103 +{ 1.104 + return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : 1.105 + (t < 60) ? -1894007588 : -899497514; 1.106 +} 1.107 + 1.108 +/* 1.109 + * Calculate the HMAC-SHA1 of a key and some data 1.110 + */ 1.111 +function corehmacsha1(key, data) 1.112 +{ 1.113 + var bkey = str2binb(key); 1.114 + if(bkey.length > 16) bkey = coresha1(bkey, key.length * chrsz); 1.115 + 1.116 + var ipad = Array(16), opad = Array(16); 1.117 + for(var i = 0; i < 16; i++) 1.118 + { 1.119 + ipad[i] = bkey[i] ^ 0x36363636; 1.120 + opad[i] = bkey[i] ^ 0x5C5C5C5C; 1.121 + } 1.122 + 1.123 + var hash = coresha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz); 1.124 + return coresha1(opad.concat(hash), 512 + 160); 1.125 +} 1.126 + 1.127 +/* 1.128 + * Add integers, wrapping at 2^32. This uses 16-bit operations internally 1.129 + * to work around bugs in some JS interpreters. 1.130 + */ 1.131 +function safeadd(x, y) 1.132 +{ 1.133 + var lsw = (x & 0xFFFF) + (y & 0xFFFF); 1.134 + var msw = (x >> 16) + (y >> 16) + (lsw >> 16); 1.135 + return (msw << 16) | (lsw & 0xFFFF); 1.136 +} 1.137 + 1.138 +/* 1.139 + * Bitwise rotate a 32-bit number to the left. 1.140 + */ 1.141 +function rol(num, cnt) 1.142 +{ 1.143 + return (num << cnt) | (num >>> (32 - cnt)); 1.144 +} 1.145 + 1.146 +/* 1.147 + * Convert an 8-bit or 16-bit string to an array of big-endian words 1.148 + * In 8-bit function, characters >255 have their hi-byte silently ignored. 1.149 + */ 1.150 +function str2binb(str) 1.151 +{ 1.152 + var bin = Array(); 1.153 + var mask = (1 << chrsz) - 1; 1.154 + for(var i = 0; i < str.length * chrsz; i += chrsz) 1.155 + bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32); 1.156 + return bin; 1.157 +} 1.158 + 1.159 +/* 1.160 + * Convert an array of big-endian words to a string 1.161 + */ 1.162 +function binb2str(bin) 1.163 +{ 1.164 + var str = ""; 1.165 + var mask = (1 << chrsz) - 1; 1.166 + for(var i = 0; i < bin.length * 32; i += chrsz) 1.167 + str += String.fromCharCode((bin[i>>5] >>> (24 - i%32)) & mask); 1.168 + return str; 1.169 +} 1.170 + 1.171 +/* 1.172 + * Convert an array of big-endian words to a hex string. 1.173 + */ 1.174 +function binb2hex(binarray) 1.175 +{ 1.176 + var hextab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; 1.177 + var str = ""; 1.178 + for(var i = 0; i < binarray.length * 4; i++) 1.179 + { 1.180 + str += hextab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + 1.181 + hextab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF); 1.182 + } 1.183 + return str; 1.184 +} 1.185 + 1.186 +/* 1.187 + * Convert an array of big-endian words to a base-64 string 1.188 + */ 1.189 +function binb2b64(binarray) 1.190 +{ 1.191 + var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 1.192 + var str = ""; 1.193 + for(var i = 0; i < binarray.length * 4; i += 3) 1.194 + { 1.195 + var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16) 1.196 + | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 ) 1.197 + | ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF); 1.198 + for(var j = 0; j < 4; j++) 1.199 + { 1.200 + if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; 1.201 + else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); 1.202 + } 1.203 + } 1.204 + return str; 1.205 +}