13 lines
563 B
TypeScript
13 lines
563 B
TypeScript
import { scryptSync, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
export function hashPassword(password: string) {
|
|
const salt = randomBytes(16).toString('hex');
|
|
return `${salt}:${scryptSync(password, salt, 64).toString('hex')}`;
|
|
}
|
|
export function verifyPassword(password: string, stored: string) {
|
|
const [salt, hash] = stored.split(':');
|
|
if (!salt || !hash) return false;
|
|
const expected = Buffer.from(hash, 'hex');
|
|
const actual = scryptSync(password, salt, 64);
|
|
return expected.length === actual.length && timingSafeEqual(actual, expected);
|
|
}
|