I'm basically using this:
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./'; $numChars = strlen($chars); $salt = '$2a$12$'; for($i = 0; $i < 22; ++$i) { $salt .= $chars[mt_rand(0, $numChars - 1)]; }
Is it okay to use that?
I'm basically using this:
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./'; $numChars = strlen($chars); $salt = '$2a$12$'; for($i = 0; $i < 22; ++$i) { $salt .= $chars[mt_rand(0, $numChars - 1)]; }
Is it okay to use that?
For PHP version 5.3.7 or higher I belive this is the best:
$blowfish_salt = "$2y$10$".bin2hex(openssl_random_pseudo_bytes(22));
For PHP version 5.5 or higher just use the new password_hash()
function with automatic salt creation.
That's good to use. You working too hard on randomizing your salt though.
You could always do something shady like this $salt = md5(mt_rand()) :)
Just use something like this when you want to save the password on your next step.
$encryptedPassword = crypt($userPassword, $salt);
This should default to blowfish if you have an up to date version of PHP
I don't know whether it's okay to use, but since mt_rand
is based on system time it's predictable. It'd be better to use a superior random generation algorithm like openssl_random_pseudo_bytes
or the /dev/random
utility if it's available.
With Blowfish you only need 21 chars for the salt the rest is forgotten.
This part $2a$12$
is not a salt its the algorithm and cost(iterations of hash).
Your salt can be made simply by sha1 and then return the first 21 chars:
$salt = substr(sha1($_SERVER['HTTP_HOST'].uniqid().microtime(true)),0,21);
So something like:
$algo = '$2a$12$'; //Keep this safe
//store along side hash as the salt, for future compares
$salt = substr(sha1($_SERVER['HTTP_HOST'].uniqid().microtime(true)),0,21);
$hash = crypt('The string to be hashed', $algo.$salt.'$');