I developed my site using XAMPP with php 5.5 installed. I just realize that my host only has php 5.4 (cannot update to 5.5 yet). My problem is that I cannot use the new php 5.5 password_hash()
feature. Is there an equivalent method for hashing with salt for php 5.4?
Is there a way to get this equivalent code (below) to work in php 5.4?
$options = [
'salt' => uniqid(mt_rand(), true),
'cost' => 12
];
$hash = password_hash($mypassword, PASSWORD_DEFAULT, $options);
When I was using PHP 5.4 I used php's crypt function with CRYPT_BLOWFISH hash type. I played a bit with parameters to match your case, and concluded that your
blowfish Pre
should be$2y$12$
to match yourcost = 12
parameter.You can run this script in your command line to verify the result matches (I will keep your insecure mt_rand salt in this part. More on this later)
For example
outputs
@JohnConde's answer is better because is safer using a well tested library. I just wanted to see what was going below the trunk.
PD: You asked
Now, your code has a major weakness. You are generating salts with mt_rand. Don't ever do that. Trust the system with the salt generation and, if you really want to generate it yourself, use something like the following salt generator (I used thisone when my apps were PHP 5.4)
To be honest I was sure the salt had 18 characters, but looking at my old code, it's done with 21 :)
Use password_compat. It's a backward compatible library to emulate
password_hash()
in older versions of PHP (5.3.7+).