Password Hashing and Verify in NodeJS using Bcrypt

  530 views   7 months ago NodeJS

In this article, we will share with you how to hashing the password and compare password string with hashing password string help of bcrypt. bcrypt library provides you with making a password in a hash string and normal string compares with hashing string in node.js applications.

Nodejs provides crypto modules to perform the encryption and hashing of sensitive information such as passwords. The Bcrypt node modules provides an easy way to create and compare hashes.

bcrypt the module provides both synchronous and asynchronous methods for work with any string make hashing and any normal string compare with already hashsing formate. so, it will help lots in our node.js application current password check with already store hashed password in our database.

Examples

const bcrypt = require('bcrypt');

bcrypt.hash('Your_password', 10, function(err, hash) {
	console.log(hash);
	// output will be
	// $2b$10$wV1ndcFMmu/6Ue4Tuy2OqeSIEQKrjnYMlBCMOG66nBnWk2TUFGDL.
});

bcrypt.compare('Your_password', '$2b$10$wV1ndcFMmu/6Ue4Tuy2OqeSIEQKrjnYMlBCMOG66nBnWk2TUFGDL.', function(err, res) {
	if(res) {
		console.log('Your password mached with database hash password');
	} else {
		console.log('Your password not mached.');
	}
});

Conclusion

As you can see, password hashing and normal password string compare with existing hashing string it is very easy to use or check the help of bcrypt library.

We hope that small tutorials help everyone.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]