1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/devtools/jint/sunspider/crypto-sha1.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,242 @@ 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.1a 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 hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));} 1.26 +function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));} 1.27 +function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));} 1.28 +function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));} 1.29 +function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));} 1.30 +function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(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 sha1_vm_test() 1.36 +{ 1.37 + return hex_sha1("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 core_sha1(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 + /* BEGIN LOOP */ 1.57 + for(var i = 0; i < x.length; i += 16) 1.58 + { 1.59 + var olda = a; 1.60 + var oldb = b; 1.61 + var oldc = c; 1.62 + var oldd = d; 1.63 + var olde = e; 1.64 + 1.65 + /* BEGIN LOOP */ 1.66 + for(var j = 0; j < 80; j++) 1.67 + { 1.68 + if(j < 16) w[j] = x[i + j]; 1.69 + else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); 1.70 + var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), 1.71 + safe_add(safe_add(e, w[j]), sha1_kt(j))); 1.72 + e = d; 1.73 + d = c; 1.74 + c = rol(b, 30); 1.75 + b = a; 1.76 + a = t; 1.77 + } 1.78 + /* END LOOP */ 1.79 + 1.80 + a = safe_add(a, olda); 1.81 + b = safe_add(b, oldb); 1.82 + c = safe_add(c, oldc); 1.83 + d = safe_add(d, oldd); 1.84 + e = safe_add(e, olde); 1.85 + } 1.86 + /* END LOOP */ 1.87 + return Array(a, b, c, d, e); 1.88 + 1.89 +} 1.90 + 1.91 +/* 1.92 + * Perform the appropriate triplet combination function for the current 1.93 + * iteration 1.94 + */ 1.95 +function sha1_ft(t, b, c, d) 1.96 +{ 1.97 + if(t < 20) return (b & c) | ((~b) & d); 1.98 + if(t < 40) return b ^ c ^ d; 1.99 + if(t < 60) return (b & c) | (b & d) | (c & d); 1.100 + return b ^ c ^ d; 1.101 +} 1.102 + 1.103 +/* 1.104 + * Determine the appropriate additive constant for the current iteration 1.105 + */ 1.106 +function sha1_kt(t) 1.107 +{ 1.108 + return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : 1.109 + (t < 60) ? -1894007588 : -899497514; 1.110 +} 1.111 + 1.112 +/* 1.113 + * Calculate the HMAC-SHA1 of a key and some data 1.114 + */ 1.115 +function core_hmac_sha1(key, data) 1.116 +{ 1.117 + var bkey = str2binb(key); 1.118 + if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz); 1.119 + 1.120 + var ipad = Array(16), opad = Array(16); 1.121 + /* BEGIN LOOP */ 1.122 + for(var i = 0; i < 16; i++) 1.123 + { 1.124 + ipad[i] = bkey[i] ^ 0x36363636; 1.125 + opad[i] = bkey[i] ^ 0x5C5C5C5C; 1.126 + } 1.127 + /* END LOOP */ 1.128 + 1.129 + var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz); 1.130 + return core_sha1(opad.concat(hash), 512 + 160); 1.131 +} 1.132 + 1.133 +/* 1.134 + * Add integers, wrapping at 2^32. This uses 16-bit operations internally 1.135 + * to work around bugs in some JS interpreters. 1.136 + */ 1.137 +function safe_add(x, y) 1.138 +{ 1.139 + var lsw = (x & 0xFFFF) + (y & 0xFFFF); 1.140 + var msw = (x >> 16) + (y >> 16) + (lsw >> 16); 1.141 + return (msw << 16) | (lsw & 0xFFFF); 1.142 +} 1.143 + 1.144 +/* 1.145 + * Bitwise rotate a 32-bit number to the left. 1.146 + */ 1.147 +function rol(num, cnt) 1.148 +{ 1.149 + return (num << cnt) | (num >>> (32 - cnt)); 1.150 +} 1.151 + 1.152 +/* 1.153 + * Convert an 8-bit or 16-bit string to an array of big-endian words 1.154 + * In 8-bit function, characters >255 have their hi-byte silently ignored. 1.155 + */ 1.156 +function str2binb(str) 1.157 +{ 1.158 + var bin = Array(); 1.159 + var mask = (1 << chrsz) - 1; 1.160 + /* BEGIN LOOP */ 1.161 + for(var i = 0; i < str.length * chrsz; i += chrsz) 1.162 + bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32); 1.163 + /* END LOOP */ 1.164 + return bin; 1.165 +} 1.166 + 1.167 +/* 1.168 + * Convert an array of big-endian words to a string 1.169 + */ 1.170 +function binb2str(bin) 1.171 +{ 1.172 + var str = ""; 1.173 + var mask = (1 << chrsz) - 1; 1.174 + /* BEGIN LOOP */ 1.175 + for(var i = 0; i < bin.length * 32; i += chrsz) 1.176 + str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask); 1.177 + /* END LOOP */ 1.178 + return str; 1.179 +} 1.180 + 1.181 +/* 1.182 + * Convert an array of big-endian words to a hex string. 1.183 + */ 1.184 +function binb2hex(binarray) 1.185 +{ 1.186 + var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; 1.187 + var str = ""; 1.188 + /* BEGIN LOOP */ 1.189 + for(var i = 0; i < binarray.length * 4; i++) 1.190 + { 1.191 + str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + 1.192 + hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF); 1.193 + } 1.194 + /* END LOOP */ 1.195 + return str; 1.196 +} 1.197 + 1.198 +/* 1.199 + * Convert an array of big-endian words to a base-64 string 1.200 + */ 1.201 +function binb2b64(binarray) 1.202 +{ 1.203 + var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 1.204 + var str = ""; 1.205 + /* BEGIN LOOP */ 1.206 + for(var i = 0; i < binarray.length * 4; i += 3) 1.207 + { 1.208 + var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16) 1.209 + | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 ) 1.210 + | ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF); 1.211 + /* BEGIN LOOP */ 1.212 + for(var j = 0; j < 4; j++) 1.213 + { 1.214 + if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; 1.215 + else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); 1.216 + } 1.217 + /* END LOOP */ 1.218 + } 1.219 + /* END LOOP */ 1.220 + return str; 1.221 +} 1.222 + 1.223 + 1.224 +var plainText = "Two households, both alike in dignity,\n\ 1.225 +In fair Verona, where we lay our scene,\n\ 1.226 +From ancient grudge break to new mutiny,\n\ 1.227 +Where civil blood makes civil hands unclean.\n\ 1.228 +From forth the fatal loins of these two foes\n\ 1.229 +A pair of star-cross'd lovers take their life;\n\ 1.230 +Whole misadventured piteous overthrows\n\ 1.231 +Do with their death bury their parents' strife.\n\ 1.232 +The fearful passage of their death-mark'd love,\n\ 1.233 +And the continuance of their parents' rage,\n\ 1.234 +Which, but their children's end, nought could remove,\n\ 1.235 +Is now the two hours' traffic of our stage;\n\ 1.236 +The which if you with patient ears attend,\n\ 1.237 +What here shall miss, our toil shall strive to mend."; 1.238 + 1.239 + /* BEGIN LOOP */ 1.240 +for (var i = 0; i <4; i++) { 1.241 + plainText += plainText; 1.242 +} 1.243 + /* END LOOP */ 1.244 + 1.245 +var sha1Output = hex_sha1(plainText);