Import source: backend/src/lib/password

Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
arsam
2026-09-18 23:09:19 +03:30
parent 135b9e8c58
commit 67fe90ac43
+18
View File
@@ -0,0 +1,18 @@
import { randomBytes, scryptSync, timingSafeEqual } from "crypto";
const keyLength = 64;
export function hashPassword(password: string) {
const salt = randomBytes(24).toString("hex");
const hash = scryptSync(password, salt, keyLength).toString("hex");
return `scrypt$${salt}$${hash}`;
}
export function verifyPassword(password: string, storedHash: string | null | undefined) {
if (!storedHash) return false;
const [algorithm, salt, hash] = storedHash.split("$");
if (algorithm !== "scrypt" || !salt || !hash) return false;
const expected = Buffer.from(hash, "hex");
const actual = scryptSync(password, salt, expected.length);
return expected.length === actual.length && timingSafeEqual(expected, actual);
}