Files
ignis/shims/crypto/random-bytes.js
2026-03-11 22:08:30 +01:00

19 lines
434 B
JavaScript

export function randomBytes(size) {
const buf = new Uint8Array(size);
crypto.getRandomValues(buf);
buf.toString = function (encoding) {
if (encoding === "hex") {
return Array.from(this)
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
if (encoding === "base64") {
return btoa(String.fromCharCode(...this));
}
return new TextDecoder().decode(this);
};
return buf;
}