【问题标题】:Is this hashing function overkill这个散列函数是否矫枉过正
【发布时间】:2012-06-11 13:10:04
【问题描述】:

我最近开始了一个项目,它包含以下哈希密码的函数:

function hash_password($password) {
    $account_id = $this->account_id;

    /*
     * Cook up some randomness
     */
    $password = str_rot13($password);

    $random_chars = "1%#)(d%6^".md5($password)."&H1%#)(d%6^&HB(D{}*&$#@$@FEFWB".md5($password)."``~~+_+_O(Ed##fvdfgRG:B>";

    $salt = $account_id;
    $salt = ((int)$salt * 123456789) * 1000;

    $salt_len     = strlen($salt);

    for($i=0; $i <= $salt_len; $i++) {
        $salt .= $random_chars[$i];            
    }

    $salt = str_repeat($salt, 3);

    return hash('sha256', base64_encode($password.$salt.$password), false);
}

*$account_id 对于每个用户帐户都是唯一的。

我的问题是:这个功能是否比做一些简单的事情更安全:

$salt = sha1($account_id);
$hash = hash('sha256', base64_encode($password.$salt), false);

干杯!

【问题讨论】:

  • 你使用什么散列算法并不重要,只要你正确地加盐。即使是 SHA1 也可以使用良好的盐非常安全。
  • Is this function any more secure - 是的,但是Is this hashing function overkill - 绝对是的。您正在使用 大量 的 CPU 周期来对密码进行哈希处理 - 每次 您处理登录或用户更改密码时都需要这样做 [, 或。 ..] 而您应该更关注的是(如果他们那么敏感的话)是防止人们首先获得散列密码。
  • 实际上它可能不太安全,因为熵要高得多 - 看到这个grc.com/haystack.htm - 你应该使用bcrypt 无论如何

标签: php hash passwords password-protection sha


【解决方案1】:

将帐户 ID 用作盐可能不是一个好主意 - 如果有人可以窃取您的散列密码,那么他们可能也可以获得帐户 ID。因此,在这种情况下,如果代码也受到很好的保护,那么在代码中使用更复杂的哈希可能会更安全。在代码中使用已知的随机字符串作为盐意味着有人必须同时破解您的数据和代码才能攻击密码——这比仅仅攻击数据库要好。

【讨论】:

    猜你喜欢
    • 2015-03-25
    • 1970-01-01
    • 2017-05-03
    • 2013-01-10
    • 2012-05-24
    • 2014-10-31
    • 1970-01-01
    • 2011-03-10
    • 2016-09-10
    相关资源
    最近更新 更多