build/pgo/js-input/crypto-sha1.html

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 <!DOCTYPE html>
michael@0 2 <head>
michael@0 3 <!--
michael@0 4 Copyright (C) 2007 Apple Inc. All rights reserved.
michael@0 5
michael@0 6 Redistribution and use in source and binary forms, with or without
michael@0 7 modification, are permitted provided that the following conditions
michael@0 8 are met:
michael@0 9 1. Redistributions of source code must retain the above copyright
michael@0 10 notice, this list of conditions and the following disclaimer.
michael@0 11 2. Redistributions in binary form must reproduce the above copyright
michael@0 12 notice, this list of conditions and the following disclaimer in the
michael@0 13 documentation and/or other materials provided with the distribution.
michael@0 14
michael@0 15 THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
michael@0 16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
michael@0 18 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
michael@0 19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
michael@0 23 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 26 -->
michael@0 27
michael@0 28 <title>SunSpider crypto-sha1</title>
michael@0 29
michael@0 30 </head>
michael@0 31
michael@0 32 <body>
michael@0 33 <h3>crypto-sha1</h3>
michael@0 34 <div id="console">
michael@0 35 </div>
michael@0 36
michael@0 37 <script>
michael@0 38
michael@0 39 var _sunSpiderStartDate = new Date();
michael@0 40
michael@0 41 /*
michael@0 42 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
michael@0 43 * in FIPS PUB 180-1
michael@0 44 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
michael@0 45 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
michael@0 46 * Distributed under the BSD License
michael@0 47 * See http://pajhome.org.uk/crypt/md5 for details.
michael@0 48 */
michael@0 49
michael@0 50 /*
michael@0 51 * Configurable variables. You may need to tweak these to be compatible with
michael@0 52 * the server-side, but the defaults work in most cases.
michael@0 53 */
michael@0 54 var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
michael@0 55 var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
michael@0 56 var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
michael@0 57
michael@0 58 /*
michael@0 59 * These are the functions you'll usually want to call
michael@0 60 * They take string arguments and return either hex or base-64 encoded strings
michael@0 61 */
michael@0 62 function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
michael@0 63 function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
michael@0 64 function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
michael@0 65 function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
michael@0 66 function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
michael@0 67 function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
michael@0 68
michael@0 69 /*
michael@0 70 * Perform a simple self-test to see if the VM is working
michael@0 71 */
michael@0 72 function sha1_vm_test()
michael@0 73 {
michael@0 74 return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
michael@0 75 }
michael@0 76
michael@0 77 /*
michael@0 78 * Calculate the SHA-1 of an array of big-endian words, and a bit length
michael@0 79 */
michael@0 80 function core_sha1(x, len)
michael@0 81 {
michael@0 82 /* append padding */
michael@0 83 x[len >> 5] |= 0x80 << (24 - len % 32);
michael@0 84 x[((len + 64 >> 9) << 4) + 15] = len;
michael@0 85
michael@0 86 var w = Array(80);
michael@0 87 var a = 1732584193;
michael@0 88 var b = -271733879;
michael@0 89 var c = -1732584194;
michael@0 90 var d = 271733878;
michael@0 91 var e = -1009589776;
michael@0 92
michael@0 93 for(var i = 0; i < x.length; i += 16)
michael@0 94 {
michael@0 95 var olda = a;
michael@0 96 var oldb = b;
michael@0 97 var oldc = c;
michael@0 98 var oldd = d;
michael@0 99 var olde = e;
michael@0 100
michael@0 101 for(var j = 0; j < 80; j++)
michael@0 102 {
michael@0 103 if(j < 16) w[j] = x[i + j];
michael@0 104 else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
michael@0 105 var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
michael@0 106 safe_add(safe_add(e, w[j]), sha1_kt(j)));
michael@0 107 e = d;
michael@0 108 d = c;
michael@0 109 c = rol(b, 30);
michael@0 110 b = a;
michael@0 111 a = t;
michael@0 112 }
michael@0 113
michael@0 114 a = safe_add(a, olda);
michael@0 115 b = safe_add(b, oldb);
michael@0 116 c = safe_add(c, oldc);
michael@0 117 d = safe_add(d, oldd);
michael@0 118 e = safe_add(e, olde);
michael@0 119 }
michael@0 120 return Array(a, b, c, d, e);
michael@0 121
michael@0 122 }
michael@0 123
michael@0 124 /*
michael@0 125 * Perform the appropriate triplet combination function for the current
michael@0 126 * iteration
michael@0 127 */
michael@0 128 function sha1_ft(t, b, c, d)
michael@0 129 {
michael@0 130 if(t < 20) return (b & c) | ((~b) & d);
michael@0 131 if(t < 40) return b ^ c ^ d;
michael@0 132 if(t < 60) return (b & c) | (b & d) | (c & d);
michael@0 133 return b ^ c ^ d;
michael@0 134 }
michael@0 135
michael@0 136 /*
michael@0 137 * Determine the appropriate additive constant for the current iteration
michael@0 138 */
michael@0 139 function sha1_kt(t)
michael@0 140 {
michael@0 141 return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
michael@0 142 (t < 60) ? -1894007588 : -899497514;
michael@0 143 }
michael@0 144
michael@0 145 /*
michael@0 146 * Calculate the HMAC-SHA1 of a key and some data
michael@0 147 */
michael@0 148 function core_hmac_sha1(key, data)
michael@0 149 {
michael@0 150 var bkey = str2binb(key);
michael@0 151 if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
michael@0 152
michael@0 153 var ipad = Array(16), opad = Array(16);
michael@0 154 for(var i = 0; i < 16; i++)
michael@0 155 {
michael@0 156 ipad[i] = bkey[i] ^ 0x36363636;
michael@0 157 opad[i] = bkey[i] ^ 0x5C5C5C5C;
michael@0 158 }
michael@0 159
michael@0 160 var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
michael@0 161 return core_sha1(opad.concat(hash), 512 + 160);
michael@0 162 }
michael@0 163
michael@0 164 /*
michael@0 165 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
michael@0 166 * to work around bugs in some JS interpreters.
michael@0 167 */
michael@0 168 function safe_add(x, y)
michael@0 169 {
michael@0 170 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
michael@0 171 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
michael@0 172 return (msw << 16) | (lsw & 0xFFFF);
michael@0 173 }
michael@0 174
michael@0 175 /*
michael@0 176 * Bitwise rotate a 32-bit number to the left.
michael@0 177 */
michael@0 178 function rol(num, cnt)
michael@0 179 {
michael@0 180 return (num << cnt) | (num >>> (32 - cnt));
michael@0 181 }
michael@0 182
michael@0 183 /*
michael@0 184 * Convert an 8-bit or 16-bit string to an array of big-endian words
michael@0 185 * In 8-bit function, characters >255 have their hi-byte silently ignored.
michael@0 186 */
michael@0 187 function str2binb(str)
michael@0 188 {
michael@0 189 var bin = Array();
michael@0 190 var mask = (1 << chrsz) - 1;
michael@0 191 for(var i = 0; i < str.length * chrsz; i += chrsz)
michael@0 192 bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
michael@0 193 return bin;
michael@0 194 }
michael@0 195
michael@0 196 /*
michael@0 197 * Convert an array of big-endian words to a string
michael@0 198 */
michael@0 199 function binb2str(bin)
michael@0 200 {
michael@0 201 var str = "";
michael@0 202 var mask = (1 << chrsz) - 1;
michael@0 203 for(var i = 0; i < bin.length * 32; i += chrsz)
michael@0 204 str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
michael@0 205 return str;
michael@0 206 }
michael@0 207
michael@0 208 /*
michael@0 209 * Convert an array of big-endian words to a hex string.
michael@0 210 */
michael@0 211 function binb2hex(binarray)
michael@0 212 {
michael@0 213 var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
michael@0 214 var str = "";
michael@0 215 for(var i = 0; i < binarray.length * 4; i++)
michael@0 216 {
michael@0 217 str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
michael@0 218 hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
michael@0 219 }
michael@0 220 return str;
michael@0 221 }
michael@0 222
michael@0 223 /*
michael@0 224 * Convert an array of big-endian words to a base-64 string
michael@0 225 */
michael@0 226 function binb2b64(binarray)
michael@0 227 {
michael@0 228 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
michael@0 229 var str = "";
michael@0 230 for(var i = 0; i < binarray.length * 4; i += 3)
michael@0 231 {
michael@0 232 var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16)
michael@0 233 | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
michael@0 234 | ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
michael@0 235 for(var j = 0; j < 4; j++)
michael@0 236 {
michael@0 237 if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
michael@0 238 else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
michael@0 239 }
michael@0 240 }
michael@0 241 return str;
michael@0 242 }
michael@0 243
michael@0 244
michael@0 245 var plainText = "Two households, both alike in dignity,\n\
michael@0 246 In fair Verona, where we lay our scene,\n\
michael@0 247 From ancient grudge break to new mutiny,\n\
michael@0 248 Where civil blood makes civil hands unclean.\n\
michael@0 249 From forth the fatal loins of these two foes\n\
michael@0 250 A pair of star-cross'd lovers take their life;\n\
michael@0 251 Whole misadventured piteous overthrows\n\
michael@0 252 Do with their death bury their parents' strife.\n\
michael@0 253 The fearful passage of their death-mark'd love,\n\
michael@0 254 And the continuance of their parents' rage,\n\
michael@0 255 Which, but their children's end, nought could remove,\n\
michael@0 256 Is now the two hours' traffic of our stage;\n\
michael@0 257 The which if you with patient ears attend,\n\
michael@0 258 What here shall miss, our toil shall strive to mend.";
michael@0 259
michael@0 260 for (var i = 0; i <4; i++) {
michael@0 261 plainText += plainText;
michael@0 262 }
michael@0 263
michael@0 264 var sha1Output = hex_sha1(plainText);
michael@0 265
michael@0 266
michael@0 267 var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
michael@0 268
michael@0 269 document.getElementById("console").innerHTML = _sunSpiderInterval;
michael@0 270 </script>
michael@0 271
michael@0 272
michael@0 273 </body>
michael@0 274 </html>

mercurial