繁体   English   中英

PHP密码保护足够安全

[英]PHP password protection secure enough

在我开始之前,我想再次提出这个问题而道歉,正如许多用户所做的那样,但通过我做过的研究,我对我发现的内容并不满意。 我只希望能在这里找到一些非常有用的东西。

因为md5或sha1被认为是不好的做法(即使使用salt ???),我也试图创建这个函数来哈希我的密码

$password = $_POST['password']; // lets say that my password is: my_sercretp@ssword123
function encrypt_the_password($password){
    $salt = "lorem_ipsumd0l0rs1t@m3tc0ns3ct3tur@d1p1sc1ng3lit";
    return hash('sha256', $salt.$password);// can use also different algorithm like sha512 or whirlpool
}
$hashed_password = encrypt_the_password($password);

请注意,这个我在一个只有一个用户的个人网站上使用它,我。 如果有多个用户,我会想出这样的事情:

$password = $_POST['password'];
function generate_salt() {
    $salt = uniqid(md5("lorem_ipsumd0l0rs1t@m3tc0ns3ct3tur@d1p1sc1ng3lit".microtime()));
    $salt = hash('sha256', $salt);// can use also different algorithm like sha512 or whirlpool
    return $salt;
}
function encrypt_the_password($password,$salt){
   return hash('sha256', $salt.$password);// can use also different algorithm like sha512 or whirlpool
}
$hashed_password = encrypt_the_password($password,generate_salt());

这是否足够安全(在每种情况下)或者这可以改善更多???


我的编辑:我尝试使用crypt()函数提出一些新的东西。 这是我的代码,如果网站只有一个用户,admin:

$password = $_POST['password'];
$salt = "L0r3mIpsUmD0l0rS1tAm3t";
$hashed_password = crypt($password', '$2a$12$' . $salt); 

如果网站有多个用户:

$password = $_POST['password'];
function generate_salt() {
        $salt = uniqid(sha1("L0r3mIpsUmD0l0rS1tAm3tc0ns3CT3tur4d1p1sc1ng3lit".microtime()));
        $salt = substr(sha1($salt), 0, 22);
        return $salt;
}
$hashed_password = crypt($password', '$2a$12$' . generate_salt()); 

这样可以还是需要改进???

通过不编写自己的算法来改进它。 你的算法是不安全的,因为你的salt是常量的,你只需要使用SHA256的一次迭代进行散列,这在计算上很便宜。

相反,使用Bcrypt ,这在计算上既昂贵又由知道他们正在做什么的人验证,所以它比你的解决方案更安全。

您应该使用PHP 5.5中内置的密码函数。 ircmaxell有一个回退库,它可以提供早期版本的PHP中的函数: https//github.com/ircmaxell/password_compat

它将始终使用最新的哈希技术,以防万一甚至为您更新记录。 请务必阅读此库附带的README。

不要自己进行散列函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM