1
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...

KneeSkrap3r
  • 103
  • 9

1 Answers1

1

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
Ghassan Elias
  • 2,213
  • 1
  • 14
  • 17
  • Thanks for your answer, but I have verified that $password is printing 'admin', both in the controller where I am calling the blowfish() function I've built and also in that function. Even when set I am seeing different hashes... – KneeSkrap3r Aug 17 '12 at 16:36
  • but you have to find or help us help you to find why password is sent as undefined. can you post how you are calling that function? – Ghassan Elias Aug 17 '12 at 16:42