【问题标题】:Is it a good/secure way for hashing passwords PHP它是散列密码PHP的好/安全方法吗
【发布时间】:2014-04-02 07:16:12
【问题描述】:

我正在使用:

function generateHash($password) {
    if (defined("CRYPT_BLOWFISH") && CRYPT_BLOWFISH) {
        $salt = '$2y$11$' . substr(md5(uniqid(rand(), true)), 0, 22);
        return crypt($password, $salt);
    }
}

用于散列密码,但它与低于 5.3.7 的 PHP 版本不兼容。 所以我创建了这个小函数来生成和验证密码但是由于我不是安全专家,所以我想知道它是非常安全还是仍然不安全。

我的功能:

//$hash variable is also used to check weather we are creating or verifying passwords.
function password($password,$hash=FALSE){

    $salt=array("hfuiliffa","wiaelfbs","usfewl","fr6j5","gwg8","bs4$","fgthwv");//An array of salts
    if ($hash){
        foreach ($salt as $s1)
            foreach ($salt as $s2)
                if ($s1.sha1($s2.$password)===$hash)
                return true;

        return FALSE;//If we are still here means time to return false.
    }else {
        return $salt[array_rand($salt)].sha1($salt[array_rand($salt)].$password);
    }
}

【问题讨论】:

  • 如果您使用> PHP 5.5 可以使用PHP 的内置函数password_hashphp.net/manual/en/function.password-hash.php
  • 是的,但我希望我的脚本适用于几乎所有版本..
  • 我建议生成随机盐,而不是将它们放在数组中。
  • 我认为最好将密码与密码一起散列,这在随机密码的情况下是不可能的。我的意思是我不希望将密码的散列保存为盐之间的子字符串

标签: php passwords password-protection php-password-hash


【解决方案1】:

您能做的最好的事情就是使用新功能password_hash()。如您所知,有一个compatibility pack 适用于 PHP 版本 5.3.7。向上。

如果您确实需要支持较低的 PHP 版本,您可以将 crypt 参数从 "$2y$%02d$" 替换为 "$2a$%02d$",这也会生成 BCrypt 哈希。这是你可以用旧版本做的最好的事情。或者,您可以查看我主页上的example code

if (version_compare(PHP_VERSION, '5.3.7') >= 0)
  $algorithm = '$2y$%02d$'; // BCrypt, with fixed unicode problem
else
  $algorithm = '$2a$%02d$'; // BCrypt

如果您绝对需要支持 PHP 4.x,则没有 BCrypt 支持,在这种情况下,我建议使用 phpass 库。顺便说一句,您使用恒定“盐”的方案几乎没有提供任何保护。

【讨论】:

  • 非常感谢,我也想评价一下我的功能好用吗?
  • @Subhanker - 很抱歉让您失望了,但是您使用恒定“盐”的方案非常不安全。做错太多了,我建议先阅读我的教程 abour safely storing passwords
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-17
  • 2010-12-22
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
相关资源
最近更新 更多