Skip to content

Commit

Permalink
use typeof to check for crypto rather than global. Fixes #185
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Sep 8, 2017
1 parent dc02a76 commit ef8bcb6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
16 changes: 6 additions & 10 deletions lib/rng-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@
// browser this is a little complicated due to unknown quality of Math.random()
// and inconsistent support for the `crypto` API. We do the best we can via
// feature-detection
var rng;

var crypto = global.crypto || global.msCrypto; // for IE 11
if (crypto && crypto.getRandomValues) {
if (typeof(crypto) != 'undefined' && crypto.getRandomValues) {
// WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
rng = function whatwgRNG() {

module.exports = function whatwgRNG() {
crypto.getRandomValues(rnds8);
return rnds8;
};
}

if (!rng) {
} else {
// Math.random()-based (RNG)
//
// If all else fails, use Math.random(). It's fast, but is of unspecified
// quality.
var rnds = new Array(16);
rng = function() {

module.exports = function mathRNG() {
for (var i = 0, r; i < 16; i++) {
if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
Expand All @@ -29,5 +27,3 @@ if (!rng) {
return rnds;
};
}

module.exports = rng;
10 changes: 4 additions & 6 deletions lib/rng.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Unique ID creation requires a high quality random # generator. In node.js
// this is pretty straight-forward - we use the crypto API.

var rb = require('crypto').randomBytes;
var crypto = require('crypto');

function rng() {
return rb(16);
}

module.exports = rng;
module.exports = function nodeRNG() {
return crypto.randomBytes(16);
};
35 changes: 35 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,41 @@ function compare(name, ids) {
});
}

test('nodeRNG', function() {
var rng = require('../lib/rng');

var bytes = rng();
console.log(rng.toString());
assert.equal(bytes.length, 16);
bytes.forEach(function(v) {return typeof(v) == 'number'});
});

test('browser mathRNG', function() {
var rng = require('../lib/rng-browser');

assert.equal(rng.name, 'mathRNG');

var bytes = rng();
assert.equal(bytes.length, 16);
bytes.forEach(function(v) {return typeof(v) == 'number'});
});

test('browser cryptoRNG', function() {
global.crypto = {
getRandomValues: function(arr) {
return randomFillSync(arr);
}
};

var rng = require('../lib/rng-browser');

delete global.crypto;

var bytes = rng();
assert.equal(bytes.length, 16);
bytes.forEach(function(v) {return typeof(v) == 'number'});
});

test('sha1 node', function() {
var sha1 = require('../lib/sha1');

Expand Down

0 comments on commit ef8bcb6

Please sign in to comment.