3

As the bcrypt documentation addresses, in order to compare a hash to a plain text we must implement the compare function like so:

bcrypt.compare(myPlaintextPassword, hash).then(function(result) { //do stuff });

but there seems to be no way to tell the function the rounds of salting. How does the functions gets that number?

asajadi84
  • 410
  • 5
  • 20

2 Answers2

5

The bcrypt output looks like this: $2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy It specifies a cost parameter of 10, indicating 2^10 key expansion rounds. The salt is N9qo8uLOickgx2ZMRZoMye and the password hash is IjZAgcfl7p92ldGxad68LJZdL17lhWy.

So all information that's needed, is input in the hash parameter.

Frederick Behrends
  • 3,075
  • 23
  • 47
2

The prefix "$2a$" or "$2b$" (or "$2y$") in a hash string in a shadow password file indicates that hash string is a bcrypt hash in modular crypt format. The rest of the hash string includes the cost parameter, a 128-bit salt (Radix-64 encoded as 22 characters), and 184 bits of the resulting hash value (Radix-64 encoded as 31 characters).The Radix-64 encoding uses the unix/crypt alphabet, and is not 'standard' Base-64. The cost parameter specifies a key expansion iteration count as a power of two, which is an input to the crypt algorithm.

For example, the shadow password record $2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy specifies a cost parameter of 10, indicating 2^10 key expansion rounds. The salt is N9qo8uLOickgx2ZMRZoMye and the resulting hash is IjZAgcfl7p92ldGxad68LJZdL17lhWy. Per standard practice, the user's password itself is not stored.

There is a whole wikipedia entry for this: https://en.wikipedia.org/wiki/Bcrypt

bill.gates
  • 14,145
  • 3
  • 19
  • 47