溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在PHP中使用bcrypt來哈希密碼

發布時間:2021-06-12 16:56:22 來源:億速云 閱讀:356 作者:小新 欄目:大數據

這篇文章將為大家詳細講解有關怎么在PHP中使用bcrypt來哈希密碼,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

我偶爾會聽到“使用bcrypt在PHP中存儲密碼,bcrypt規則”的建議。

但是什么bcrypt?PHP不提供任何這樣的功能,維基百科關于文件加密實用程序的喋喋不休,Web搜索只是揭示了幾種不同語言的Blowfish實現?,F在Blowfish也可以通過PHP獲得mcrypt,但這對于存儲密碼有什么幫助?河豚是一種通用密碼,它有兩種工作方式。如果它可以被加密,它可以被解密。密碼需要單向散列函數。

什么是解釋?


bcrypt是一種哈希算法,可以通過硬件進行擴展(通過可配置的循環次數)。其緩慢和多輪確保攻擊者必須部署大量資金和硬件才能破解密碼。添加到每個密碼鹽(bcrypt需要鹽),你可以肯定的是,一個攻擊實際上是不可行的,沒有可笑的金額或硬件。

bcrypt使用Eksblowfish算法來散列密碼。雖然EksblowfishBlowfish的加密階段完全相同,但Eksblowfish的關鍵調度階段確保任何后續狀態都依賴salt和key(用戶密碼),并且在沒有兩者都知道的情況下不能預先計算狀態。由于這個關鍵差異,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()方式:

<?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 (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$%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 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來哈希密碼”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女