MCPcopy Create free account
hub / github.com/GoogleChrome/samples / initRandomFill

Function initRandomFill

mkbitmap/mkbitmap.js:1175–1202  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1173 }};
1174
1175 function initRandomFill() {
1176 if (typeof crypto == 'object' && typeof crypto['getRandomValues'] == 'function') {
1177 // for modern web browsers
1178 return (view) => crypto.getRandomValues(view);
1179 } else
1180 if (ENVIRONMENT_IS_NODE) {
1181 // for nodejs with or without crypto support included
1182 try {
1183 var crypto_module = require('crypto');
1184 var randomFillSync = crypto_module['randomFillSync'];
1185 if (randomFillSync) {
1186 // nodejs with LTS crypto support
1187 return (view) => crypto_module['randomFillSync'](view);
1188 }
1189 // very old nodejs with the original crypto API
1190 var randomBytes = crypto_module['randomBytes'];
1191 return (view) => (
1192 view.set(randomBytes(view.byteLength)),
1193 // Return the original view to match modern native implementations.
1194 view
1195 );
1196 } catch (e) {
1197 // nodejs doesn't have crypto support
1198 }
1199 }
1200 // we couldn't find a proper implementation, as Math.random() is not suitable for /dev/random, see emscripten-core/emscripten/pull/7096
1201 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: function(array) { for (var i = 0; i < array.length; i++) array[i] = (Math.random()*256)|0 } };");
1202 }
1203 function randomFill(view) {
1204 // Lazily init on the first invocation.
1205 return (randomFill = initRandomFill())(view);

Callers 1

randomFillFunction · 0.70

Calls 2

setMethod · 0.80
abortFunction · 0.70

Tested by

no test coverage detected