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
optimacms-0.1.61 spec/dummy/node_modules/brorand/index.js
disco_app-0.18.0 test/dummy/node_modules/brorand/index.js
disco_app-0.18.2 test/dummy/node_modules/brorand/index.js
disco_app-0.16.1 test/dummy/node_modules/brorand/index.js
disco_app-0.15.2 test/dummy/node_modules/brorand/index.js
disco_app-0.18.4 test/dummy/node_modules/brorand/index.js
disco_app-0.18.1 test/dummy/node_modules/brorand/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/brorand/index.js
disco_app-0.14.0 test/dummy/node_modules/brorand/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/brorand/index.js
tang-0.2.1 spec/tang_app/node_modules/brorand/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/brorand/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/brorand/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/brorand/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/brorand/index.js
tang-0.2.0 spec/tang_app/node_modules/brorand/index.js
tang-0.1.0 spec/tang_app/node_modules/brorand/index.js
tang-0.0.9 spec/tang_app/node_modules/brorand/index.js
enju_library-0.3.8 spec/dummy/node_modules/brorand/index.js
ilog-0.4.1 node_modules/brorand/index.js