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

25 lines
698 B
JavaScript
Raw Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
const { createHmac } = require("crypto");
/**
* # Hash password Function
* @param {object} param0
* @param {string} param0.password - Password to hash
2024-12-08 08:58:57 +00:00
* @param {string} [param0.encryptionKey] - Encryption key
2023-09-21 14:00:04 +00:00
* @returns {string}
*/
module.exports = function hashPassword({ password, encryptionKey }) {
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;
};