Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throwing error when insecure rng is used #294

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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