Sha256: 3deb9a82f06f1add80673ea960f409fb3d456bf9e74192ecfde453f0b56d9ee4

Contents?: true

Size: 1.47 KB

Versions: 36

Compression:

Stored size: 1.47 KB

Contents

var r;

module.exports = function rand(len) {
  if (!r)
    r = new Rand(null);

  return r.generate(len);
};

function Rand(rand) {
  this.rand = rand;
}
module.exports.Rand = Rand;

Rand.prototype.generate = function generate(len) {
  return this._rand(len);
};

// Emulate crypto API using randy
Rand.prototype._rand = function _rand(n) {
  if (this.rand.getBytes)
    return this.rand.getBytes(n);

  var res = new Uint8Array(n);
  for (var i = 0; i < res.length; i++)
    res[i] = this.rand.getByte();
  return res;
};

if (typeof self === 'object') {
  if (self.crypto && self.crypto.getRandomValues) {
    // Modern browsers
    Rand.prototype._rand = function _rand(n) {
      var arr = new Uint8Array(n);
      self.crypto.getRandomValues(arr);
      return arr;
    };
  } else if (self.msCrypto && self.msCrypto.getRandomValues) {
    // IE
    Rand.prototype._rand = function _rand(n) {
      var arr = new Uint8Array(n);
      self.msCrypto.getRandomValues(arr);
      return arr;
    };

  // Safari's WebWorkers do not have `crypto`
  } else if (typeof window === 'object') {
    // Old junk
    Rand.prototype._rand = function() {
      throw new Error('Not implemented yet');
    };
  }
} else {
  // Node.js or Web worker with no crypto support
  try {
    var crypto = require('crypto');
    if (typeof crypto.randomBytes !== 'function')
      throw new Error('Not supported');

    Rand.prototype._rand = function _rand(n) {
      return crypto.randomBytes(n);
    };
  } catch (e) {
  }
}

Version data entries

36 entries across 35 versions & 13 rubygems

Version Path
ilog-0.4.0 node_modules/brorand/index.js
ilog-0.3.3 node_modules/brorand/index.js
jester-data-8.0.0 node_modules/brorand/index.js
ezii-os-5.2.1 node_modules/brorand/index.js
ezii-os-2.0.1 node_modules/brorand/index.js
ezii-os-1.1.0 node_modules/brorand/index.js
ezii-os-1.0.0 node_modules/brorand/index.js
ezii-os-0.0.0.1.0 node_modules/brorand/index.js
ezii-os-0.0.0.0.1 node_modules/brorand/index.js
optimacms-0.4.3 spec/dummy/node_modules/brorand/index.js
optimacms-0.4.2 spec/dummy/node_modules/brorand/index.js
locomotivecms-4.0.0.alpha1 app/javascript/node_modules/brorand/index.js
locomotivecms-3.4.0 app/javascript/node_modules/brorand/index.js
cortex-0.1.3 spec/dummy/node_modules/brorand/index.js
dragonfly_puppeteer-0.1.0 node_modules/brorand/index.js
lanes-0.8.0 node_modules/brorand/index.js