我試圖創建一個哈希函數,給定一個隨機鹽,從它生成密碼哈希。Crypt函數爲相同的密碼生成相同的散列
問題是,如果我爲兩個不同的用戶輸入相同的密碼,則生成的散列對於兩者都是相同的。
Waht可能是問題嗎?
public function generateSalt()
{
return $salt = substr(sha1(uniqid(rand(), true)), 0, 32);
}
public function pwdEncrypt($password, $salt)
{
$hash = crypt($password, '$2a$' . $salt . '$');
return $hash;
}
public function registerUser($nome, $email, $password, $permitions, $active)
{
$this->nome = $nome;
$this->email = $email;
$salt = $this->generateSalt();
$this->password = $this->pwdEncrypt($password, $salt);
//INSERT METHODS BELOW
}
發現向我們展示瞭如何調用' pwdEncript()'。順便說一下,正確的拼寫是「加密」 – ThiefMaster 2012-07-27 13:40:45
@ThiefMaster感謝您的通知。我已經使用你問的信息編輯了帖子 – 2012-07-27 13:43:02
確保你實際上傳遞了$ password和$ salt,可能沒有值傳遞給這些變量,你得到的只是'crypt()' $ 2a $'和'$' – Austin 2012-07-27 13:44:07