argon2.ts

1008 B · raw

1import { argon2id } from "hash-wasm";23// Argon2id params: m=65536 KiB (64 MiB), t=3, p=4, 32-byte output.4// Meets the OWASP "password manager" tier (m=65536, t=3, p=1) and exceeds it5// on parallelism. Changing any of these changes the derived key — existing6// vaults on disk can only be unlocked with the params they were sealed under.7// Raising them tightens attacker cost; lowering them is a regression.8export async function deriveKey(9  password: string,10  salt: Uint8Array11): Promise<Uint8Array> {12  const result = await argon2id({13    password,14    salt,15    parallelism: 4,16    iterations: 3,17    memorySize: 65536,18    hashLength: 32,19    outputType: "binary",20  });21  return result;22}2324// Per-vault salt: 16 bytes from the OS CSPRNG. A fresh salt is generated at25// vault creation and on every changePassword. Prevents cross-user26// precomputation attacks against the same master passphrase.27export function generateSalt(): Uint8Array {28  return crypto.getRandomValues(new Uint8Array(16));29}