Packages

A replacement for gleam_crypto that works on all targets.

Current section

Files

Jump to
munch src random.ffi.mjs
Raw

src/random.ffi.mjs

import { BitArray$BitArray } from "./gleam.mjs";
const maximumChunkSize = 65_536;
export function strong_random_bytes(length) {
const crypto = globalThis.crypto;
if (!crypto?.getRandomValues) {
throw new Error("WebCrypto API is not available");
}
const bytes = new Uint8Array(length);
for (let offset = 0; offset < bytes.length; offset += maximumChunkSize) {
crypto.getRandomValues(bytes.subarray(offset, offset + maximumChunkSize));
}
return BitArray$BitArray(bytes);
}