2026-03-07 18:11:46 +01:00
|
|
|
export function randomBytes(size) {
|
|
|
|
|
const buf = new Uint8Array(size);
|
|
|
|
|
crypto.getRandomValues(buf);
|
|
|
|
|
|
2026-03-11 22:08:30 +01:00
|
|
|
buf.toString = function (encoding) {
|
|
|
|
|
if (encoding === "hex") {
|
|
|
|
|
return Array.from(this)
|
|
|
|
|
.map((b) => b.toString(16).padStart(2, "0"))
|
|
|
|
|
.join("");
|
2026-03-07 18:11:46 +01:00
|
|
|
}
|
2026-03-17 12:38:30 +01:00
|
|
|
|
2026-03-11 22:08:30 +01:00
|
|
|
if (encoding === "base64") {
|
2026-03-07 18:11:46 +01:00
|
|
|
return btoa(String.fromCharCode(...this));
|
|
|
|
|
}
|
2026-03-17 12:38:30 +01:00
|
|
|
|
2026-03-07 18:11:46 +01:00
|
|
|
return new TextDecoder().decode(this);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|