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.

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

mercurial