vendor/symfony/security-core/Encoder/BCryptPasswordEncoder.php line 14

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Core\Encoder;
  11. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.'BCryptPasswordEncoder::class, NativePasswordEncoder::class), E_USER_DEPRECATED);
  12. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  13. /**
  14.  * @author Elnur Abdurrakhimov <elnur@elnur.pro>
  15.  * @author Terje BrĂ¥ten <terje@braten.be>
  16.  *
  17.  * @deprecated since Symfony 4.3, use NativePasswordEncoder instead
  18.  */
  19. class BCryptPasswordEncoder extends BasePasswordEncoder implements SelfSaltingEncoderInterface
  20. {
  21.     const MAX_PASSWORD_LENGTH 72;
  22.     private $cost;
  23.     /**
  24.      * @param int $cost The algorithmic cost that should be used
  25.      *
  26.      * @throws \RuntimeException         When no BCrypt encoder is available
  27.      * @throws \InvalidArgumentException if cost is out of range
  28.      */
  29.     public function __construct(int $cost)
  30.     {
  31.         if ($cost || $cost 31) {
  32.             throw new \InvalidArgumentException('Cost must be in the range of 4-31.');
  33.         }
  34.         $this->cost $cost;
  35.     }
  36.     /**
  37.      * Encodes the raw password.
  38.      *
  39.      * It doesn't work with PHP versions lower than 5.3.7, since
  40.      * the password compat library uses CRYPT_BLOWFISH hash type with
  41.      * the "$2y$" salt prefix (which is not available in the early PHP versions).
  42.      *
  43.      * @see https://github.com/ircmaxell/password_compat/issues/10#issuecomment-11203833
  44.      *
  45.      * It is almost best to **not** pass a salt and let PHP generate one for you.
  46.      *
  47.      * @param string $raw  The password to encode
  48.      * @param string $salt The salt
  49.      *
  50.      * @return string The encoded password
  51.      *
  52.      * @throws BadCredentialsException when the given password is too long
  53.      *
  54.      * @see http://lxr.php.net/xref/PHP_5_5/ext/standard/password.c#111
  55.      */
  56.     public function encodePassword($raw$salt)
  57.     {
  58.         if ($this->isPasswordTooLong($raw)) {
  59.             throw new BadCredentialsException('Invalid password.');
  60.         }
  61.         $options = ['cost' => $this->cost];
  62.         if ($salt) {
  63.             // Ignore $salt, the auto-generated one is always the best
  64.         }
  65.         return password_hash($rawPASSWORD_BCRYPT$options);
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function isPasswordValid($encoded$raw$salt)
  71.     {
  72.         return !$this->isPasswordTooLong($raw) && password_verify($raw$encoded);
  73.     }
  74. }