Skip to content

Commit

Permalink
switch random_get to use crypto.getRandomValues
Browse files Browse the repository at this point in the history
  • Loading branch information
Aandreba committed May 1, 2024
1 parent 74f6ce5 commit 6729ef8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,9 +825,17 @@ 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);

let i = 0;
for (; i < buf_len; ) {
const next_i = i + 65_536;
globalThis.crypto.getRandomValues(
buffer8.subarray(i, next_i > buf_len ? buf_len : next_i),
);
i = next_i;
}
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down

0 comments on commit 6729ef8

Please sign in to comment.