Comparing Password
In this step we compare the hashed password with the plain-text password .
const bcrypt = require("bcrypt");
const plainPassword = "gfgPassword"; //random text
const saltRounds = 10;
bcrypt.hash(plainPassword, saltRounds, function (err, hashedpassword) {
if (err) {
console.error(err);
return;
}
console.log("hashedpassword: ", hashedpassword);
bcrypt.compare(plainPassword, hashedpassword, function (err, result) {
if (result) console.log("Comparion Result : " + result);
else console.log("error " + err);
});
});
Output:
- Compare function is used to compare the plainPassword with the hashedpassword.It also contains the callback function which is used to handle the error if passwords are different.
How to install bcrypt using npm?
In the world of web development, security is paramount, especially when handling user passwords. One of the most widely used libraries for password hashing in Node.js applications is bcrypt
. This article will guide you through the process of installing bcrypt
Using npm, demonstrate how to use it for secure password hashing and comparison.