Skip to content

Commit 0b9b186

Browse files
committed
Merge pull request #45 from kyledrake/nodumbcrypto
Remove Math.random RNG as it is not cryptographically secure
2 parents fcf43d8 + b8695c4 commit 0b9b186

File tree

1 file changed

+6
-29
lines changed

1 file changed

+6
-29
lines changed

rng.js

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,10 @@
1-
// Original code adapted from Robert Kieffer.
2-
// details at https://github.com/broofa/node-uuid
3-
4-
51
(function() {
6-
var _global = this;
7-
8-
var mathRNG, whatwgRNG;
9-
10-
// NOTE: Math.random() does not guarantee "cryptographic quality"
11-
mathRNG = function(size) {
12-
var bytes = new Buffer(size);
13-
var r;
14-
15-
for (var i = 0, r; i < size; i++) {
16-
if ((i & 0x03) == 0) r = Math.random() * 0x100000000;
17-
bytes[i] = r >>> ((i & 0x03) << 3) & 0xff;
18-
}
19-
2+
module.exports = function(size) {
3+
var bytes = new Buffer(size); //in browserify, this is an extended Uint8Array
4+
/* This will not work in older browsers.
5+
* See https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
6+
*/
7+
crypto.getRandomValues(bytes);
208
return bytes;
219
}
22-
23-
if (_global.crypto && crypto.getRandomValues) {
24-
whatwgRNG = function(size) {
25-
var bytes = new Buffer(size); //in browserify, this is an extended Uint8Array
26-
crypto.getRandomValues(bytes);
27-
return bytes;
28-
}
29-
}
30-
31-
module.exports = whatwgRNG || mathRNG;
32-
3310
}())

0 commit comments

Comments
 (0)