0

I'm writing a Password class and I'm using password_hash with PASSWORD_DEFAULT to create the hash.

The documentation says that password_hash could also return false if the hashing fails. In that case I'm throwing an exception and I would like to test this case carefully.

Do you have an example of a string $password such that password_hash($spassword, PASSWORD_DEFAULT) === false?

This has nothing to do with password_verify returning false when it doesn't match the hash.

10
  • @FranzGleichmann I guess that in that case is password_verify that returns false, not password_hash Commented Sep 27, 2016 at 16:38
  • oh sorry. you're right, my bad. however, the next result google gave ne contains the answer: stackoverflow.com/questions/32226295/password-hash-returns-null - it's not about an invalid pasword but an invalid algorithm Commented Sep 27, 2016 at 16:44
  • I assume that the only time password_hash could fail would be if your server is not configured properly. Commented Sep 27, 2016 at 16:45
  • 1
    I don't think there is a guaranteed way to get password_hash to return false. By definition it's for failures the Zend developers didn't think would happen. I don't really see why you would need to make PHP return false for a unit test anyway, you can just call your code for handling a false response directly. Commented Sep 27, 2016 at 17:04
  • 2
    Checking that you are correctly comparing a variable to false seems like an overly trivial unit test. If you really want to do it though the neatest thing I can suggest is wrapping password_hash in a function that can be made to return false during a test. Commented Sep 27, 2016 at 17:16

1 Answer 1

2

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:

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.