Node.js - How to hashing and compare password using bcrypt.

  8 months ago     Views:  987  
Cole Bartell
1 Follows


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.

Also check : How to check current password with hash password in laravel

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. if you have known more ways to how to hashing and compare the password in node.js or any issues or questions related to these tutorials so please comment below. Thanks.


Was this article helpful?

1 out of 1 person found this article helpful.

Leave a comment

OR

No Comments