{"size": 2343, "ext": "js", "max_stars_repo_path": "src/lib/rng.js", "max_stars_repo_name": "mahmuaka/madelineTon.js", "max_stars_repo_stars_event_min_datetime": "2019-12-23T11:11:48.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-24T16:55:52.000Z", "max_issues_repo_path": "src/lib/rng.js", "max_issues_repo_name": "mahmuaka/madelineTon.js", "max_issues_repo_issues_event_min_datetime": "2020-01-09T18:18:13.000Z", "max_issues_repo_issues_event_max_datetime": "2020-01-14T10:45:53.000Z", "max_forks_repo_path": "src/lib/rng.js", "max_forks_repo_name": "mahmuaka/madelineTon.js", "max_forks_repo_forks_event_min_datetime": "2020-02-15T20:58:36.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-24T16:55:59.000Z"}
{"max_stars_count": 17, "max_issues_count": 1, "max_forks_count": 8, "avg_line_length": 27.5647058824, "max_line_length": 93, "alphanum_fraction": 0.6491677337}
// Random number generator - requires a PRNG backend, e.g. prng4.js

// For best results, put code like
// <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
// in your main HTML document.

import { prng_newstate } from './prng4'
import { windowObject } from '../crypto-sync/poly';
// Pool size must be a multiple of 4 and greater than 32.
// An array of bytes the size of the pool will be passed to init()
var rng_psize = 256;

var rng_state;
var rng_pool;
var rng_pptr;

// Mix in a 32-bit integer into the pool
function rng_seed_int(x) {
  rng_pool[rng_pptr++] ^= x & 255;
  rng_pool[rng_pptr++] ^= (x >> 8) & 255;
  rng_pool[rng_pptr++] ^= (x >> 16) & 255;
  rng_pool[rng_pptr++] ^= (x >> 24) & 255;
  if (rng_pptr >= rng_psize) rng_pptr -= rng_psize;
}

// Mix in the current time (w/milliseconds) into the pool
function rng_seed_time() {
  rng_seed_int(new Date().getTime());
}

// Initialize the pool with junk if needed.
if (rng_pool == null) {
  rng_pool = new Array();
  rng_pptr = 0;
  var t;
  if (windowObject.crypto && windowObject.crypto.getRandomValues) {
    // Use webcrypto if available
    var ua = new Uint8Array(32);
    windowObject.crypto.getRandomValues(ua);
    for (t = 0; t < 32; ++t)
      rng_pool[rng_pptr++] = ua[t];
  }
  if (navigator.appName == "Netscape" && navigator.appVersion < "5" && windowObject.crypto) {
    // Extract entropy (256 bits) from NS4 RNG if available
    var z = windowObject.crypto.random(32);
    for (t = 0; t < z.length; ++t)
      rng_pool[rng_pptr++] = z.charCodeAt(t) & 255;
  }
  while (rng_pptr < rng_psize) { // extract some randomness from Math.random()
    t = Math.floor(65536 * Math.random());
    rng_pool[rng_pptr++] = t >>> 8;
    rng_pool[rng_pptr++] = t & 255;
  }
  rng_pptr = 0;
  rng_seed_time();
}

function rng_get_byte() {
  if (rng_state == null) {
    rng_seed_time();
    rng_state = prng_newstate();
    rng_state.init(rng_pool);
    for (rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
      rng_pool[rng_pptr] = 0;
    rng_pptr = 0;
    //rng_pool = null;
  }
  // TODO: allow reseeding after first request
  return rng_state.next();
}

function rng_get_bytes(ba) {
  var i;
  for (i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
}


export {
  rng_get_byte,
  rng_get_bytes,
  rng_pool,
  rng_seed_int,
  rng_seed_time,
  rng_state
}