在阅读了半天关于正确编码密码的方法后,我不知所措,我选择了这种方式。但是我不确定这是否是最好的方法。提前致谢。
$password= "abc123";
$salt = mcrypt_create_iv(32, MCRYPT_RAND);
$this_will_be_stored_in_db= crypt($password,$salt);
echo $this_will_be_stored_in_db;
问题是 crypt() 是单向散列,这与编码不同,因此在通过该代码传递值后,您将无法获得原始值。如果散列是您想要对密码执行的操作,我建议您使用 bcrypt。这是一个如何实现它的示例:
$password = 'secretPassword';
$salt = '$2a$13$'.substr(sha1($password),0,22);
$hashed_value = substr(crypt($pass, $salt), 29);
您可以在此站点上阅读有关 bcrypt 的更多信息:http: //www.nathandavison.com/posts/view/13/php-bcrypt-hash-a-password-with-a-logical-salt