![]() |
VOOZH | about |
When developing applications, one of the critical aspects of user authentication is ensuring that passwords are stored securely. Plain text storage of passwords is a significant security risk. Instead, passwords should be encrypted using strong hashing algorithms. In Node.js, one of the popular modules for this purpose is bcryptjs.
bcryptjs is a JavaScript implementation of the bcrypt password hashing function. It is designed to be secure and efficient, making it a suitable choice for hashing passwords in Node.js applications.
To encrypt password in Node App using bcrypt module, firstly
bcryptjs module is imported. A plain text password password is defined. A variable hashedPassword is declared to store the hashed password.bcrypt.genSalt(10, function (err, Salt) {...}) generates a salt with 10 rounds and executes a callback function with the generated salt.bcrypt.hash(password, Salt, function (err, hash) {...}) hashes the password with the generated Salt.hashedPassword and logged.bcrypt.compare(password, hashedPassword, async function (err, isMatch) {...}) compares the original password with the hashed password.Step 1: You can visit the link to Install bcryptjs module. You can install this package by using this command.
npm install bcryptjsStep 2: After installing bcryptjs module you can check your request version in the command prompt using the command.
npm version bcryptjsStep 3: After that, you can create a folder and add a file for example index.js, To run this file you need to run the following command.
node index.jsExample: Implementation to show encryption in Node.js using bcryptjs module
Step to run the application: Run the application using the following command:
node index.jsOutput: We will see the following output on the console screen.
$2a$10$4DRBPlbjKO7WuL2ndpbisOheLfgVwDlngY7t18/ZZBFNcW3HdWFGm Encrypted password is: pass123 Decrypted password is: $2a$10$4DRBPlbjKO7WuL2ndpbisOheLfgVwDlngY7t18/ZZBFNcW3HdWFGm