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:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user