reFactor verify function

This commit is contained in:
kehiy 2023-05-15 02:18:20 -07:00
parent 1125752511
commit 0eb0c351ee

View file

@ -2,7 +2,7 @@ const passwordHashOld = require("password-hash");
const bcrypt = require("bcryptjs"); const bcrypt = require("bcryptjs");
const saltRounds = bcrypt.genSalt(); const saltRounds = bcrypt.genSaltSync();
/** /**
* Hash a password * Hash a password
@ -20,13 +20,12 @@ exports.generate = function (password) {
* @returns {boolean} Does the password match the hash? * @returns {boolean} Does the password match the hash?
*/ */
exports.verify = function (password, hash) { exports.verify = function (password, hash) {
if (isSHA1(hash)) { let match;
return passwordHashOld.verify(password, hash); match = isSHA1(hash) ? passwordHashOld.verify(password, hash) : bcrypt.compareSync(password, hash);
} return match;
return bcrypt.compareSync(password, hash);
}; };
/** /**
* Is the hash a SHA1 hash * Is the hash a SHA1 hash
* @param {string} hash * @param {string} hash
@ -34,7 +33,7 @@ exports.verify = function (password, hash) {
*/ */
function isSHA1(hash) { function isSHA1(hash) {
return (typeof hash === "string" && hash.startsWith("sha1")); return (typeof hash === "string" && hash.startsWith("sha1"));
} };
/** /**
* Does the hash need to be rehashed? * Does the hash need to be rehashed?