0
0
mirror of https://github.com/louislam/uptime-kuma.git synced 2024-11-22 11:47:30 +01:00
uptime-kuma/server/password-hash.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

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