1// Random number generator - requires a PRNG backend, e.g. prng4.js 2 3// For best results, put code like 4// <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'> 5// in your main HTML document. 6 7var rng_state; 8var rng_pool; 9var rng_pptr; 10 11// Mix in a 32-bit integer into the pool 12function rng_seed_int(x) { 13 rng_pool[rng_pptr++] ^= x & 255; 14 rng_pool[rng_pptr++] ^= (x >> 8) & 255; 15 rng_pool[rng_pptr++] ^= (x >> 16) & 255; 16 rng_pool[rng_pptr++] ^= (x >> 24) & 255; 17 if(rng_pptr >= rng_psize) rng_pptr -= rng_psize; 18} 19 20// Mix in the current time (w/milliseconds) into the pool 21function rng_seed_time() { 22 rng_seed_int(new Date().getTime()); 23} 24 25// Initialize the pool with junk if needed. 26if(rng_pool == null) { 27 rng_pool = new Array(); 28 rng_pptr = 0; 29 var t; 30 if(window.crypto && window.crypto.getRandomValues) { 31 // Use webcrypto if available 32 var ua = new Uint8Array(32); 33 window.crypto.getRandomValues(ua); 34 for(t = 0; t < 32; ++t) 35 rng_pool[rng_pptr++] = ua[t]; 36 } 37 if(navigator.appName == "Netscape" && navigator.appVersion < "5" && window.crypto) { 38 // Extract entropy (256 bits) from NS4 RNG if available 39 var z = window.crypto.random(32); 40 for(t = 0; t < z.length; ++t) 41 rng_pool[rng_pptr++] = z.charCodeAt(t) & 255; 42 } 43 while(rng_pptr < rng_psize) { // extract some randomness from Math.random() 44 t = Math.floor(65536 * Math.random()); 45 rng_pool[rng_pptr++] = t >>> 8; 46 rng_pool[rng_pptr++] = t & 255; 47 } 48 rng_pptr = 0; 49 rng_seed_time(); 50 //rng_seed_int(window.screenX); 51 //rng_seed_int(window.screenY); 52} 53 54function rng_get_byte() { 55 if(rng_state == null) { 56 rng_seed_time(); 57 rng_state = prng_newstate(); 58 rng_state.init(rng_pool); 59 for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr) 60 rng_pool[rng_pptr] = 0; 61 rng_pptr = 0; 62 //rng_pool = null; 63 } 64 // TODO: allow reseeding after first request 65 return rng_state.next(); 66} 67 68function rng_get_bytes(ba) { 69 var i; 70 for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte(); 71} 72 73function SecureRandom() {} 74 75SecureRandom.prototype.nextBytes = rng_get_bytes;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.