V1 remains intact on main and in history. This branch replaces it with a new application: node:sqlite storage with migrations, scrypt auth and sessions, content/SRS/attempt/stats services, document extraction and chunking, a provider-neutral AI layer (OpenAI, Anthropic, Gemini, OpenAI-compatible, Ollama), the generation pipeline and the tool-using assistant. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
62 lines
2.8 KiB
TypeScript
62 lines
2.8 KiB
TypeScript
import crypto from 'node:crypto';
|
|
|
|
const SCRYPT = { N: 32768, r: 8, p: 1, keylen: 32 };
|
|
|
|
function scrypt(password: string, salt: Buffer, N: number, r: number, p: number, keylen: number): Promise<Buffer> {
|
|
return new Promise((resolve, reject) => {
|
|
crypto.scrypt(password.normalize('NFKC'), salt, keylen, { N, r, p, maxmem: 256 * N * r }, (err, key) => (err ? reject(err) : resolve(key)));
|
|
});
|
|
}
|
|
|
|
/** scrypt password hash: `scrypt$N$r$p$salt$hash` (base64url fields). */
|
|
export async function hashPassword(password: string): Promise<string> {
|
|
const salt = crypto.randomBytes(16);
|
|
const key = await scrypt(password, salt, SCRYPT.N, SCRYPT.r, SCRYPT.p, SCRYPT.keylen);
|
|
return ['scrypt', SCRYPT.N, SCRYPT.r, SCRYPT.p, salt.toString('base64url'), key.toString('base64url')].join('$');
|
|
}
|
|
|
|
export async function verifyPassword(password: string, stored: string): Promise<boolean> {
|
|
const parts = stored.split('$');
|
|
if (parts.length !== 6 || parts[0] !== 'scrypt') return false;
|
|
const [, n, r, p, saltB64, hashB64] = parts;
|
|
const expected = Buffer.from(hashB64, 'base64url');
|
|
try {
|
|
const actual = await scrypt(password, Buffer.from(saltB64, 'base64url'), Number(n), Number(r), Number(p), expected.length);
|
|
return actual.length === expected.length && crypto.timingSafeEqual(actual, expected);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/** AES-256-GCM. Output: `v1.<iv>.<tag>.<ciphertext>` (base64url). */
|
|
export function encryptSecret(plain: string, key: Buffer): string {
|
|
const iv = crypto.randomBytes(12);
|
|
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
|
|
const data = Buffer.concat([cipher.update(plain, 'utf8'), cipher.final()]);
|
|
return ['v1', iv.toString('base64url'), cipher.getAuthTag().toString('base64url'), data.toString('base64url')].join('.');
|
|
}
|
|
|
|
/** Returns null when the value cannot be decrypted (wrong key, tampering). */
|
|
export function decryptSecret(value: string | null | undefined, key: Buffer): string | null {
|
|
if (!value) return null;
|
|
const parts = value.split('.');
|
|
if (parts.length !== 4 || parts[0] !== 'v1') return null;
|
|
try {
|
|
const decipher = crypto.createDecipheriv('aes-256-gcm', key, Buffer.from(parts[1], 'base64url'));
|
|
decipher.setAuthTag(Buffer.from(parts[2], 'base64url'));
|
|
return Buffer.concat([decipher.update(Buffer.from(parts[3], 'base64url')), decipher.final()]).toString('utf8');
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function maskSecret(secret: string | null): string {
|
|
if (!secret) return '';
|
|
return secret.length <= 8 ? '••••' : `${secret.slice(0, 3)}••••${secret.slice(-4)}`;
|
|
}
|
|
|
|
export function safeEqual(a: string, b: string): boolean {
|
|
const x = crypto.createHash('sha256').update(a).digest();
|
|
const y = crypto.createHash('sha256').update(b).digest();
|
|
return crypto.timingSafeEqual(x, y);
|
|
}
|