Files
WT-Offline/packages/local-service/src/accounts/password.ts
T
N0VAN0M4LY 18f63bf33e Implement local login
Route login and character requests by their original host names on one
loopback port. Add local accounts, saves, and credential redaction.
2026-11-24 05:01:26 +08:00

18 lines
743 B
TypeScript

/** Password hashes for local accounts (scrypt, per-account random salt). */
import { randomBytes, scryptSync, timingSafeEqual } from 'node:crypto';
const KEY_LENGTH = 32;
export function hashPassword(password: string): string {
const salt = randomBytes(16);
return `scrypt$${salt.toString('hex')}$${scryptSync(password, salt, KEY_LENGTH).toString('hex')}`;
}
export function verifyPassword(password: string, stored: string): boolean {
const [scheme, saltHex, hashHex] = stored.split('$');
if (scheme !== 'scrypt' || !saltHex || !hashHex) return false;
const expected = Buffer.from(hashHex, 'hex');
const actual = scryptSync(password, Buffer.from(saltHex, 'hex'), expected.length);
return timingSafeEqual(actual, expected);
}