I think I'm missing something critical here. In the CPasswordHelper::hashPassword
function we have lines:
$salt=self::generateSalt($cost);
$hash=crypt($password,$salt);
return $hash;
And in the CPasswordHelper::verifyPassword
there is this line:
$test=crypt($password,$hash);
return self::same($test, $hash);
What about the salt? To my understanding its not even beeing kept, but it doesn't make any sense, so I'm guessing I didn't understand it completely.
CPasswordHelper works like PHP's functions password_hash() and password_verify(), they are wrappers around the crypt() function. When you generate a BCrypt hash, you will get a string of 60 characters, containing the salt.
The variable $hashToStoreInDb will now contain a hash-value like this:
The salt you can find after the third
$
, it is generated automatically by password_hash() using the random source of the operating system. Because the salt is included in the resulting string, the function password_verify(), or actually the wrapped crypt function, can extract it from there, and can calculate a hash with the same salt (and the same cost factor). Those two hashes are then comparable.The salt is being stored as part of the hash.