我们如何在PHP中创建一个相当安全的密码哈希?

Bum*_*imp 12 php passwords hash

我一直在读密码哈希,但我读过的所有论坛都充斥着人们辩论其背后的理论的帖子,我真的不明白.

我有一个旧的(可能非常弱)密码脚本,如下所示:$ hash = sha1($ pass1);

function createSalt()
{
$string = md5(uniqid(rand(), true));
return substr($string, 0, 3);
}

$salt = createSalt();
$hash = sha1($salt . $hash);
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,盐越长,黑客必须生成的表格越大,以打破哈希.如果我错了,请纠正我.

我正在寻找一个更安全的新脚本,我想这样的事情可以:

function createSalt()
{
$string = hash('sha256', uniqid(rand(), true));
return $string;
}


$hash = hash('sha256', $password);
$salt = createSalt();
$secret_server_hash =     'ac1d81c5f99fdfc6758f21010be4c673878079fdc8f144394030687374f185ad';
$salt2 = hash('sha256', $salt);
$hash = $salt2 . $hash . $secret_server_hash;
$hash = hash('sha512', $hash );
Run Code Online (Sandbox Code Playgroud)

这更安全吗?这有明显的开销吗?

最重要的是,是否有更好的方法可以确保我的数据库中的密码无法通过密码分析(现实地)恢复,从而确保安全性受损的唯一方法是通过我自己的编码错误?

编辑:

在阅读了所有答案并进一步重新研究之后,我决定继续实施保护密码的bcrypt方法.话虽如此,出于好奇心的缘故,如果我要采用上面的代码并对其进行循环,比如100,000次迭代,是否会实现类似于bcrypt的强度/安全性的东西?

And*_*ore 11

盐到目前为止只能帮助你.如果您使用的哈希算法如此之快,以至于生成彩虹表几乎没有成本,那么您的安全性仍会受到影响.

几点建议:

  • 千万不要使用单一的盐的所有密码.每个密码使用随机生成的盐.
  • 千万不要老调重弹未修改哈希(碰撞的问题,看我前面的回答,您需要无限的输入散列).
  • 千万不要尝试创建自己的哈希算法或混合匹配算法为复杂的操作.
  • 如果坚持使用破坏/不安全/快速哈希原语,请使用密钥强化.这增加了攻击者计算彩虹表所需的时间.例:

function strong_hash($input, $salt = null, $algo = 'sha512', $rounds = 20000) {
  if($salt === null) {
    $salt = crypto_random_bytes(16);
  } else {
    $salt = pack('H*', substr($salt, 0, 32));
  }

  $hash = hash($algo, $salt . $input);

  for($i = 0; $i < $rounds; $i++) {
    // $input is appended to $hash in order to create
    // infinite input.
    $hash = hash($algo, $hash . $input);
  }

  // Return salt and hash. To verify, simply
  // passed stored hash as second parameter.
  return bin2hex($salt) . $hash;
}

function crypto_random_bytes($count) {
  static $randomState = null;

  $bytes = '';

  if(function_exists('openssl_random_pseudo_bytes') &&
      (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) { // OpenSSL slow on Win
    $bytes = openssl_random_pseudo_bytes($count);
  }

  if($bytes === '' && is_readable('/dev/urandom') &&
     ($hRand = @fopen('/dev/urandom', 'rb')) !== FALSE) {
    $bytes = fread($hRand, $count);
    fclose($hRand);
  }

  if(strlen($bytes) < $count) {
    $bytes = '';

    if($randomState === null) {
      $randomState = microtime();
      if(function_exists('getmypid')) {
        $randomState .= getmypid();
      }
    }

    for($i = 0; $i < $count; $i += 16) {
      $randomState = md5(microtime() . $randomState);

      if (PHP_VERSION >= '5') {
        $bytes .= md5($randomState, true);
      } else {
        $bytes .= pack('H*', md5($randomState));
      }
    }

    $bytes = substr($bytes, 0, $count);
  }

  return $bytes;
}
Run Code Online (Sandbox Code Playgroud)

而不是部署自己的(固有的缺陷)哈希/盐算法,为什么不使用由安全专业人员开发的算法?

使用bcrypt.它的开发正是为了这一点.它的速度慢,多轮确保攻击者必须部署大量资金和硬件才能破解密码.添加到每个密码的盐(bcrypt REQUIRES盐),你可以确定攻击几乎是不可行的,没有可笑的资金或硬件.

非便携模式下的Portable PHP Hashing Framework允许您轻松地使用bcrypt生成哈希.

您还可以使用crypt()函数生成输入字符串的bcrypt哈希值.如果沿着那条路走下去,请确保每个哈希生成一个盐.

此类可以自动生成salt并验证输入的现有哈希值.

class Bcrypt {
  private $rounds;
  public function __construct($rounds = 12) {
    if(CRYPT_BLOWFISH != 1) {
      throw new Exception("bcrypt not supported in this installation. See http://php.net/crypt");
    }

    $this->rounds = $rounds;
  }

  public function hash($input) {
    $hash = crypt($input, $this->getSalt());

    if(strlen($hash) > 13)
      return $hash;

    return false;
  }

  public function verify($input, $existingHash) {
    $hash = crypt($input, $existingHash);

    return $hash === $existingHash;
  }

  private function getSalt() {
    $salt = sprintf('$2a$%02d$', $this->rounds);

    $bytes = $this->getRandomBytes(16);

    $salt .= $this->encodeBytes($bytes);

    return $salt;
  }

  private $randomState;
  private function getRandomBytes($count) {
    $bytes = '';

    if(function_exists('openssl_random_pseudo_bytes') &&
        (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) { // OpenSSL slow on Win
      $bytes = openssl_random_pseudo_bytes($count);
    }

    if($bytes === '' && is_readable('/dev/urandom') &&
       ($hRand = @fopen('/dev/urandom', 'rb')) !== FALSE) {
      $bytes = fread($hRand, $count);
      fclose($hRand);
    }

    if(strlen($bytes) < $count) {
      $bytes = '';

      if($this->randomState === null) {
        $this->randomState = microtime();
        if(function_exists('getmypid')) {
          $this->randomState .= getmypid();
        }
      }

      for($i = 0; $i < $count; $i += 16) {
        $this->randomState = md5(microtime() . $this->randomState);

        if (PHP_VERSION >= '5') {
          $bytes .= md5($this->randomState, true);
        } else {
          $bytes .= pack('H*', md5($this->randomState));
        }
      }

      $bytes = substr($bytes, 0, $count);
    }

    return $bytes;
  }

  private function encodeBytes($input) {
    return strtr(rtrim(base64_encode($input), '='), '+', '.');
  }
}
Run Code Online (Sandbox Code Playgroud)

您可以这样使用此代码:

$bcrypt = new Bcrypt(15);

$hash = $bcrypt->hash('password');
$isGood = $bcrypt->verify('password', $hash);
Run Code Online (Sandbox Code Playgroud)