Sat, 03 Jan 2015 20:18:00 +0100
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 | // seedrandom.js version 2.1. |
michael@0 | 2 | // Author: David Bau |
michael@0 | 3 | // Date: 2013 Mar 16 |
michael@0 | 4 | // |
michael@0 | 5 | // Defines a method Math.seedrandom() that, when called, substitutes |
michael@0 | 6 | // an explicitly seeded RC4-based algorithm for Math.random(). Also |
michael@0 | 7 | // supports automatic seeding from local or network sources of entropy. |
michael@0 | 8 | // |
michael@0 | 9 | // http://davidbau.com/encode/seedrandom.js |
michael@0 | 10 | // http://davidbau.com/encode/seedrandom-min.js |
michael@0 | 11 | // |
michael@0 | 12 | // Usage: |
michael@0 | 13 | // |
michael@0 | 14 | // <script src=http://davidbau.com/encode/seedrandom-min.js></script> |
michael@0 | 15 | // |
michael@0 | 16 | // Math.seedrandom('yay.'); Sets Math.random to a function that is |
michael@0 | 17 | // initialized using the given explicit seed. |
michael@0 | 18 | // |
michael@0 | 19 | // Math.seedrandom(); Sets Math.random to a function that is |
michael@0 | 20 | // seeded using the current time, dom state, |
michael@0 | 21 | // and other accumulated local entropy. |
michael@0 | 22 | // The generated seed string is returned. |
michael@0 | 23 | // |
michael@0 | 24 | // Math.seedrandom('yowza.', true); |
michael@0 | 25 | // Seeds using the given explicit seed mixed |
michael@0 | 26 | // together with accumulated entropy. |
michael@0 | 27 | // |
michael@0 | 28 | // <script src="https://jsonlib.appspot.com/urandom?callback=Math.seedrandom"> |
michael@0 | 29 | // </script> Seeds using urandom bits from a server. |
michael@0 | 30 | // |
michael@0 | 31 | // More advanced examples: |
michael@0 | 32 | // |
michael@0 | 33 | // Math.seedrandom("hello."); // Use "hello." as the seed. |
michael@0 | 34 | // document.write(Math.random()); // Always 0.9282578795792454 |
michael@0 | 35 | // document.write(Math.random()); // Always 0.3752569768646784 |
michael@0 | 36 | // var rng1 = Math.random; // Remember the current prng. |
michael@0 | 37 | // |
michael@0 | 38 | // var autoseed = Math.seedrandom(); // New prng with an automatic seed. |
michael@0 | 39 | // document.write(Math.random()); // Pretty much unpredictable x. |
michael@0 | 40 | // |
michael@0 | 41 | // Math.random = rng1; // Continue "hello." prng sequence. |
michael@0 | 42 | // document.write(Math.random()); // Always 0.7316977468919549 |
michael@0 | 43 | // |
michael@0 | 44 | // Math.seedrandom(autoseed); // Restart at the previous seed. |
michael@0 | 45 | // document.write(Math.random()); // Repeat the 'unpredictable' x. |
michael@0 | 46 | // |
michael@0 | 47 | // function reseed(event, count) { // Define a custom entropy collector. |
michael@0 | 48 | // var t = []; |
michael@0 | 49 | // function w(e) { |
michael@0 | 50 | // t.push([e.pageX, e.pageY, +new Date]); |
michael@0 | 51 | // if (t.length < count) { return; } |
michael@0 | 52 | // document.removeEventListener(event, w); |
michael@0 | 53 | // Math.seedrandom(t, true); // Mix in any previous entropy. |
michael@0 | 54 | // } |
michael@0 | 55 | // document.addEventListener(event, w); |
michael@0 | 56 | // } |
michael@0 | 57 | // reseed('mousemove', 100); // Reseed after 100 mouse moves. |
michael@0 | 58 | // |
michael@0 | 59 | // Version notes: |
michael@0 | 60 | // |
michael@0 | 61 | // The random number sequence is the same as version 1.0 for string seeds. |
michael@0 | 62 | // Version 2.0 changed the sequence for non-string seeds. |
michael@0 | 63 | // Version 2.1 speeds seeding and uses window.crypto to autoseed if present. |
michael@0 | 64 | // |
michael@0 | 65 | // The standard ARC4 key scheduler cycles short keys, which means that |
michael@0 | 66 | // seedrandom('ab') is equivalent to seedrandom('abab') and 'ababab'. |
michael@0 | 67 | // Therefore it is a good idea to add a terminator to avoid trivial |
michael@0 | 68 | // equivalences on short string seeds, e.g., Math.seedrandom(str + '\0'). |
michael@0 | 69 | // Starting with version 2.0, a terminator is added automatically for |
michael@0 | 70 | // non-string seeds, so seeding with the number 111 is the same as seeding |
michael@0 | 71 | // with '111\0'. |
michael@0 | 72 | // |
michael@0 | 73 | // When seedrandom() is called with zero args, it uses a seed |
michael@0 | 74 | // drawn from the browser crypto object if present. If there is no |
michael@0 | 75 | // crypto support, seedrandom() uses the current time, the native rng, |
michael@0 | 76 | // and a walk of several DOM objects to collect a few bits of entropy. |
michael@0 | 77 | // |
michael@0 | 78 | // Each time the one- or two-argument forms of seedrandom are called, |
michael@0 | 79 | // entropy from the passed seed is accumulated in a pool to help generate |
michael@0 | 80 | // future seeds for the zero- and two-argument forms of seedrandom. |
michael@0 | 81 | // |
michael@0 | 82 | // On speed - This javascript implementation of Math.random() is about |
michael@0 | 83 | // 3-10x slower than the built-in Math.random() because it is not native |
michael@0 | 84 | // code, but that is typically fast enough. Some details (timings on |
michael@0 | 85 | // Chrome 25 on a 2010 vintage macbook): |
michael@0 | 86 | // |
michael@0 | 87 | // seeded Math.random() - avg less than 0.0002 milliseconds per call |
michael@0 | 88 | // seedrandom('explicit.') - avg less than 0.2 milliseconds per call |
michael@0 | 89 | // seedrandom('explicit.', true) - avg less than 0.2 milliseconds per call |
michael@0 | 90 | // seedrandom() with crypto - avg less than 0.2 milliseconds per call |
michael@0 | 91 | // seedrandom() without crypto - avg about 12 milliseconds per call |
michael@0 | 92 | // |
michael@0 | 93 | // On a 2012 windows 7 1.5ghz i5 laptop, Chrome, Firefox 19, IE 10, and |
michael@0 | 94 | // Opera have similarly fast timings. Slowest numbers are on Opera, with |
michael@0 | 95 | // about 0.0005 milliseconds per seeded Math.random() and 15 milliseconds |
michael@0 | 96 | // for autoseeding. |
michael@0 | 97 | // |
michael@0 | 98 | // LICENSE (BSD): |
michael@0 | 99 | // |
michael@0 | 100 | // Copyright 2013 David Bau, all rights reserved. |
michael@0 | 101 | // |
michael@0 | 102 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 103 | // modification, are permitted provided that the following conditions are met: |
michael@0 | 104 | // |
michael@0 | 105 | // 1. Redistributions of source code must retain the above copyright |
michael@0 | 106 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 107 | // |
michael@0 | 108 | // 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 109 | // notice, this list of conditions and the following disclaimer in the |
michael@0 | 110 | // documentation and/or other materials provided with the distribution. |
michael@0 | 111 | // |
michael@0 | 112 | // 3. Neither the name of this module nor the names of its contributors may |
michael@0 | 113 | // be used to endorse or promote products derived from this software |
michael@0 | 114 | // without specific prior written permission. |
michael@0 | 115 | // |
michael@0 | 116 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 117 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 118 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 119 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 120 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 121 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 122 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 123 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 124 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 125 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 126 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 127 | // |
michael@0 | 128 | /** |
michael@0 | 129 | * All code is in an anonymous closure to keep the global namespace clean. |
michael@0 | 130 | */ |
michael@0 | 131 | (function ( |
michael@0 | 132 | global, pool, math, width, chunks, digits) { |
michael@0 | 133 | |
michael@0 | 134 | // |
michael@0 | 135 | // The following constants are related to IEEE 754 limits. |
michael@0 | 136 | // |
michael@0 | 137 | var startdenom = math.pow(width, chunks), |
michael@0 | 138 | significance = math.pow(2, digits), |
michael@0 | 139 | overflow = significance * 2, |
michael@0 | 140 | mask = width - 1; |
michael@0 | 141 | |
michael@0 | 142 | // |
michael@0 | 143 | // seedrandom() |
michael@0 | 144 | // This is the seedrandom function described above. |
michael@0 | 145 | // |
michael@0 | 146 | math['seedrandom'] = function(seed, use_entropy) { |
michael@0 | 147 | var key = []; |
michael@0 | 148 | |
michael@0 | 149 | // Flatten the seed string or build one from local entropy if needed. |
michael@0 | 150 | var shortseed = mixkey(flatten( |
michael@0 | 151 | use_entropy ? [seed, tostring(pool)] : |
michael@0 | 152 | 0 in arguments ? seed : autoseed(), 3), key); |
michael@0 | 153 | |
michael@0 | 154 | // Use the seed to initialize an ARC4 generator. |
michael@0 | 155 | var arc4 = new ARC4(key); |
michael@0 | 156 | |
michael@0 | 157 | // Mix the randomness into accumulated entropy. |
michael@0 | 158 | mixkey(tostring(arc4.S), pool); |
michael@0 | 159 | |
michael@0 | 160 | // Override Math.random |
michael@0 | 161 | |
michael@0 | 162 | // This function returns a random double in [0, 1) that contains |
michael@0 | 163 | // randomness in every bit of the mantissa of the IEEE 754 value. |
michael@0 | 164 | |
michael@0 | 165 | math['random'] = function() { // Closure to return a random double: |
michael@0 | 166 | var n = arc4.g(chunks), // Start with a numerator n < 2 ^ 48 |
michael@0 | 167 | d = startdenom, // and denominator d = 2 ^ 48. |
michael@0 | 168 | x = 0; // and no 'extra last byte'. |
michael@0 | 169 | while (n < significance) { // Fill up all significant digits by |
michael@0 | 170 | n = (n + x) * width; // shifting numerator and |
michael@0 | 171 | d *= width; // denominator and generating a |
michael@0 | 172 | x = arc4.g(1); // new least-significant-byte. |
michael@0 | 173 | } |
michael@0 | 174 | while (n >= overflow) { // To avoid rounding up, before adding |
michael@0 | 175 | n /= 2; // last byte, shift everything |
michael@0 | 176 | d /= 2; // right using integer math until |
michael@0 | 177 | x >>>= 1; // we have exactly the desired bits. |
michael@0 | 178 | } |
michael@0 | 179 | return (n + x) / d; // Form the number within [0, 1). |
michael@0 | 180 | }; |
michael@0 | 181 | |
michael@0 | 182 | // Return the seed that was used |
michael@0 | 183 | return shortseed; |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | // |
michael@0 | 187 | // ARC4 |
michael@0 | 188 | // |
michael@0 | 189 | // An ARC4 implementation. The constructor takes a key in the form of |
michael@0 | 190 | // an array of at most (width) integers that should be 0 <= x < (width). |
michael@0 | 191 | // |
michael@0 | 192 | // The g(count) method returns a pseudorandom integer that concatenates |
michael@0 | 193 | // the next (count) outputs from ARC4. Its return value is a number x |
michael@0 | 194 | // that is in the range 0 <= x < (width ^ count). |
michael@0 | 195 | // |
michael@0 | 196 | /** @constructor */ |
michael@0 | 197 | function ARC4(key) { |
michael@0 | 198 | var t, keylen = key.length, |
michael@0 | 199 | me = this, i = 0, j = me.i = me.j = 0, s = me.S = []; |
michael@0 | 200 | |
michael@0 | 201 | // The empty key [] is treated as [0]. |
michael@0 | 202 | if (!keylen) { key = [keylen++]; } |
michael@0 | 203 | |
michael@0 | 204 | // Set up S using the standard key scheduling algorithm. |
michael@0 | 205 | while (i < width) { |
michael@0 | 206 | s[i] = i++; |
michael@0 | 207 | } |
michael@0 | 208 | for (i = 0; i < width; i++) { |
michael@0 | 209 | s[i] = s[j = mask & (j + key[i % keylen] + (t = s[i]))]; |
michael@0 | 210 | s[j] = t; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | // The "g" method returns the next (count) outputs as one number. |
michael@0 | 214 | (me.g = function(count) { |
michael@0 | 215 | // Using instance members instead of closure state nearly doubles speed. |
michael@0 | 216 | var t, r = 0, |
michael@0 | 217 | i = me.i, j = me.j, s = me.S; |
michael@0 | 218 | while (count--) { |
michael@0 | 219 | t = s[i = mask & (i + 1)]; |
michael@0 | 220 | r = r * width + s[mask & ((s[i] = s[j = mask & (j + t)]) + (s[j] = t))]; |
michael@0 | 221 | } |
michael@0 | 222 | me.i = i; me.j = j; |
michael@0 | 223 | return r; |
michael@0 | 224 | // For robust unpredictability discard an initial batch of values. |
michael@0 | 225 | // See http://www.rsa.com/rsalabs/node.asp?id=2009 |
michael@0 | 226 | })(width); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | // |
michael@0 | 230 | // flatten() |
michael@0 | 231 | // Converts an object tree to nested arrays of strings. |
michael@0 | 232 | // |
michael@0 | 233 | function flatten(obj, depth) { |
michael@0 | 234 | var result = [], typ = (typeof obj)[0], prop; |
michael@0 | 235 | if (depth && typ == 'o') { |
michael@0 | 236 | for (prop in obj) { |
michael@0 | 237 | if (obj.hasOwnProperty(prop)) { |
michael@0 | 238 | try { result.push(flatten(obj[prop], depth - 1)); } catch (e) {} |
michael@0 | 239 | } |
michael@0 | 240 | } |
michael@0 | 241 | } |
michael@0 | 242 | return (result.length ? result : typ == 's' ? obj : obj + '\0'); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | // |
michael@0 | 246 | // mixkey() |
michael@0 | 247 | // Mixes a string seed into a key that is an array of integers, and |
michael@0 | 248 | // returns a shortened string seed that is equivalent to the result key. |
michael@0 | 249 | // |
michael@0 | 250 | function mixkey(seed, key) { |
michael@0 | 251 | var stringseed = seed + '', smear, j = 0; |
michael@0 | 252 | while (j < stringseed.length) { |
michael@0 | 253 | key[mask & j] = |
michael@0 | 254 | mask & ((smear ^= key[mask & j] * 19) + stringseed.charCodeAt(j++)); |
michael@0 | 255 | } |
michael@0 | 256 | return tostring(key); |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | // |
michael@0 | 260 | // autoseed() |
michael@0 | 261 | // Returns an object for autoseeding, using window.crypto if available. |
michael@0 | 262 | // |
michael@0 | 263 | /** @param {Uint8Array=} seed */ |
michael@0 | 264 | function autoseed(seed) { |
michael@0 | 265 | try { |
michael@0 | 266 | global.crypto.getRandomValues(seed = new Uint8Array(width)); |
michael@0 | 267 | return tostring(seed); |
michael@0 | 268 | } catch (e) { |
michael@0 | 269 | return [+new Date, global.document, global.history, |
michael@0 | 270 | global.navigator, global.screen, tostring(pool)]; |
michael@0 | 271 | } |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | // |
michael@0 | 275 | // tostring() |
michael@0 | 276 | // Converts an array of charcodes to a string |
michael@0 | 277 | // |
michael@0 | 278 | function tostring(a) { |
michael@0 | 279 | return String.fromCharCode.apply(0, a); |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | // |
michael@0 | 283 | // When seedrandom.js is loaded, we immediately mix a few bits |
michael@0 | 284 | // from the built-in RNG into the entropy pool. Because we do |
michael@0 | 285 | // not want to intefere with determinstic PRNG state later, |
michael@0 | 286 | // seedrandom will not call math.random on its own again after |
michael@0 | 287 | // initialization. |
michael@0 | 288 | // |
michael@0 | 289 | mixkey(math.random(), pool); |
michael@0 | 290 | |
michael@0 | 291 | // End anonymous scope, and pass initial values. |
michael@0 | 292 | })( |
michael@0 | 293 | this, // global window object |
michael@0 | 294 | [], // pool: entropy pool starts empty |
michael@0 | 295 | Math, // math: package containing random, pow, and seedrandom |
michael@0 | 296 | 256, // width: each RC4 output is 0 <= x < 256 |
michael@0 | 297 | 6, // chunks: at least six RC4 outputs for each double |
michael@0 | 298 | 52 // digits: there are 52 significant digits in a double |
michael@0 | 299 | ); |