Even it's quite old, but I had the very same question and dove a little depeer into. So I share my sparse results here:
- the linked thread has actually a different topic: "when does
password_hash return null". That's the case when it can't handle stuff in the $options-parameter (like ("cost" => -1) or whatever. But it's about null, not false.
- I tried various exotic strings (raw bytes, very very long strings, null-bytes etc.= but never got a
false neither with for PASSWORD_DEFAULT, not with PASSWORD_BCRYPT in 5.5, 5.6 and 7.4.
- Compability-Libs for older PHP-Versions (without native
password-hash-function) may return false:
- Looking at the source code of PHP:
Conclusion:
I think it's best to catch the false case (and thus the null-case as well, although it can not happen if no $options provided), may it be as unlikely as it is. This is maybe the most crucial part of your application! Especially when using PASSWORD_DEFAULT, since it may change over time and therefore it's behavior, or older versions of PHP are allowed for your application. I would throw a fatal error here or even die out, since as far I can see, the false-case can only happen due to serious misconfiguration of the server. Therefore it can not be tested in unit-tests.
$hash = password_hash($password, PASSWORD_DEFAULT);
if (!$hash) {
trigger_error("Fatal error when encrypting the password", E_USER_ERROR);
}
password_verifythat returnsfalse, notpassword_hashpassword_hashcould fail would be if your server is not configured properly.falseseems like an overly trivial unit test. If you really want to do it though the neatest thing I can suggest is wrappingpassword_hashin a function that can be made to return false during a test.