datasquirel/package-shared/functions/dsql/hashPassword.ts

27 lines
611 B
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
import { createHmac } from "crypto";
2023-09-21 14:00:04 +00:00
2025-01-10 19:10:28 +00:00
type Param = {
password: string;
encryptionKey?: string;
};
2023-09-21 14:00:04 +00:00
/**
* # Hash password Function
*/
2025-01-10 19:10:28 +00:00
export default function hashPassword({
password,
encryptionKey,
}: Param): string {
2024-12-08 08:58:57 +00:00
const finalEncryptionKey =
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
if (!finalEncryptionKey?.match(/.{8,}/)) {
throw new Error("Encryption key is invalid");
}
const hmac = createHmac("sha512", finalEncryptionKey);
2023-09-21 14:00:04 +00:00
hmac.update(password);
let hashed = hmac.digest("base64");
return hashed;
2025-01-10 19:10:28 +00:00
}