Skip to content

Commit

Permalink
feat(OCLT-30): add Argon2id wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ythepaut committed Apr 27, 2024
1 parent be252d2 commit cf7ce57
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions build/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { bytesToHex, hexToBytes, bytesToBase64, base64ToBytes, base64ToHex, hexT
export { randomBytes } from "@occult-app/crypto/random";
export { sha256, sha512 } from "@occult-app/crypto/sha2";
export { kdf } from "@occult-app/crypto/kdf";
export { ppf } from "@occult-app/crypto/ppf";
3 changes: 2 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
},
"transform": {
"^.+\\.(ts|tsx)$": ["ts-jest", { "tsconfig": "./tsconfig.json" }]
}
},
"setupFiles": ["./setup.jest.ts"]
}
3 changes: 3 additions & 0 deletions setup.jest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { TextEncoder, TextDecoder } from "util";

Object.assign(global, { TextDecoder, TextEncoder });
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ import {
import { randomBytes } from "./random";
import { sha256, sha512 } from "./sha2";
import { kdf } from "./kdf";
import { ppf } from "./ppf";
25 changes: 25 additions & 0 deletions src/ppf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ByteArray } from "./types";
import { argon2id } from "@noble/hashes/argon2";

const ARGON2ID_ITERATION_COUNT: number = 3;
const ARGON2ID_MEMORY_COST: number = 65536; // 65536 KB = 64 MiB
const ARGON2ID_PARALLELISM: number = 2;

/**
* Password Processing Function : Derives a password using Argon2id.
*
* @param password The password to derive.
* @param salt PPF salt
* @param outputLength The length of the output key in bytes.
* @returns The derived key.
*/
function ppf(password: string, salt: ByteArray, outputLength: number = 32): ByteArray {
return argon2id(password, salt, {
t: ARGON2ID_ITERATION_COUNT,
m: ARGON2ID_MEMORY_COST,
p: ARGON2ID_PARALLELISM,
dkLen: outputLength
});
}

export { ppf };
26 changes: 26 additions & 0 deletions tests/ppf.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ppf } from "../src/ppf";
import { ByteArray, bytesToHex } from "../src/types";

describe("Password Processing Function", () => {
const password: string = "SuperS3cret!";
const salt: ByteArray = new Uint8Array([
121, 242, 146, 172, 41, 234, 112, 205, 222, 96, 195, 136, 193, 80, 218, 41, 123, 174, 77,
21, 34, 42, 99, 51, 122, 60, 188, 65, 213, 142, 189, 132
]);

it("should compute the Argon2id hash", () => {
const derivedPassword: ByteArray = ppf(password, salt);

expect(bytesToHex(derivedPassword)).toEqual(
"3482e8e7e70165709a15a690d8a6a3312ae0ea5e645f9be767b7e68aa3bead61"
);
});

it("should compute the Argon2id hash (512 bits)", () => {
const derivedPassword: ByteArray = ppf(password, salt, 64);

expect(bytesToHex(derivedPassword)).toEqual(
"ab2c298ec6e0f1e87d79ba4b43b96daa3e53350d4631983306050507d690053b162c2067b6c6bfd1601cf8b33081464647d5ec817c810af44aa0c68d7b3a40c3"
);
});
});

0 comments on commit cf7ce57

Please sign in to comment.