千家信息网

怎么在PHP中使用bcrypt来哈希密码

发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,这篇文章将为大家详细讲解有关怎么在PHP中使用bcrypt来哈希密码,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。我偶尔会听到"使用bcrypt在PHP中存储密码,
千家信息网最后更新 2025年12月03日怎么在PHP中使用bcrypt来哈希密码

这篇文章将为大家详细讲解有关怎么在PHP中使用bcrypt来哈希密码,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

我偶尔会听到"使用bcrypt在PHP中存储密码,bcrypt规则"的建议。

但是什么bcrypt?PHP不提供任何这样的功能,维基百科关于文件加密实用程序的喋喋不休,Web搜索只是揭示了几种不同语言的Blowfish实现。现在Blowfish也可以通过PHP获得mcrypt,但这对于存储密码有什么帮助?河豚是一种通用密码,它有两种工作方式。如果它可以被加密,它可以被解密。密码需要单向散列函数。

什么是解释?


bcrypt是一种哈希算法,可以通过硬件进行扩展(通过可配置的循环次数)。其缓慢和多轮确保攻击者必须部署大量资金和硬件才能破解密码。添加到每个密码盐(bcrypt需要盐),你可以肯定的是,一个攻击实际上是不可行的,没有可笑的金额或硬件。

bcrypt使用Eksblowfish算法来散列密码。虽然EksblowfishBlowfish的加密阶段完全相同,但Eksblowfish的关键调度阶段确保任何后续状态都依赖salt和key(用户密码),并且在没有两者都知道的情况下不能预先计算状态。由于这个关键差异,bcrypt是一种单向哈希算法。如果不知道盐,圆和密码(密码),则无法检索纯文本密码。[ 来源 ]

如何使用bcrypt:

使用PHP> = 5.5-DEV

密码散列函数现在已直接构建到PHP> = 5.5中。您现在可以使用password_hash()创建bcrypt任何密码的哈希值:

 11];echo password_hash('rasmuslerdorf', PASSWORD_BCRYPT, $options)."\n";// $2y$11$6DP.V0nO7YI3iSki4qog6OQI5eiO6Jnjsqg7vdnb.JgGIsxniOn4C

要根据现有的散列验证用户提供的密码,可以使用以下password_verify()方式:

使用PHP> = 5.3.7,<5.5-DEV(也是RedHat PHP> = 5.3.3)

GitHub上有一个兼容库,它基于上面用C编写的函数的源代码,它提供了相同的功能。安装兼容性库后,用法与上述相同(如果仍在5.3.x分支上,则减去速记数组表示法)。

使用PHP <5.3.7 (DEPRECATED)

您可以使用crypt()函数来生成输入字符串的bcrypt散列。这个类可以自动生成salt并根据输入验证现有的散列。如果您使用的PHP版本高于或等于5.3.7,强烈建议您使用内置函数或compat库。此替代方案仅用于历史目的。

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$d$', $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 is slow on Windows      $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){    // The following is code from the PHP Password Hashing Framework    $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';    $output = '';    $i = 0;    do {      $c1 = ord($input[$i++]);      $output .= $itoa64[$c1 >> 2];      $c1 = ($c1 & 0x03) << 4;      if ($i >= 16) {        $output .= $itoa64[$c1];        break;      }      $c2 = ord($input[$i++]);      $c1 |= $c2 >> 4;      $output .= $itoa64[$c1];      $c1 = ($c2 & 0x0f) << 2;      $c2 = ord($input[$i++]);      $c1 |= $c2 >> 6;      $output .= $itoa64[$c1];      $output .= $itoa64[$c2 & 0x3f];    } while (true);    return $output;  }}

你可以使用这样的代码:

$bcrypt = new Bcrypt(15);$hash = $bcrypt->hash('password');$isGood = $bcrypt->verify('password', $hash);或者,您也可以使用Portable PHP Hashing Framework

关于"怎么在PHP中使用bcrypt来哈希密码"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

0