Skip to content

Commit

Permalink
Fixed problems with randombytes
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzc0re committed Oct 27, 2023
1 parent 8190683 commit bb255a5
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/c/utils/random_bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,32 @@ uint32_t
randombytes_random(void)
{
return EM_ASM_INT_V({
const crypto = globalThis.crypto;
const buf = new Uint32Array(1);
crypto.getRandomValues(buf);
return buf[0] >>> 0;
if (Module.getRandomValue === undefined) {
try {
var window_ = 'object' === typeof window ? window : self;
var crypto_ = typeof window_.crypto !== 'undefined' ? window_.crypto : window_.msCrypto;
var randomValuesStandard = function() {
var buf = new Uint32Array(1);
crypto_.getRandomValues(buf);
return buf[0] >>> 0;
};
randomValuesStandard();
Module.getRandomValue = randomValuesStandard;
} catch (e) {
try {
var crypto = require('crypto');
var randomValueNodeJS = function() {
var buf = crypto['randomBytes'](4);
return (buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]) >>> 0;
};
randomValueNodeJS();
Module.getRandomValue = randomValueNodeJS;
} catch (e) {
throw 'No secure random number generator found';
}
}
}
return Module.getRandomValue();
});
}

Expand Down

0 comments on commit bb255a5

Please sign in to comment.