Comparing a Password
To compare a plain-text password with a hashed password, you can use the bcrypt.compare() function.
const bcrypt = require("bcrypt");
const hashedPassword =
"$2b$10$iGg/9uZhbLVhl.BkFnfNoO0OGnLuweX.URICnzXIePPz5uCFrj7uu";
const plainPassword = "gfg1122";
bcrypt.compare(plainPassword, hashedPassword, function (err, result) {
if (err) {
console.error(err);
return;
}
if (result) {
console.log("Password is correct!");
} else {
console.log("Password is incorrect!");
}
});
In this example, plainPassword is the plain-text password that you want to compare, and hashedPassword is the hashed password retrieved from your database. The bcrypt.compare() function compares the plain-text password with the hashed password and returns a boolean value indicating whether the passwords matches or not.
NPM bcrypt
bcrypt is a popular npm package used for password hashing. It utilizes the bcrypt hashing algorithm, which is designed to be slow and computationally intensive, making it resistant to brute-force attacks even with the increasing computational power of modern hardware.