正确使用地穴?

时间:2012-08-08 23:34:28

标签: php hash passwords

因此,在研究了这一点后,我想知道这是不是最好的做法。

当我将用户密码发送到DB时,我正在执行此操作:

// DB input:

$mySalt = time(); // generate random salt such as a timestamp
$password = crypt($_POST['password'], $mySalt);
// submit $password and $mySalt to DB here via PDO

当我在登录时检查密码时,我正在这样做:

// At login:

// retrieve the password and the salt from the DB
if(crypt($_POST['password'], $saltFromDb) === $passFromDb)
// allow login

这是正确的方法,还是我错过了什么?谢谢你的任何建议。

1 个答案:

答案 0 :(得分:2)

您需要的是使用crypt中提供的内置salting和散列函数。下面是一个使用好散列算法调用blowfish(bcrypt)的示例:How do you use bcrypt for hashing passwords in PHP?

在这种情况下,算法越慢越好。

从DB获取它时,您只需使用crypt()来评估整个字符串,以了解它是否验证为正确的密码等。