2021-07-27 13:47:13 -04:00
|
|
|
const passwordHashOld = require("password-hash");
|
2021-08-31 07:56:44 -04:00
|
|
|
const bcrypt = require("bcryptjs");
|
2021-07-13 10:22:46 -04:00
|
|
|
const saltRounds = 10;
|
|
|
|
|
2022-04-20 14:56:40 -04:00
|
|
|
/**
|
|
|
|
* Hash a password
|
2023-08-11 03:46:41 -04:00
|
|
|
* @param {string} password Password to hash
|
|
|
|
* @returns {string} Hash
|
2022-04-20 14:56:40 -04:00
|
|
|
*/
|
2021-07-13 10:22:46 -04:00
|
|
|
exports.generate = function (password) {
|
|
|
|
return bcrypt.hashSync(password, saltRounds);
|
2022-04-20 14:56:40 -04:00
|
|
|
};
|
2021-07-13 10:22:46 -04:00
|
|
|
|
2022-04-20 14:56:40 -04:00
|
|
|
/**
|
|
|
|
* Verify a password against a hash
|
2023-08-11 03:46:41 -04:00
|
|
|
* @param {string} password Password to verify
|
|
|
|
* @param {string} hash Hash to verify against
|
2022-04-20 14:56:40 -04:00
|
|
|
* @returns {boolean} Does the password match the hash?
|
|
|
|
*/
|
2021-07-13 10:22:46 -04:00
|
|
|
exports.verify = function (password, hash) {
|
|
|
|
if (isSHA1(hash)) {
|
2022-04-20 14:56:40 -04:00
|
|
|
return passwordHashOld.verify(password, hash);
|
2021-07-13 10:22:46 -04:00
|
|
|
}
|
2021-07-27 13:47:13 -04:00
|
|
|
|
|
|
|
return bcrypt.compareSync(password, hash);
|
2022-04-20 14:56:40 -04:00
|
|
|
};
|
2021-07-13 10:22:46 -04:00
|
|
|
|
2022-04-20 14:56:40 -04:00
|
|
|
/**
|
|
|
|
* Is the hash a SHA1 hash
|
2023-08-11 03:46:41 -04:00
|
|
|
* @param {string} hash Hash to check
|
|
|
|
* @returns {boolean} Is SHA1 hash?
|
2022-04-20 14:56:40 -04:00
|
|
|
*/
|
2021-07-13 10:22:46 -04:00
|
|
|
function isSHA1(hash) {
|
2022-04-13 12:30:32 -04:00
|
|
|
return (typeof hash === "string" && hash.startsWith("sha1"));
|
2021-07-13 10:22:46 -04:00
|
|
|
}
|
|
|
|
|
2022-04-20 14:56:40 -04:00
|
|
|
/**
|
|
|
|
* Does the hash need to be rehashed?
|
2023-08-11 03:46:41 -04:00
|
|
|
* @param {string} hash Hash to check
|
|
|
|
* @returns {boolean} Needs to be rehashed?
|
2022-04-20 14:56:40 -04:00
|
|
|
*/
|
2021-07-13 10:22:46 -04:00
|
|
|
exports.needRehash = function (hash) {
|
|
|
|
return isSHA1(hash);
|
2022-04-20 14:56:40 -04:00
|
|
|
};
|