Files
mrp-qrcode/lib/password.ts
T
2026-04-20 15:49:01 -05:00

30 lines
860 B
TypeScript

import bcrypt from "bcryptjs";
const ADMIN_ROUNDS = 12;
const PIN_ROUNDS = 12;
export async function hashPassword(password: string): Promise<string> {
if (!password || password.length < 8) {
throw new Error("Password must be at least 8 characters");
}
return bcrypt.hash(password, ADMIN_ROUNDS);
}
export async function verifyPassword(password: string, hash: string): Promise<boolean> {
return bcrypt.compare(password, hash);
}
export function isValidPin(pin: string): boolean {
return /^\d{4}$/.test(pin);
}
export async function hashPin(pin: string): Promise<string> {
if (!isValidPin(pin)) throw new Error("PIN must be exactly 4 digits");
return bcrypt.hash(pin, PIN_ROUNDS);
}
export async function verifyPin(pin: string, hash: string): Promise<boolean> {
if (!isValidPin(pin)) return false;
return bcrypt.compare(pin, hash);
}