Skip to content

feat: Add getRandomBytes - #10563

Merged
FrederikBolding merged 3 commits into
mainfrom
fb/csprng
Sep 29, 2026
Merged

FrederikBolding merged 3 commits into
mainfrom
fb/csprng

Conversation

@FrederikBolding

@FrederikBolding FrederikBolding commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Explanation

Add utility for generating cryptographically secure random numbers.

References

N/A

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Low Risk
New additive API with input validation and tests; no changes to existing crypto primitives or consumers.

Overview
Adds getRandomBytes to @metamask/cryptography as a small wrapper around globalThis.crypto.getRandomValues, returning a Uint8Array of the requested size.

The function rejects non-positive lengths with a clear error. It is re-exported from the package entrypoint, covered by unit tests (length, CSPRNG call, zero-length throw), and noted in the package changelog under the initial release notes.

Reviewed by Cursor Bugbot for commit 6aa9eed. Bugbot is set up for automated code reviews on this repo. Configure here.

@FrederikBolding
FrederikBolding marked this pull request as ready for review September 29, 2026 09:45
@FrederikBolding
FrederikBolding requested a review from a team as a code owner September 29, 2026 09:45
* @param length - The number of random bytes to generate.
* @returns An `Uint8Array` of the provided length with random bytes.
*/
export function getRandomBytes(length: number): Uint8Array {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe validate length > 0?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

@FrederikBolding FrederikBolding Sep 29, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FrederikBolding
FrederikBolding added this pull request to the merge queue Sep 29, 2026
Merged via the queue into main with commit f130848 Sep 29, 2026
44 checks passed
@FrederikBolding
FrederikBolding deleted the fb/csprng branch September 29, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants