Nodejs bcrypt is used to store the password in an encrypted format with hash and salt.
How to install bcrypt package in Nodejs?
Firstly installed bcrypt package through below command.
npm install --save bcrypt
How to generate password in encrypted format through bcrypt?
Bcrypt is used to generated secure passwords.
Suppose, You create generate_hash_password.js file and write the below code.
const bcrypt = require('bcrypt'); var password="Hello123" const saltRounds = 10; //write number var hash_password = bcrypt.hashSync(password, saltRounds); //Password must be save into database console.log("Hash Password:-",hash_password);
Note:- firstly include bcrypt package through require method after that write the hashSync method of bcrypt.
How to compare encrypted password?
Firstly create the compare_password.js file and write the below code.
const bcrypt = require('bcrypt'); const saltRounds = 10; var password='Hello123'; var hash_password = '$2b$10$jgXSMwBDsysCirC5xMGYwue0kv7Q00H9.pGBcag6Awrkeb1Pu2o6.'; //which is coming from database let encrypt_pwd = hash_password; let check_password = bcrypt.compareSync(password, encrypt_pwd); console.log("Password is:-",check_password);
Note:- compareSync method has two parameters
1) raw password
2) encrypted password