You might be interested in viewing this post on the pros and cons of "double hashing" a password and this FAQ on password hashing.
Hashing with some algorithm al
a password, and re-hashing with the al
algorithm the hash result is more likely to reduce password entropy. This means that different passwords might give the same hash when processed by hash_password($password)
.
Instead of implementing your own hashing functions, you should use the native password API whenever possible (starting from PHP 5.5 onwards). Its key function, password_hash
, generates a strong salt to be used with the hash and is secure against most brute-force attacks.
EDIT: you might want to give more entropy (= randomness) to your hashed passwords by computing the SHA-512 value of the password + a random salt, and then password_hash
ing this hash. The final PHP code would look like this:
function generateSalt() {
// Cryptographically secure function to generate strings
return random_bytes(64 /* use 64 bytes */);
}
/**
* Generates a strong SHA-512 hash, for use by passwordSecureHash()
* @param string $password the plain-text password
* @param string $salt the salt to generate the hash, to be kept at ALL COSTS - this function will not do it for you!
* @return string the computed hash.
*/
function sha512(string $password, string $salt) {
return hash('sha512', $password . $salt);
}
/**
* Returns the final, secure password hash for storage in database server.
* @param string $hash the SHA-512 resulting from the sha512() call.
* @return string the hashed password.
*/
function passwordSecureHash(string $hash) {
if(strlen($hash) !== 128) {
// Ensure the SHA-512 hash has 128 characters
return FALSE;
}
// Always let PHP generate the final password salt for you
return password_hash($hash);
}