import { createHmac } from "crypto";
import grabKeys from "../../utils/grab-keys";

type Param = {
    password: string;
    encryptionKey?: string;
};

/**
 * # Hash password Function
 */
export default function hashPassword({
    password,
    encryptionKey,
}: Param): string {
    const { key } = grabKeys({ encryptionKey });

    if (!key?.match(/.{8,}/)) {
        throw new Error("Encryption key is invalid");
    }

    const hmac = createHmac("sha512", key);
    hmac.update(password);
    let hashed = hmac.digest("base64");
    return hashed;
}
