Route login and character requests by their original host names on one loopback port. Add local accounts, saves, and credential redaction.
18 lines
743 B
TypeScript
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);
|
|
}
|