Skip to content

feat: Add WebCrypto wrappers for SHA hashing - #10282

Merged
FrederikBolding merged 8 commits into
mainfrom
fb/add-webcrypto-sha
Sep 21, 2026
Merged

FrederikBolding merged 8 commits into
mainfrom
fb/add-webcrypto-sha

Conversation

@FrederikBolding

@FrederikBolding FrederikBolding commented Sep 17, 2026

Copy link
Copy Markdown
Member

Explanation

Add wrappers for native SHA-256, SHA-512 and SHA-384 to metamask/cryptography.

References

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
Isolated new hashing wrappers over the platform Web Crypto API with unit tests; no changes to existing consumer auth or key-handling flows in this diff.

Overview
Replaces the @metamask/cryptography placeholder with async SHA helpers (sha256, sha384, sha512) that wrap globalThis.crypto.subtle.digest and accept any BufferSource, returning digests as Uint8Array. The package entrypoint now re-exports ./sha.js instead of the sample greeter.

Adds Jest coverage for each algorithm across Uint8Array, ArrayBuffer, and DataView inputs (using @metamask/utils in tests only), documents the initial release in the changelog, and wires TypeScript project references plus the README dependency graph to @metamask/utils.

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

@FrederikBolding
FrederikBolding marked this pull request as ready for review September 21, 2026 08:21
@FrederikBolding
FrederikBolding requested a review from a team as a code owner September 21, 2026 08:21
Comment thread packages/cryptography/src/sha.ts Outdated
* @param bytes - A byte array.
* @returns The SHA-256 hash as a byte array.
*/
export async function sha256(

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.

Do we not want a fallback in case WebCrypto is not available?

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.

My thinking with the cryptography package was to not have fallbacks. Instead expecting the clients have WebCrypto available.

Comment thread packages/cryptography/src/sha.ts Outdated
Comment thread packages/cryptography/src/sha.ts Outdated
Comment thread packages/cryptography/src/sha.ts Outdated
Comment thread packages/cryptography/src/sha.ts Outdated

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.

If you change the functions to allow BufferSource, adding some tests with other byte array representations here would be good.

Also alphabetic sorting.

@FrederikBolding
FrederikBolding added this pull request to the merge queue Sep 21, 2026
Merged via the queue into main with commit 821e7cf Sep 21, 2026
42 checks passed
@FrederikBolding
FrederikBolding deleted the fb/add-webcrypto-sha branch September 21, 2026 09:53
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