0

The following code snippet should not emit 'MATCHED' because the password 'testtest' does not match 'testtesttest', but does on PHP 7.4.3 for me. Am I doing something wrong?

<?php
$sPass = 'testtesttest';
$sSalt = hash('sha256','this is my salt');
$sShadow = password_hash($sSalt . $sPass,PASSWORD_BCRYPT);
echo (password_verify($sSalt . 'testtest',$sShadow) ? 'MATCHED' : 'nomatch');

Note, if you remove the salt references above, the code works fine. It's like the password_hash and password_verify functions of PHP have a size limitation where they no longer become accurate if the string is longer than so many characters.

So, I'm thinking this is a bug.

Volomike
  • 23,743
  • 21
  • 113
  • 209
  • Related: https://stackoverflow.com/q/21479655/2943403 , https://stackoverflow.com/q/39175224/2943403 – mickmackusa May 21 '22 at 10:12
  • 1
    Sorry, I misinterpreted your question. I thought you meant that `PHP 7.4.3` specifically was failing, but other versions didn't. I expected an outlying result for that version versus other versions. I didn't even check if it was the incorrect result for ALL versions. I'll delete my comment. – mickmackusa May 22 '22 at 01:49
  • Versions 8.1.0 - 8.1.15, and 8.0.0 - 8.0.27 and 8.2.0 - 8.2.2 are affected by CVE-2023-0567 / CWE-330. See https://bugs.php.net/bug.php?id=81744 – MattBianco Feb 15 '23 at 07:25

1 Answers1

3

BCrypt can only handle 72 characters, your salt takes up 64 characters, so only 8 characters of your password are considered.

The input to the bcrypt function is the password string (up to 72 bytes), a numeric cost, and a 16-byte (128-bit) salt value.

Use the binary form of your salt to not "waste" as many characters or just don't use one at all, as password_hash will generate one anyway.

tkausl
  • 13,686
  • 2
  • 33
  • 50