import { randomBytes, scryptSync, timingSafeEqual } from "node:crypto";
const N = 16384;
const R = 8;
const P = 1;
const KEYLEN = 64;
const SALT_BYTES = 16;
/**
 * Hachage de mot de passe — scrypt (node:crypto), format autonome :
 *   scrypt$16384$8$1$<salt hex>$<hash hex>
 */
export function hashPassword(password) {
    const salt = randomBytes(SALT_BYTES);
    const hash = scryptSync(password, salt, KEYLEN, { N, r: R, p: P });
    return `scrypt$${N}$${R}$${P}$${salt.toString("hex")}$${hash.toString("hex")}`;
}
export function verifyPassword(password, stored) {
    const parts = stored.split("$");
    if (parts.length !== 6 || parts[0] !== "scrypt")
        return false;
    const n = Number(parts[1]);
    const r = Number(parts[2]);
    const p = Number(parts[3]);
    const salt = Buffer.from(parts[4] ?? "", "hex");
    const expected = Buffer.from(parts[5] ?? "", "hex");
    if (salt.length === 0 || expected.length !== KEYLEN)
        return false;
    const actual = scryptSync(password, salt, KEYLEN, { N: n, r: r, p: p });
    return timingSafeEqual(actual, expected);
}
//# sourceMappingURL=passwords.js.map