栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用bcrypt在PHP中对密码进行哈希处理?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用bcrypt在PHP中对密码进行哈希处理?

bcrypt
是一种哈希算法,可通过硬件(通过可配置的回合数)进行扩展。它的缓慢性和多次回合确保了攻击者必须部署大量资金和硬件才能破解您的密码。加上每个口令的盐(
bcrypt
需要盐),您可以确定,如果没有可笑的资金或硬件,攻击实际上是不可行的。

bcrypt
使用 Eksblowfish 算法对密码进行哈希处理。尽管 EksblowfishBlowfish
的加密阶段完全相同,但是 Eksblowfish
的密钥调度阶段可确保任何后续状态都取决于salt和密钥(用户密码),并且在不了解这两个状态的情况下无法对任何状态进行预先计算。
由于存在这一关键差异,因此
bcrypt
是一种单向哈希算法。
在不知道盐,四舍五入 和密钥 (密码)的情况下,您无法检索纯文本密码。


如何使用bcrypt:

使用PHP> = 5.5-DEV

密码哈希函数现已直接内置到PHP>=5.5中。您现在可以

password_hash()
用来创建
bcrypt
任何密码的哈希值:

<?php// Usage 1:echo password_hash('rasmuslerdorf', PASSWORD_DEFAULT)."n";// $2y$10$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx// For example:// $2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a// Usage 2:$options = [  'cost' => 11];echo password_hash('rasmuslerdorf', PASSWORD_BCRYPT, $options)."n";// $2y$11$6DP.V0nO7YI3iSki4qog6OQI5eiO6Jnjsqg7vdnb.JgGIsxniOn4C

要针对现有哈希值验证用户提供的密码,可以这样使用

password_verify()
](http://php.net/password_verify):

<?php// See the password_hash() example to see where this came from.$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';if (password_verify('rasmuslerdorf', $hash)) {    echo 'Password is valid!';} else {    echo 'Invalid password.';}

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

GitHub上有一个[兼容性库,该兼容性库是基于最初用C编写的上述函数的源代码创建的,该库提供相同的功能。一旦安装了兼容性库,用法与上面相同(如果仍在5.3.x分支上,请减去简写数组表示法)。

使用PHP <5.3.7 (已弃用)

您可以使用

crypt()
函数来生成输入字符串的bcrypt散列。此类可以自动生成盐,并根据输入来验证现有哈希。
如果您使用的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$%02d$', $this->rounds);    $bytes = $this->getRandomBytes(16);    $salt .= $this->enpreBytes($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 enpreBytes($input){    // The following is pre 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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/411977.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号