import crypto from 'node:crypto' import util from 'node:util' const scrypt = util.promisify(crypto.scrypt) const tokenLength = 64 export async function hashPassword(password: string) { const salt = crypto.randomBytes(32) const hash = (await scrypt(password, salt, 64)) as Buffer return Buffer.concat([salt, hash]).toString('base64') } export async function verifyPassword(password: string, hash: string) { const buf = Buffer.from(hash, 'base64') return crypto.timingSafeEqual(buf.subarray(32), (await scrypt(password, buf.subarray(0, 32), 64)) as Buffer) } export function generateToken(length = tokenLength) { return crypto.randomBytes(length / 2).toString('hex') }