Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs - how to compare two hashes password of bcrypt

Hello I need some help with this issue after I search the solution and I have not found yet,

I want to compare 2 hash password with bcrypt of the same password, how do I do it?

for example:

I have these 2 hash password that came from the same password in bcrypt:

var password = E@Js#07Do=U$
var hash1 = $2a$10$fKAyjaG0pCkisZfRpKsBxursD6QigXQpm1TaPBDZ4KhIZRguYPKHe
var hash2 = $2a$10$mgApOcRIp7RSK3lRIIlQ5e/GjVFbxAFytGAEc0Bo17..r8v2pPR22
// that's not working for me
bcrypt.compare(passwordHash, userPasswordLoginHash, function(err, isMatch) {
   if (err) throw err;
   if(isMatch){
      console.log('correct password!')
   }
   callback(null, isMatch);
});

how can i compare them, to determine that they came from the same password, by using bcryptjs npm package?

like image 230
hadar Avatar asked Apr 22 '19 22:04

hadar


People also ask

How to hash and compare passwords in node?

The bcrypt library on NPM makes it really easy to hash and compare passwords in Node. If you're coming from a PHP background, these are roughly equivalent to password_hash () and password_verify ().

What is the use of @bcrypt in Node JS?

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.

How to hash passwords with bcrypt in JavaScript?

The bcrypt library makes the process easy by providing you with methods to hash and compare passwords. To start using the library, you need to install it with your package manager: npm install bcrypt # or yarn add bcrypt Then include the module to your JavaScript code with require:

How to compare a bcrypt hash with a plaintext password?

Instead, the docs for bcrypt says the to compare all you have to do is bcrypt.compare (myPlaintextPassword, hash, function (err, res) {...}) without any indication to the salt, but instead referring to the plainTextPassword. I don't understand how this works, if the hash has been created using a salt.


3 Answers

This is impossible by design - as a core security property of true password hashing.

If you could compare two password hashes without knowing the original password, then if an attacker cracked one password on the system, they would instantly know the passwords of all users who are using that password, without any additional work. It should be immediately obvious why this would be a bad thing.

For example, if passwords were stored using a hash inappropriate for password storage (such as MD5), then if 50 users had a password of 'password', then all of their hashed passwords would have the identical MD5 hash ('5f4dcc3b5aa765d61d8327deb882cf99'), and cracking one of them would reveal every user's password.

You can't do that with a modern password hash like bcrypt. The only way to "compare" two modern password hashes is to know the plaintext in advance, and then apply the algorithm using the salt in each hash. And even if two users have the same password, the attacker has to perform the same expensive computation to crack each of them independently, because the unique salts make each hash unique.

More generally - and this may sound a bit bold - but there is no legitimate use case for any system or administrator to ever compare two different users' passwords. User passwords should be 100% independent and 100% opaque to the system once stored. If a system or business case requires this kind of comparison, it should be redesigned to eliminate that requirement.

like image 78
Royce Williams Avatar answered Oct 18 '22 02:10

Royce Williams


"With bcrypt lib you compare plain text password to the one hashed using the same lib."

The problem is with a micro services architecture, that is very insecure. If I have a front end passing an unhashed password to the backend, the unhashed password is getting logged (possibly in multiple places) before it gets compared against the hash in the DB on the system backend.

like image 5
TLL Avatar answered Oct 18 '22 03:10

TLL


With bcrypt lib you compare plain text password to the one hashed using the same lib.

Say you hashed a password

const myPlaintextPassword = 'E@Js#07Do=U$'
bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) {
  // Store hash in your password DB.
  // example output, taking your hash
  // hash = $2a$10$fKAyjaG0pCkisZfRpKsBxursD6QigXQpm1TaPBDZ4KhIZRguYPKHe
});

You compare like:

// db query, get hashed password, found hash
// hash = $2a$10$fKAyjaG0pCkisZfRpKsBxursD6QigXQpm1TaPBDZ4KhIZRguYPKHe
// User input again:
const myPlaintextPassword = 'E@Js#07Do=U$'
bcrypt.compare(myPlaintextPassword, hash, function(err, res) {
  // res is true as the original password is the same
  // res == true
});
like image 4
1565986223 Avatar answered Oct 18 '22 04:10

1565986223