-
-
Notifications
You must be signed in to change notification settings - Fork 308
feat: Add WebCrypto wrappers for SHA hashing #10282
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
b470fad
feat: Add WebCrypto wrappers for SHA hashing
FrederikBolding c05985c
Update tsconfig and README
FrederikBolding 4dc5015
Add generics from newer TypeScript DOM types
FrederikBolding 5f088a3
Fix lint and CHANGELOG
FrederikBolding 9f5f8cc
Use BufferSource
FrederikBolding 925ce83
Ordering
FrederikBolding 2ecb207
Add a couple more tests
FrederikBolding 0465a77
Remove typo
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
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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,9 +1 @@ | ||
| /** | ||
| * Example function that returns a greeting for the given name. | ||
| * | ||
| * @param name - The name to greet. | ||
| * @returns The greeting. | ||
| */ | ||
| export default function greeter(name: string): string { | ||
| return `Hello, ${name}!`; | ||
| } | ||
| export * from './sha.js'; |
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,93 @@ | ||
| import { stringToBytes, bytesToHex } from '@metamask/utils'; | ||
|
|
||
| import { sha256, sha384, sha512 } from './sha.js'; | ||
|
|
||
| describe('SHA-256', () => { | ||
| 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('returns a digest for an array buffer', async () => { | ||
| const digest = await sha256(new ArrayBuffer(1024)); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0x5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns a digest for a data view', async () => { | ||
| const digest = await sha256(new DataView(new ArrayBuffer(1024), 512)); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0x076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('SHA-384', () => { | ||
| it('returns a digest for a byte array', async () => { | ||
| const digest = await sha384(stringToBytes('foo bar')); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0x6839312f3db343477070d3c0b2becd417b357154d48794d01d78cfb4617ed5ab819a77b6832f6542dd18bb738131ef7e', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns a digest for a larger byte array', async () => { | ||
| const digest = await sha384(new Uint8Array(1024).fill(1)); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0x45730a19acff8481e7e2b99c4100a09a0288a3bc45df56ff7e72dd92ef9e4c92f925c9d6ba1ea96c934a5f1e782a7cc7', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns a digest for an array buffer', async () => { | ||
| const digest = await sha384(new ArrayBuffer(1024)); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0xccdfa1aec6214bf6db74b4addaca7f87ab5980bcfdbf6f5fcab8d8425bc2169ca3bc9dd6046b26e4b1da6ba33c31dfb0', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns a digest for a data view', async () => { | ||
| const digest = await sha384(new DataView(new ArrayBuffer(1024), 512)); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0xd83d9a38c238ef3b7bc207bbea3287a8b37b37e731480a8d240d2a6953086c5ecbdf7ee4c72fec3a3e9d4a87f4f9b4fe', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('SHA-512', () => { | ||
| it('returns a digest for a byte array', async () => { | ||
| const digest = await sha512(stringToBytes('foo bar')); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0x65019286222ace418f742556366f9b9da5aaf6797527d2f0cba5bfe6b2f8ed24746542a0f2be1da8d63c2477f688b608eb53628993afa624f378b03f10090ce7', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns a digest for a larger byte array', async () => { | ||
| const digest = await sha512(new Uint8Array(1024).fill(1)); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0x19c6841f3d6e33a4d28e7cb47ff938728479c56bb930f3e8535ec24d9453d9665b7dc1163181b94a1ada9554e953a094ed44fd6faee7a9bbde6615375bab4ae8', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns a digest for an array buffer', async () => { | ||
| const digest = await sha512(new ArrayBuffer(1024)); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0x8efb4f73c5655351c444eb109230c556d39e2c7624e9c11abc9e3fb4b9b9254218cc5085b454a9698d085cfa92198491f07a723be4574adc70617b73eb0b6461', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns a digest for a data view', async () => { | ||
| const digest = await sha512(new DataView(new ArrayBuffer(1024), 512)); | ||
| expect(bytesToHex(digest)).toBe( | ||
| '0xdf40d4a774e0b453a5b87c00d6f0ef5d753143454e88ee5f7b607134598294c7905ccbcf94bbc46e474db6eb44e56a6dbb6d9a1be9d4fb5d1b5f2d0c6ed34bfe', | ||
| ); | ||
| }); | ||
| }); |
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,35 @@ | ||
| /** | ||
| * Compute a SHA-256 digest for a given byte array. | ||
| * | ||
| * @param bytes - A byte array. | ||
| * @returns The SHA-256 hash as a byte array. | ||
| */ | ||
| export async function sha256(bytes: BufferSource): Promise<Uint8Array> { | ||
| return new Uint8Array( | ||
| await globalThis.crypto.subtle.digest('SHA-256', bytes), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Compute a SHA-384 digest for a given byte array. | ||
| * | ||
| * @param bytes - A byte array. | ||
| * @returns The SHA-384 hash as a byte array. | ||
| */ | ||
| export async function sha384(bytes: BufferSource): Promise<Uint8Array> { | ||
| return new Uint8Array( | ||
| await globalThis.crypto.subtle.digest('SHA-384', bytes), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Compute a SHA-512 digest for a given byte array. | ||
| * | ||
| * @param bytes - A byte array. | ||
| * @returns The SHA-512 hash as a byte array. | ||
| */ | ||
| export async function sha512(bytes: BufferSource): Promise<Uint8Array> { | ||
| return new Uint8Array( | ||
| await globalThis.crypto.subtle.digest('SHA-512', 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| { | ||
| "extends": "../../tsconfig.packages.json", | ||
| "references": [], | ||
| "references": [ | ||
| { | ||
| "path": "../utils" | ||
| } | ||
| ], | ||
| "include": ["../../types", "./src"] | ||
| } |
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.
There was a problem hiding this comment.
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.