Skip to content

Commit 5a8d9e5

Browse files
authored
Convert uuid_generate to use existing randomFill function. NFC (#23247)
1 parent b94d6e6 commit 5a8d9e5

25 files changed

+30
-57
lines changed

src/library_uuid.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,11 @@ addToLibrary({
2424
// Write a RFC4122 version 4 compliant UUID largely based on the method found in
2525
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
2626
// tweaked slightly in order to use the 'compact' UUID form used by libuuid.
27-
uuid_generate__deps: ['$writeArrayToMemory'],
27+
uuid_generate__deps: ['$writeArrayToMemory', '$randomFill'],
2828
uuid_generate: (out) => {
2929
// void uuid_generate(uuid_t out);
30-
var uuid = null;
31-
32-
if (ENVIRONMENT_IS_NODE) {
33-
#if ENVIRONMENT_MAY_BE_NODE
34-
// If Node.js try to use crypto.randomBytes
35-
try {
36-
var rb = require('crypto')['randomBytes'];
37-
uuid = rb(16);
38-
} catch(e) {}
39-
#endif // ENVIRONMENT_MAY_BE_NODE
40-
} else if (ENVIRONMENT_IS_WEB &&
41-
typeof window.crypto != 'undefined' &&
42-
typeof window.crypto.getRandomValues != 'undefined') {
43-
// If crypto.getRandomValues is available try to use it.
44-
uuid = new Uint8Array(16);
45-
window.crypto.getRandomValues(uuid);
46-
}
47-
48-
// Fall back to Math.random if a higher quality random number generator is not available.
49-
if (!uuid) {
50-
uuid = new Array(16);
51-
var d = new Date().getTime();
52-
for (var i = 0; i < 16; i++) {
53-
var r = ((d + Math.random() * 256) % 256)|0;
54-
d = (d / 256)|0;
55-
uuid[i] = r;
56-
}
57-
}
30+
var uuid = new Uint8Array(16);
31+
randomFill(uuid);
5832

5933
// Makes uuid compliant to RFC-4122
6034
uuid[6] = (uuid[6] & 0x0F) | 0x40; // uuid version

src/library_wasi.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,8 @@ var WasiLibrary = {
586586
return (view) => crypto_module['randomFillSync'](view);
587587
}
588588
// very old nodejs with the original crypto API
589-
var randomBytes = crypto_module['randomBytes'];
590589
return (view) => (
591-
view.set(randomBytes(view.byteLength)),
590+
view.set(crypto_module['randomBytes'](view.byteLength)),
592591
// Return the original view to match modern native implementations.
593592
view
594593
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8408
1+
8405
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20486
1+
20468
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8392
1+
8390
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20454
1+
20436
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9413
1+
9412
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24255
1+
24237
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8374
1+
8370
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20379
1+
20361

0 commit comments

Comments
 (0)