‪TYPO3CMS  ‪main
BcryptPasswordHash.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
32 {
36  protected const ‪PREFIX = '$2y$';
37 
44  protected ‪$options = [
45  'cost' => 12,
46  ];
47 
53  public function ‪__construct(array ‪$options = [])
54  {
55  $newOptions = ‪$this->options;
56  // Check options for validity
57  if (isset(‪$options['cost'])) {
58  if (!$this->‪isValidBcryptCost((int)‪$options['cost'])) {
59  throw new \InvalidArgumentException(
60  'cost must not be lower than ' . PASSWORD_BCRYPT_DEFAULT_COST . ' or higher than 31',
61  1533902002
62  );
63  }
64  $newOptions['cost'] = (int)‪$options['cost'];
65  }
66  $this->options = $newOptions;
67  }
68 
72  public function ‪isAvailable(): bool
73  {
74  return true;
75  }
76 
84  public function ‪checkPassword(string $plainPW, string $saltedHashPW): bool
85  {
86  return password_verify($this->‪processPlainPassword($plainPW), $saltedHashPW);
87  }
88 
92  public function ‪getHashedPassword(string $password): ?string
93  {
94  $hashedPassword = null;
95  if ($password !== '') {
96  $password = $this->‪processPlainPassword($password);
97  $hashedPassword = password_hash($password, PASSWORD_BCRYPT, $this->options);
98  if (!is_string($hashedPassword) || empty($hashedPassword)) {
99  throw new InvalidPasswordHashException('Cannot generate password, probably invalid options', 1517174114);
100  }
101  }
102  return $hashedPassword;
103  }
104 
111  public function ‪isValidSaltedPW(string $saltedPW): bool
112  {
113  $result = false;
114  $passwordInfo = password_get_info($saltedPW);
115  // Validate the cost value, password_get_info() does not check it
116  $cost = (int)substr($saltedPW, 4, 2);
117  if (isset($passwordInfo['algo'])
118  && $passwordInfo['algo'] === PASSWORD_BCRYPT
119  && strncmp($saltedPW, static::PREFIX, strlen(static::PREFIX)) === 0
120  && $this->‪isValidBcryptCost($cost)
121  ) {
122  $result = true;
123  }
124  return $result;
125  }
132  public function ‪isHashUpdateNeeded(string $passString): bool
133  {
134  return password_needs_rehash($passString, PASSWORD_BCRYPT, $this->options);
135  }
136 
145  protected function ‪processPlainPassword(string $password): string
146  {
147  return base64_encode(hash('sha384', $password, true));
148  }
149 
153  protected function ‪isValidBcryptCost(int $cost): bool
154  {
155  return $cost >= PASSWORD_BCRYPT_DEFAULT_COST && $cost <= 31;
156  }
157 }
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash\isHashUpdateNeeded
‪bool isHashUpdateNeeded(string $passString)
Definition: BcryptPasswordHash.php:131
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash
Definition: BcryptPasswordHash.php:32
‪TYPO3\CMS\Core\Crypto\PasswordHashing
Definition: AbstractArgon2PasswordHash.php:18
‪TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException
Definition: InvalidPasswordHashException.php:25
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash\isValidSaltedPW
‪bool isValidSaltedPW(string $saltedPW)
Definition: BcryptPasswordHash.php:110
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash\getHashedPassword
‪getHashedPassword(string $password)
Definition: BcryptPasswordHash.php:91
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash\checkPassword
‪checkPassword(string $plainPW, string $saltedHashPW)
Definition: BcryptPasswordHash.php:83
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash\__construct
‪__construct(array $options=[])
Definition: BcryptPasswordHash.php:52
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash\processPlainPassword
‪processPlainPassword(string $password)
Definition: BcryptPasswordHash.php:144
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash\isValidBcryptCost
‪isValidBcryptCost(int $cost)
Definition: BcryptPasswordHash.php:152
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash\isAvailable
‪isAvailable()
Definition: BcryptPasswordHash.php:71
‪TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashInterface
Definition: PasswordHashInterface.php:25
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash\PREFIX
‪const PREFIX
Definition: BcryptPasswordHash.php:36
‪TYPO3\CMS\Core\Crypto\PasswordHashing\BcryptPasswordHash\$options
‪array $options
Definition: BcryptPasswordHash.php:43