Skip to content

Commit 5e3fab4

Browse files
authored
Cleanup randomFill. NFC (#23260)
Under node we can rely on `randomFillSync` always being available since its been around since v9.0.0: https://nodejs.org/api/crypto.html#cryptorandomfillsyncbuffer-offset-size Also, remove the return value from this function since it always fills the entire buffer. https://caniuse.com/getrandomvalues
1 parent 5a8d9e5 commit 5e3fab4

35 files changed

+51
-70
lines changed

src/closure-externs/node-externs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,5 @@ path.isAbsolute;
137137
* @type {Object.<string,*>}
138138
*/
139139
path.posix;
140+
141+
crypto.randomFillSync;

src/library_fs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,8 @@ FS.staticInit();
14111411
var randomBuffer = new Uint8Array(1024), randomLeft = 0;
14121412
var randomByte = () => {
14131413
if (randomLeft === 0) {
1414-
randomLeft = randomFill(randomBuffer).byteLength;
1414+
randomFill(randomBuffer);
1415+
randomLeft = randomBuffer.byteLength;
14151416
}
14161417
return randomBuffer[--randomLeft];
14171418
};

src/library_wasi.js

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -561,53 +561,35 @@ var WasiLibrary = {
561561
// random.h
562562

563563
$initRandomFill: () => {
564-
if (typeof crypto == 'object' && typeof crypto['getRandomValues'] == 'function') {
565-
// for modern web browsers
564+
if (typeof crypto == 'object' && typeof crypto.getRandomValues == 'function') {
566565
#if SHARED_MEMORY
567566
// like with most Web APIs, we can't use Web Crypto API directly on shared memory,
568567
// so we need to create an intermediate buffer and copy it to the destination
569-
return (view) => (
570-
view.set(crypto.getRandomValues(new Uint8Array(view.byteLength))),
571-
// Return the original view to match modern native implementations.
572-
view
573-
);
568+
return (view) => view.set(crypto.getRandomValues(new Uint8Array(view.byteLength)));
574569
#else
575570
return (view) => crypto.getRandomValues(view);
576571
#endif
577-
} else
572+
}
573+
578574
#if ENVIRONMENT_MAY_BE_NODE
579575
if (ENVIRONMENT_IS_NODE) {
580-
// for nodejs with or without crypto support included
581-
try {
582-
var crypto_module = require('crypto');
583-
var randomFillSync = crypto_module['randomFillSync'];
584-
if (randomFillSync) {
585-
// nodejs with LTS crypto support
586-
return (view) => crypto_module['randomFillSync'](view);
587-
}
588-
// very old nodejs with the original crypto API
589-
return (view) => (
590-
view.set(crypto_module['randomBytes'](view.byteLength)),
591-
// Return the original view to match modern native implementations.
592-
view
593-
);
594-
} catch (e) {
595-
// nodejs doesn't have crypto support
596-
}
576+
return (view) => require('crypto').randomFillSync(view);
597577
}
598578
#endif // ENVIRONMENT_MAY_BE_NODE
599-
// we couldn't find a proper implementation, as Math.random() is not suitable for /dev/random, see emscripten-core/emscripten/pull/7096
579+
580+
// we couldn't find a proper implementation, as Math.random() is not
581+
// suitable for /dev/random, see emscripten-core/emscripten/pull/7096
600582
#if ASSERTIONS
601-
abort('no cryptographic support found for randomDevice. consider polyfilling it if you want to use something insecure like Math.random(), e.g. put this in a --pre-js: var crypto = { getRandomValues: (array) => { for (var i = 0; i < array.length; i++) array[i] = (Math.random()*256)|0 } };');
583+
abort('no cryptographic support found for random function. consider polyfilling it if you want to use something insecure like Math.random(), e.g. put this in a --pre-js: var crypto = { getRandomValues: (array) => { for (var i = 0; i < array.length; i++) array[i] = (Math.random()*256)|0 } };');
602584
#else
603-
abort('initRandomDevice');
585+
abort();
604586
#endif
605587
},
606588

607589
$randomFill__deps: ['$initRandomFill'],
608590
$randomFill: (view) => {
609591
// Lazily init on the first invocation.
610-
return (randomFill = initRandomFill())(view);
592+
(randomFill = initRandomFill())(view);
611593
},
612594

613595
random_get__proxy: 'none',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8405
1+
8365
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20468
1+
20373
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
128894
1+
128914
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8390
1+
8347
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20436
1+
20341
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
128343
1+
128363
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9412
1+
9369

0 commit comments

Comments
 (0)