Skip to content

Commit

Permalink
Modify random_get to use crypto.getRandomValues (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aandreba authored May 3, 2024
1 parent 74f6ce5 commit 7d86a13
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,9 +825,18 @@ export default class WASI {
},
sched_yield() {},
random_get(buf: number, buf_len: number) {
const buffer8 = new Uint8Array(self.inst.exports.memory.buffer);
for (let i = 0; i < buf_len; i++) {
buffer8[buf + i] = (Math.random() * 256) | 0;
const buffer8 = new Uint8Array(
self.inst.exports.memory.buffer,
).subarray(buf, buf + buf_len);

if ("crypto" in globalThis) {
for (let i = 0; i < buf_len; i += 65536) {
crypto.getRandomValues(buffer8.subarray(i, i + 65536));
}
} else {
for (let i = 0; i < buf_len; i++) {
buffer8[i] = (Math.random() * 256) | 0;
}
}
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down

0 comments on commit 7d86a13

Please sign in to comment.