I am trying to use SHA512 algorithm in PHP using function crypt.
My salt:
$salt = base64_encode(substr(str_shuffle("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0, 12));
I get something like this:
Q4CALzJNenFaZnNK
I am not sure why I get lenght 16 while I specified 12.
And to hash the password, I use this:
$hashed = crypt('myPassword', '$6$rounds=5000000$'.$salt);
The output is something like that:
$6$rounds=5000000$Q4CALzJNenFaZnNK$9QTP6C.BZ9Z.U85UIEAVX1dEIdShHFoYGgTMvgv9Cx/XZY1mK/n2rY4FuHSoigjgIXfqGZftZSxrrF.cDBzt8/
Lenght: 121
So my question is it ok to store this password in the database or should I strip $ signs as I saw in few examples?
Also I already store passwords in VARCHAR(255) and I was wondering if I could make the output twice as long, i.e. near 255 characters?
Is this way more secure than for instance Blowfish?
My findings:
The length of a hashed password is not that important as I first thought (60 characters is well enough to store instead of 128 or 256).
It is best to use password_hash function and forget about generating your own salt - php.net know what they do.
So I ended up hashing passwords this way:
$hash = password_hash($password, PASSWORD_BCRYPT, array("cost"=>15));
PASSWORD_BCRYPT is Blowfish algorith with the default cost of 10 (times it runs the algorithm or something). 10 is a good number to slow down the brute force attacks. I wanted to show how you can change the cost manually.