Calio/server/auth.js
Elijah 6fc0a01c56
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
first commit
2026-10-20 19:04:12 -07:00

25 lines
667 B
JavaScript

import {
scryptSync,
randomBytes,
timingSafeEqual,
createHash,
} from "node:crypto";
export function hashPassword(password) {
const salt = randomBytes(16).toString("hex");
return `scrypt:${salt}:${scryptSync(password, salt, 64).toString("hex")}`;
}
export function checkPassword(password, hash) {
const [method, salt, hex] = hash.split(":");
if (
method !== "scrypt" ||
!/^[a-f0-9]{32}$/.test(salt) ||
!/^[a-f0-9]{128}$/.test(hex)
)
return false;
return timingSafeEqual(
scryptSync(password, salt, 64),
Buffer.from(hex, "hex"),
);
}
export const tokenHash = (token) =>
createHash("sha256").update(token).digest("hex");