import { scryptSync, randomBytes, timingSafeEqual, createHash, } from "node:crypto"; export function hashPassword(password) { const salt = randomBytes(16).toString("hex"); return `scrypt:${salt}:${scryptSync(password, salt, 64).toString("hex")}`; } export function checkPassword(password, hash) { const [method, salt, hex] = hash.split(":"); if ( method !== "scrypt" || !/^[a-f0-9]{32}$/.test(salt) || !/^[a-f0-9]{128}$/.test(hex) ) return false; return timingSafeEqual( scryptSync(password, salt, 64), Buffer.from(hex, "hex"), ); } export const tokenHash = (token) => createHash("sha256").update(token).digest("hex");