-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use randomBytes from @noble/hashes
- Loading branch information
Showing
4 changed files
with
9 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1 @@ | ||
/** | ||
* Generates cryptographically secure random bytes. | ||
* @param size The number of random bytes to generate. The `size` must not be greater than 65536. | ||
* @returns A Uint8Array containing the random bytes. | ||
* @throws {RangeError} If `size` is greater than 65536. | ||
*/ | ||
export function randomBytes(size: number): Uint8Array { | ||
const MAX_BYTES = 65536; // limit of Crypto.getRandomValues() | ||
if (size > MAX_BYTES) { | ||
throw new RangeError(`size must be less than or equal to ${MAX_BYTES}`); | ||
} | ||
|
||
const bytes = new Uint8Array(size); | ||
try { | ||
crypto.getRandomValues(bytes); // Node.js 20+ and modern browsers | ||
} catch { | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
require("crypto").getRandomValues(bytes); // Node.js 18 | ||
} | ||
|
||
return bytes; | ||
} | ||
export { randomBytes } from "@noble/hashes/utils"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.