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