27 lines
611 B
TypeScript
27 lines
611 B
TypeScript
import { createHmac } from "crypto";
|
|
|
|
type Param = {
|
|
password: string;
|
|
encryptionKey?: string;
|
|
};
|
|
|
|
/**
|
|
* # Hash password Function
|
|
*/
|
|
export default function hashPassword({
|
|
password,
|
|
encryptionKey,
|
|
}: Param): string {
|
|
const finalEncryptionKey =
|
|
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
|
|
|
if (!finalEncryptionKey?.match(/.{8,}/)) {
|
|
throw new Error("Encryption key is invalid");
|
|
}
|
|
|
|
const hmac = createHmac("sha512", finalEncryptionKey);
|
|
hmac.update(password);
|
|
let hashed = hmac.digest("base64");
|
|
return hashed;
|
|
}
|