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
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
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8406
1+
8404
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20510
1+
20492
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9417
1+
9416
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-
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-
3651
1+
3646
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7910
1+
7892
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7704
1+
7699
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18980
1+
18962
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2882
1+
2880
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6198
1+
6180

test/test_other.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15243,10 +15243,10 @@ def test_uuid(self):
1524315243

1524415244
js_out = read_file('test_uuid.js')
1524515245

15246-
# Check that test.js compiled with --closure 1 contains ").randomBytes" and
15247-
# "window.crypto.getRandomValues"
15248-
self.assertContained(").randomBytes", js_out)
15249-
self.assertContained("window.crypto.getRandomValues", js_out)
15246+
# Check that test.js compiled with --closure 1 contains ".randomBytes(" and
15247+
# ".getRandomValues("
15248+
self.assertContained(".randomBytes(", js_out)
15249+
self.assertContained(".getRandomValues(", js_out)
1525015250

1525115251
def test_wasm64_no_asan(self):
1525215252
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sMEMORY64', '-fsanitize=address'])

0 commit comments

Comments
 (0)