From 67fe90ac4365982f879af47daf80107866f05251 Mon Sep 17 00:00:00 2001 From: arsam Date: Fri, 18 Sep 2026 23:09:19 +0330 Subject: [PATCH] Import source: backend/src/lib/password Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality. --- backend/src/lib/password.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 backend/src/lib/password.ts diff --git a/backend/src/lib/password.ts b/backend/src/lib/password.ts new file mode 100644 index 0000000..ded9a17 --- /dev/null +++ b/backend/src/lib/password.ts @@ -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); +}