28 lines
690 B
TypeScript
28 lines
690 B
TypeScript
import bcrypt from "bcryptjs";
|
|
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
|
|
const BCRYPT_ROUNDS = 12;
|
|
|
|
export async function hashPassword(password: string): Promise<string> {
|
|
return bcrypt.hash(password, BCRYPT_ROUNDS);
|
|
}
|
|
|
|
export async function verifyPassword(
|
|
password: string,
|
|
passwordHash: string,
|
|
): Promise<boolean> {
|
|
return bcrypt.compare(password, passwordHash);
|
|
}
|
|
|
|
export function createOpaqueToken(size = 24): string {
|
|
return randomBytes(size).toString("base64url");
|
|
}
|
|
|
|
export function createStableId(): string {
|
|
return randomUUID();
|
|
}
|
|
|
|
export function sha256(input: string): string {
|
|
return createHash("sha256").update(input).digest("hex");
|
|
}
|