Hashing of Password in Node.js with Bcrypt JS | Authentication & Security

 

hashing of password

Hashing of password is the common approach to secure our authentication, So today we will learn how we can hash password in Node.js with Bcrypt.

Hashing of Password in Node.js

Introduction - Hashing of Password 

Hashing of password is a common approach to storing passwords securely. A hash is a one-way function that generates a representation of the password. So when a user signs up for an account and they choose a password, the password is stored as the generated hash,  rather than the actual characters that the user typed in. When you run a password through a hashing function, it will always produce the same output. When the user tries to login with their email and password, the entered password is hashed again and then compared to what is stored in the database. If the two hashes are the same, the user has entered the correct password.

Hashing Library

For hashing of password we will be using bcrypt,js library in Node.js from NPM. (Node Package Manager) .

First you need to install bcrypt.js library in your project. Run the blow code in your terminal.

npm i bcrypt


Usage

Then we need to import bcrypt.js.

const bcrypt = require('bcrypt');

Hashing the password

const myFunction = async () => {
    const password = 'myPassword';
    const hashingPassword =
        await bcrypt.hash(password , 9);
}


Now, you can store the hashingPassword into the database.

[Meanings of underlined words in the above code]

1. Password is the user password

2. Here you can select any number if you want, but 9 is the perfect number to encrypt the password. If you select a small number than 9 then it will be crackable or if you choose a number which is bigger than 9, it will take time to render.

Verify the password

To verify our password, bcrypt has a method called compare.

const isMatch = bcrypt.compare ( 
    'myPassword' , hashedPassword
);

// 'myPassword'  is the user password.
// hashedPassword is the hashed password from the database.

if (isMatch){
    console.log('Logged in');
} else {
    console.log('Incorrect Password');
}


Learn more on Hashing of Password in Node.js



Conclusion

So in this way you can easily perform hashing of password in Node.js with bcrypt js. To get more coding tips tricks and guide follow The Info Docx.

Reference / Source - Poor Coders

hashing password php, password hashing and salting, hashing password in java, what is hashing encryption, what are the advantages of hashing passwords, password hashing competition, what is password hashing and salting, how password hashing works, what is difference between encryption and hashing, hashing the password, java password hashing library, can hashed passwords be decrypted, password hashing best practices, password hashing php 7, password hashing digest, hashing password in login, hashing password sha, how safe is hashing password, hashing password access, password hashing example bcrypt, password hashing definition, hashing password android studio, hashing password compare, hashing password javascript, password hashing javascript library, hashing password scrypt, hashing password jpa, hashing password yii2, best password hashing 2020, hash password example, hashed password to plain text, what is password hashing, how to get hash password, password hasher, hash password converter to text online

Post a Comment

Previous Post Next Post