uptime-kuma/server/password-hash.js

41 lines
946 B
JavaScript
Raw Normal View History

2021-07-27 19:47:13 +02:00
const passwordHashOld = require("password-hash");
const bcrypt = require("bcryptjs");
2023-05-15 02:18:20 -07:00
const saltRounds = bcrypt.genSaltSync();
2021-07-13 22:22:46 +08:00
/**
* Hash a password
* @param {string} password
* @returns {string}
*/
2021-07-13 22:22:46 +08:00
exports.generate = function (password) {
return bcrypt.hashSync(password, saltRounds);
};
2021-07-13 22:22:46 +08:00
/**
* Verify a password against a hash
* @param {string} password
* @param {string} hash
* @returns {boolean} Does the password match the hash?
*/
2021-07-13 22:22:46 +08:00
exports.verify = function (password, hash) {
2023-05-15 02:18:20 -07:00
let match;
2023-05-15 02:33:11 -07:00
match = isSHA1(hash) ? passwordHashOld.verify(password, hash) : bcrypt.compareSync(password, hash);
2023-05-15 02:18:20 -07:00
return match;
};
2021-07-13 22:22:46 +08:00
/**
* Is the hash a SHA1 hash
* @param {string} hash
* @returns {boolean}
*/
2021-07-13 22:22:46 +08:00
function isSHA1(hash) {
2022-04-14 00:30:32 +08:00
return (typeof hash === "string" && hash.startsWith("sha1"));
2023-05-15 02:18:20 -07:00
};
2021-07-13 22:22:46 +08:00
/**
* Does the hash need to be rehashed?
* @returns {boolean}
*/
2021-07-13 22:22:46 +08:00
exports.needRehash = function (hash) {
return isSHA1(hash);
2023-05-15 02:37:32 -07:00
};