Skip to content

Commit

Permalink
feat(OCLT-25): add sha2 wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ythepaut committed Apr 26, 2024
1 parent 45bcb90 commit dbfa46a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 19 deletions.
1 change: 1 addition & 0 deletions build/exports.js
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";
3 changes: 1 addition & 2 deletions build/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 1 addition & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"author": "Occult Team",
"license": "GPL-3.0-or-later",
"homepage": "https://github.com/occult-app/crypto#readme",
"dependencies": {
"@noble/hashes": "1.4.0"
},
"devDependencies": {
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
Expand Down Expand Up @@ -59,9 +62,5 @@
"extends": [
"@commitlint/config-conventional"
]
},
"dependencies": {
"@noble/curves": "1.4.0",
"@noble/hashes": "1.4.0"
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ import {
hexToBase64
} from "./types";
import { randomBytes } from "./random";
import { sha256, sha512 } from "./sha2";
20 changes: 20 additions & 0 deletions src/sha2.ts
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 };
22 changes: 22 additions & 0 deletions tests/sha2.spec.ts
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"
);
});
});

0 comments on commit dbfa46a

Please sign in to comment.