密码哈希算法

时间:2011-07-22 02:52:38

标签: php algorithm hash passwords

我对哈希密码知之甚少,但我想知道。我想知道以下算法对于没有信用卡信息或类似信息的普通网站有多好,我也想知道如何改进它。 算法是:

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

5 个答案:

答案 0 :(得分:3)

不要混合使用多个哈希值,每个哈希值都经过优化,以便自行运行。

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

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

答案 1 :(得分:3)

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

答案 2 :(得分:1)

这是我创建的一个类,用于为我集成的api存储id / password组合。每个用户都可以拥有自己的唯一凭据。我不建议在不兼容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);
?>

答案 3 :(得分:0)

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

  • 并且从不要求输入密码,而是要求使用密码哈希+盐(例如日期)的哈希来避免重放攻击

答案 4 :(得分:0)

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