I'm currently implementing a login system. I want to store the password and the salt in a database. Now I found out that there is a hash()
and a crypt()
function which seems to do the same (valid for SHA512).
hash()
is newer and seems to support more hashing alogrithms than crypt()
. Or there any other differences I should know/care about?
Edit:
function generatePasswordHash($password){
$salt = base64_encode(mcrypt_create_iv(8));
$calculatedPasswordHash = crypt($password, '$1$' . $salt . '$');
return $calculatedPasswordHash;
}
The result looks like $1$Qh6ByGJ9$zLn3yq62egvmc9D7SzA2u.
Here my password checking function:
function checkLoginData($username, $password){
global $db;
$sql = "SELECT * FROM users WHERE username = :username";
$result = $db->ExecuteQuery($sql, array("username"=>$username));
if(!empty($result)){
$result = $result[0];
$savedPasswordHash = $result['password'];
$splitted = explode("$", $savedPasswordHash);
$salt = $splitted[2];
$calculatedPasswordHash = crypt($password, '$1$' . $salt . '$');
if($savedPasswordHash === $calculatedPasswordHash){
return true;
}
}
return false;
}
$data
parameter ofhash()
through concatenation). – Sheltermcrypt()
is the wrong way? Here are the available encryption functions. Don't see MD5, SHA1, SHA2 here ... Seems crypt with MD5 is my only option. – Sheltercrypt
function inCRYPT_MD5
mode. Not simply md5(data+salt). Single iteration md5 sucks, even with salt. – GigaCRYPT_MD5
. Thanks for clarifying that point again. See my edited question, which should do that. – Sheltermcrypt_create_iv
returns. You might need to encode it before passing it tocrypt
. It's also recommended to use at least 64 bit salts. – Gigacrypt
returns the salt as part of its result, i.e. it's unnecessary to return/store the salt again. – Giga