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

25 lines
698 B
JavaScript
Raw Normal View History

2024-12-06 13:24:26 +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:57:48 +00:00
* @param {string} [param0.encryptionKey] - Encryption key
2024-12-06 13:24:26 +00:00
* @returns {string}
*/
module.exports = function hashPassword({ password, encryptionKey }) {
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;
};