-
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.
- Loading branch information
Showing
7 changed files
with
49 additions
and
19 deletions.
There are no files selected for viewing
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,2 +1,3 @@ | ||
export { bytesToHex, hexToBytes, bytesToBase64, base64ToBytes, base64ToHex, hexToBase64 } from "@occult-app/crypto/types"; | ||
export { randomBytes } from "@occult-app/crypto/random"; | ||
export { sha256, sha512 } from "@occult-app/crypto/sha2"; |
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
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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()); | ||
} | ||
|
||
function sha256(input: string): ByteArray { | ||
return sha2(input, "SHA-256"); | ||
} | ||
|
||
function sha512(input: string): ByteArray { | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ByteArray, bytesToHex } from "../src/types"; | ||
import { sha256, sha512 } from "../src/sha2"; | ||
|
||
describe("SHA2", () => { | ||
const input: string = "hash me!"; | ||
|
||
it("should compute the SHA-256 hash", async () => { | ||
const hash: ByteArray = await sha256(input); | ||
expect(hash.length).toEqual(32); | ||
expect(bytesToHex(hash)).toEqual( | ||
"f1df4a6d8659471333f7f6470d593e0911b4d487856d88c83d2d187afa195927" | ||
); | ||
}); | ||
|
||
it("should compute the SHA-512 hash", async () => { | ||
const hash: ByteArray = await sha512(input); | ||
expect(hash.length).toEqual(64); | ||
expect(bytesToHex(hash)).toEqual( | ||
"3b20c1f6b69e9087e8df6ca856c43f752ba005e29d8fe0de2c20f12d456cfaf63dc4717a7c3e9c5da7166dcf2e932f62a47f113325553a0908883c190859b0c3" | ||
); | ||
}); | ||
}); |