dsql-admin/dsql-app/package-shared/functions/dsql/hashPassword.ts

27 lines
611 B
TypeScript
Raw Normal View History

2025-01-13 08:00:21 +00:00
import { createHmac } from "crypto";
2024-12-06 13:24:26 +00:00
2025-01-13 08:00:21 +00:00
type Param = {
password: string;
encryptionKey?: string;
};
2024-12-06 13:24:26 +00:00
/**
* # Hash password Function
*/
2025-01-13 08:00:21 +00:00
export default function hashPassword({
password,
encryptionKey,
}: Param): string {
2024-12-08 08:57:48 +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);
2024-12-06 13:24:26 +00:00
hmac.update(password);
let hashed = hmac.digest("base64");
return hashed;
2025-01-13 08:00:21 +00:00
}