1
votes
public static function blowfish($password, $storedpass = false) {
    //if encrypted data is passed, check it against input ($info) 
      if ($storedpass) { 
            if (substr($storedpass, 0, 60) == crypt($password, "$2a$08$".substr($storedpass, 60))) { 
                return true; 
            }  else { 
                return false; 
            } 
      }  else { 
            //make a salt and hash it with input, and add salt to end 
            $salt = "143cd669b02e155c3cca6e";//substr(bin2hex(openssl_random_pseudo_bytes(22)), 0, 22);
            //for ($i = 0; $i < 22; $i++) { 
                //$salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mt_rand(0, 63), 1); 
            //} 

            //return 82 char string (60 char hash & 22 char salt) 
            return crypt($password, "$2a$08$".$salt).$salt; 
     }
}

print(substr($storedpass, 0, 60)."<br />");
print(crypt($password, "$2a$08$".substr($storedpass, 60))."<br />");
print(substr($storedpass, 60));

Produces the result:

$2a$08$143cd669b02e155c3cca6eM3k8s9BdE4jErJXJ8wSxshJDPcJQVPW
$2a$08$143cd669b02e155c3cca6eEiYm6ilW1ZC1PBS07LOh2XSq1NODSKK
143cd669b02e155c3cca6e

You can see I was previously generating a random salt of 22 characters, and I know all about PHPASS, that mt_rand() is not a CSPRNG, etc etc. What confuses/concerns me is simply why crypt() (given $password = 'admin') generates a different hash even using a static salt. You can see I've printed the substr($storedpass, 60) which generates the proper salt, but then running the crypt() function (with the same parameters to create the initial $storedpass) it generates a different result, breaking authentication for a (relatively small and not mission-critical) application of mine...

1
It's recommended to just pass the entire hash in as the salt: PHP will take care of substrings and such for you.Waleed Khan
How does that value 60 relate to the salt? Why don't you use constants?Maarten Bodewes
@DarkXphenomenon that post has a lot of meaty goodness, but still doesn't answer the question.KneeSkrap3r

1 Answers

1
votes

it seems that you are sending the $password argument to the function as (undefined).

that would generate this hash:

$2a$08$143cd669b02e155c3cca6eM3k8s9BdE4jErJXJ8wSxshJDPcJQVPW

but (for example) if you run this:

$password = 'admin';
echo $storedpass = blowfish($password)."<br />";

print(substr($storedpass, 0, 60)."<br />");
print(crypt('admin', '$2a$08$'.substr($storedpass, 60))."<br />");
print(substr($storedpass, 60));

if (blowfish($password, $storedpass) == true) {
    echo 1;
}

that would output:

$2a$08$143cd669b02e155c3cca6eEiYm6ilW1ZC1PBS07LOh2XSq1NODSKK143cd669b02e155c3cca6e
$2a$08$143cd669b02e155c3cca6eEiYm6ilW1ZC1PBS07LOh2XSq1NODSKK
$2a$08$143cd669b02e155c3cca6eEiYm6ilW1ZC1PBS07LOh2XSq1NODSKK
143cd669b02e155c3cca6e
1