generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Add SHA-256 utility #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c8d34cb
feat: Add SHA-256 utility
FrederikBolding 47a8473
Fix lint
FrederikBolding fc4902b
Export
FrederikBolding cc45448
Tweak Node 18 tests
FrederikBolding d33945d
Use webcrypto as mock
FrederikBolding 85e2b84
Simplify
FrederikBolding 6cbed7a
Apply suggestions from code review
FrederikBolding 8961a54
Fix lint
FrederikBolding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import * as nobleHashes from '@noble/hashes/sha256'; | ||
| import { webcrypto } from 'crypto'; | ||
| import { parse } from 'semver'; | ||
|
|
||
| import { bytesToHex, stringToBytes } from './bytes'; | ||
| import { sha256 } from './hashing'; | ||
|
|
||
| describe('sha256', () => { | ||
| const isNode18 = parse(process.version)?.major === 18; | ||
|
|
||
| // The global does not exist in Node 18, so we must add it. | ||
| // eslint-disable-next-line jest/no-if | ||
| if (isNode18) { | ||
| Object.defineProperty(globalThis, 'crypto', { | ||
| value: webcrypto, | ||
| writable: true, | ||
| }); | ||
| } | ||
|
|
||
| it('returns a digest for a byte array', async () => { | ||
| const digest = await sha256(stringToBytes('foo bar')); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0xfbc1a9f858ea9e177916964bd88c3d37b91a1e84412765e29950777f265c4b75', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns a digest for a larger byte array', async () => { | ||
| const digest = await sha256(new Uint8Array(1024).fill(1)); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0x5a648d8015900d89664e00e125df179636301a2d8fa191c1aa2bd9358ea53a69', | ||
| ); | ||
| }); | ||
|
|
||
| it('falls back to noble when digest function is unavailable', async () => { | ||
| const nobleSpy = jest.spyOn(nobleHashes, 'sha256'); | ||
|
|
||
| Object.defineProperty(globalThis.crypto.subtle, 'digest', { | ||
| value: undefined, | ||
| writable: true, | ||
| }); | ||
|
|
||
| const digest = await sha256(stringToBytes('foo bar')); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0xfbc1a9f858ea9e177916964bd88c3d37b91a1e84412765e29950777f265c4b75', | ||
| ); | ||
|
|
||
| expect(nobleSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('falls back to noble when subtle APIs are unavailable', async () => { | ||
| const nobleSpy = jest.spyOn(nobleHashes, 'sha256'); | ||
|
|
||
| Object.defineProperty(globalThis.crypto, 'subtle', { | ||
| value: undefined, | ||
| writable: true, | ||
| }); | ||
|
|
||
| const digest = await sha256(stringToBytes('foo bar')); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0xfbc1a9f858ea9e177916964bd88c3d37b91a1e84412765e29950777f265c4b75', | ||
| ); | ||
|
|
||
| expect(nobleSpy).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { sha256 as nobleSha256 } from '@noble/hashes/sha256'; | ||
|
|
||
| /** | ||
| * Compute a SHA-256 digest for a given byte array. | ||
| * | ||
| * Uses the native crypto implementation and falls back to noble. | ||
| * | ||
| * @param bytes - A byte array. | ||
| * @returns The SHA-256 hash as a byte array. | ||
| */ | ||
| export async function sha256(bytes: Uint8Array): Promise<Uint8Array> { | ||
| // Use crypto.subtle.digest whenever possible as it is faster. | ||
| if ( | ||
| 'crypto' in globalThis && | ||
| typeof globalThis.crypto === 'object' && | ||
| // eslint-disable-next-line no-restricted-globals | ||
| globalThis.crypto.subtle?.digest | ||
| ) { | ||
| // eslint-disable-next-line no-restricted-globals | ||
| return new Uint8Array( | ||
| await globalThis.crypto.subtle.digest('SHA-256', bytes), | ||
| ); | ||
| } | ||
| return nobleSha256(bytes); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.