【问题标题】:Password Hash Algorithm密码哈希算法
【发布时间】:2011-10-10 17:46:25
【问题描述】:

我不太了解散列密码,但我想知道。我想知道以下算法对于没有信用卡信息或类似信息的普通网站有多好,我也想知道如何改进它。 算法是:

hash('sha512', crypt(hash('whirlpool', $password.$username), base64_encode(md5(strlen($password)))))

【问题讨论】:

  • 如何防止播放攻击?它不会阻止任何人录制和播放您的(不变的、静态的)字符串。顺便说一句,混合各种哈希(漩涡、sha、md5)是一个非常糟糕的主意。
  • 另见 Openwall 的 Portable PHP password hashing framework (PHPass)。它强化了对用户密码的一些常见攻击。

标签: php algorithm hash passwords


【解决方案1】:

不要混合多个哈希值,每个哈希值都经过优化以使其自身发挥最佳效果。

根据您使用该哈希的目的,将 $password 放入其中也是一个非常糟糕的主意。如果它被存储在用户的计算机上,就像在 cookie 中一样。你不希望它在那里。

如果您将哈希存储在数据库中,您还可以通过在使用哈希算法之前添加动态随机字符串来使其更好。然后每次访问都会为用户生成一个新的哈希。

【讨论】:

    【解决方案2】:

    我强烈建议使用well-known, tested, vetted hash/crypt function 而不是任何本土算法。

    【讨论】:

      【解决方案3】:

      这是我创建的一个类,用于存储我集成的 api 的 ID/密码组合。每个用户都可以拥有自己唯一的凭据。我不建议在不符合 PCI 的计算机上存储任何信用卡数据。

      这是我的确切课程,但你有一些缺失的部分,所以我已经评论了这些。请注意,向量是唯一的(将其视为哈希),我将其与加密数据一起存储在数据库中。

      密钥不在公共目录中,这将涉及保护您的盒子的另一个主题。

      <?php
      // This is on my index page but added here so you see all constants.
      define('DIR', dirname(__FILE__) . '/');
      
      
      class locker {
        private $algorithm = MCRYPT_RIJNDAEL_256;
        private $key;
        private $mode = MCRYPT_MODE_CBC;
        public $iv;  // Public so we can change to the one used to encrypt it.
      
        public function __construct()
        {
          // Lets include our key
          // The key is located Outside of the public directory.
          $this->key = file_get_contents(DIR .'../keys/passphrase.key');
          // Create the initialization vector for added security.
          $this->iv = mcrypt_create_iv(mcrypt_get_iv_size($this->algorithm, MCRYPT_MODE_ECB), MCRYPT_RAND);
        }
      
        public function encrypt($string)
        {
          return base64_encode(mcrypt_encrypt($this->algorithm, $this->key, base64_encode($string), $this->mode, $this->iv));
        }
      
        public function decrypt($string)
        {
          return base64_decode(mcrypt_decrypt($this->algorithm, $this->key, base64_decode($string), $this->mode, $this->iv));
        }
      
        // Helper functions so you can see what you can do on your own box.
        public function list_modes()
        {
          print_r(mcrypt_list_modes());
        }
      
        public function list_algorithms()
        {
          print_r(mcrpt_list_algorithms());
        }
      }
      ?>
      
      <?php
      //Example usage
      $locker = new locker;
      $pass = $locker->encrypt('passwordvalue');
      $iv = $locker->iv;
      
      // Decrypt it
      $locker = new locker;
      $locker->iv = $iv;
      $pass = $locker->decrypt($pass);
      ?>
      

      【讨论】:

        【解决方案4】:

        如果你想要强大的东西。你必须 - 永远不要保存密码,而是使用哈希(不需要太多)以避免 db hack 使用密码。

        • 永远不要询问密码,而是要密码哈希+盐的哈希(例如日期)以避免播放攻击

        【讨论】:

          【解决方案5】:

          试试我写的这个(非常易于使用)类,它包括自动算法检测,以获得您的服务器支持的最安全的算法:http://netentwicklung.wordpress.com/2012/06/17/87/

          【讨论】:

            猜你喜欢
            • 2020-09-16
            • 2012-05-24
            • 2018-10-31
            • 1970-01-01
            • 2012-07-07
            • 1970-01-01
            • 2021-02-09
            • 1970-01-01
            相关资源
            最近更新 更多