Skip to content

Commit 348b1ff

Browse files
committed
Generate UID without randombytes dependency
1 parent 79ac5da commit 348b1ff

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ See the accompanying LICENSE file for terms.
66

77
'use strict';
88

9-
var randomBytes = require('randombytes');
10-
119
// Generate an internal UID to make the regexp pattern harder to guess.
1210
var UID_LENGTH = 16;
1311
var UID = generateUID();
@@ -35,7 +33,20 @@ function escapeUnsafeChars(unsafeChar) {
3533
}
3634

3735
function generateUID() {
38-
var bytes = randomBytes(UID_LENGTH);
36+
var bytes;
37+
if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') {
38+
bytes = crypto.getRandomValues(new Uint8Array(UID_LENGTH));
39+
} else {
40+
try {
41+
var nodeCrypto = require('crypto');
42+
bytes = nodeCrypto.randomBytes(UID_LENGTH);
43+
} catch {
44+
// We'll throw an error later
45+
}
46+
}
47+
if (!bytes) {
48+
throw new Error('Secure random number generation is not supported by this platform.');
49+
}
3950
var result = '';
4051
for(var i=0; i<UID_LENGTH; ++i) {
4152
result += bytes[i].toString(16);

package-lock.json

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,5 @@
2626
"homepage": "https://github.com/yahoo/serialize-javascript",
2727
"devDependencies": {
2828
"benchmark": "^2.1.4"
29-
},
30-
"dependencies": {
31-
"randombytes": "^2.1.0"
3229
}
3330
}

0 commit comments

Comments
 (0)