-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: fix build to use WebCryptoAPI and mock for tests
- Loading branch information
Showing
9 changed files
with
127 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 +1,13 @@ | ||
import { Crypto } from "@peculiar/webcrypto"; | ||
import { TextEncoder, TextDecoder } from "util"; | ||
|
||
Object.assign(global, { TextDecoder, TextEncoder }); | ||
|
||
const crypto = new Crypto(); | ||
|
||
Object.defineProperty(global.self, "crypto", { | ||
value: { | ||
subtle: crypto.subtle, | ||
getRandomValues: crypto.getRandomValues | ||
}, | ||
}); |
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
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,20 +1,17 @@ | ||
import { ByteArray } from "../types"; | ||
import { createHash, Hash } from "crypto"; | ||
|
||
type Algorithm = "SHA-256" | "SHA-512"; | ||
|
||
function sha2(input: string, algorithm: Algorithm): ByteArray { | ||
const hash: Hash = createHash(algorithm); | ||
hash.update(input); | ||
return new Uint8Array(hash.digest()); | ||
async function sha2(input: ByteArray, algorithm: Algorithm): Promise<ByteArray> { | ||
return new Uint8Array(await window.crypto.subtle.digest(algorithm, input)); | ||
} | ||
|
||
function sha256(input: string): ByteArray { | ||
return sha2(input, "SHA-256"); | ||
async function sha256(input: ByteArray): Promise<ByteArray> { | ||
return await sha2(input, "SHA-256"); | ||
} | ||
|
||
function sha512(input: string): ByteArray { | ||
return sha2(input, "SHA-512"); | ||
async function sha512(input: ByteArray): Promise<ByteArray> { | ||
return await sha2(input, "SHA-512"); | ||
} | ||
|
||
export { sha256, sha512 }; |
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