Skip to content

Commit

Permalink
BREAKING CHANGE: Removed Math.random rng fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
uri committed Apr 17, 2019
1 parent 36a5f18 commit 798af6f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 23 deletions.
15 changes: 2 additions & 13 deletions lib/rng-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,7 @@ if (getRandomValues) {
return rnds8;
};
} 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);

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;
}

return rnds;
module.exports = function noSecureRNG() {
throw Error('uuid: No secure RNG available. See https://github.com/kelektiv/node-uuid/wiki#no-good-rng-error for more info.');
};
}
11 changes: 2 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,9 @@ test('nodeRNG', function() {
}
});

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

var bytes = rng();
assert.equal(bytes.length, 16);

for (var i = 0; i < bytes.length; i++) {
assert.equal(typeof(bytes[i]), 'number');
}
assert.throws(rng, Error)
});

test('cryptoRNG', function() {
Expand Down
2 changes: 1 addition & 1 deletion v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function v1(options, buf, offset) {
// specified. We do this lazily to minimize issues related to insufficient
// system entropy. See #189
if (node == null || clockseq == null) {
var seedBytes = rng();
var seedBytes = (options.rng || rng)();
if (node == null) {
// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
node = _nodeId = [
Expand Down

0 comments on commit 798af6f

Please sign in to comment.