Password Encryption in Node.js using bcryptjs Module - GeeksforGeeks (2024)

Last Updated : 12 Jun, 2024

Summarize

Comments

Improve

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.

What 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.

Key Features

  • Security: Uses a computationally intensive hashing algorithm to make brute-force attacks difficult.
  • Salting: Adds a unique salt to each password to ensure that even if two users have the same password, their hashes will be different.
  • Cross-Platform: Works across different operating systems and platforms.

Approach

To encrypt password in Node App using bcrypt module, firstly

  • The 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.
  • Inside the salt generation callback, bcrypt.hash(password, Salt, function (err, hash) {...}) hashes the password with the generated Salt.
  • If an error occurs, an error message is logged. If successful, the hashed password is stored in hashedPassword and logged.
  • bcrypt.compare(password, hashedPassword, async function (err, isMatch) {...}) compares the original password with the hashed password.
  • If they match, logs indicate successful encryption and matching. If they don’t match, an error message is logged.

Steps to Set Up Node Project and Implement bcrypt

Step 1: You can visit the link to Install bcryptjs module. You can install this package by using this command.

npm install bcryptjs

Step 2: After installing bcryptjs module you can check your request version in the command prompt using the command.

npm version bcryptjs

Step 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.js

Example: Implementation to show encryption in Node.js using bcryptjs module

Node
// Filename - index.js// Requiring moduleconst bcrypt = require('bcryptjs');const password = 'pass123';const hashedPassword;// Encryption of the string passwordbcrypt.genSalt(10, function (err, Salt) { // The bcrypt is used for encrypting password. bcrypt.hash(password, Salt, function (err, hash) { if (err) { return console.log('Cannot encrypt'); } hashedPassword = hash; console.log(hash); bcrypt.compare(password, hashedPassword, async function (err, isMatch) { // Comparing the original password to // encrypted password if (isMatch) { console.log('Encrypted password is: ', password); console.log('Decrypted password is: ', hashedPassword); } if (!isMatch) { // If password doesn't match the following // message will be sent console.log(hashedPassword + ' is not encryption of ' + password); } }) })})

Step to run the application: Run the application using the following command:

node index.js

Output: 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



Password Encryption in Node.js using bcryptjs Module - GeeksforGeeks (2)

Improve

Please Login to comment...

Password Encryption in Node.js using bcryptjs Module - GeeksforGeeks (2024)
Top Articles
What Comenity Bank Credit Cards Are Easy to Get?
10 Times Sasuke Was Actually Stronger Than Naruto
Bleak Faith: Forsaken – im Test (PS5)
Fat Hog Prices Today
Was ist ein Crawler? | Finde es jetzt raus! | OMT-Lexikon
Danielle Moodie-Mills Net Worth
What is Mercantilism?
Practical Magic 123Movies
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Words From Cactusi
Clafi Arab
Jasmine
Boat Jumping Female Otezla Commercial Actress
3656 Curlew St
How To Delete Bravodate Account
What Is A Good Estimate For 380 Of 60
Readyset Ochsner.org
Theycallmemissblue
Cooktopcove Com
Jack Daniels Pop Tarts
What Time Chase Close Saturday
National Office Liquidators Llc
Fdny Business
WEB.DE Apps zum mailen auf dem SmartPhone, für Ihren Browser und Computer.
Program Logistics and Property Manager - Baghdad, Iraq
Dragonvale Valor Dragon
Ac-15 Gungeon
Like Some Annoyed Drivers Wsj Crossword
Thick Ebony Trans
Galaxy Fold 4 im Test: Kauftipp trotz Nachfolger?
Netwerk van %naam%, analyse van %nb_relaties% relaties
Meridian Owners Forum
Bj's Tires Near Me
Rays Salary Cap
Morlan Chevrolet Sikeston
Metro 72 Hour Extension 2022
Aveda Caramel Toner Formula
دانلود سریال خاندان اژدها دیجی موویز
Priscilla 2023 Showtimes Near Consolidated Theatres Ward With Titan Luxe
Mckinley rugzak - Mode accessoires kopen? Ruime keuze
Wisconsin Women's Volleyball Team Leaked Pictures
Noaa Marine Weather Forecast By Zone
Weekly Math Review Q2 7 Answer Key
Paul Shelesh
Rescare Training Online
Victoria Vesce Playboy
City Of Irving Tx Jail In-Custody List
The Jazz Scene: Queen Clarinet: Interview with Doreen Ketchens – International Clarinet Association
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Minecraft: Piglin Trade List (What Can You Get & How)
Plasma Donation Greensburg Pa
Uncle Pete's Wheeling Wv Menu
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5890

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.