vendor/symfony/security-core/Encoder/MessageDigestPasswordEncoder.php line 17

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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. use Symfony\Component\PasswordHasher\Hasher\MessageDigestPasswordHasher;
  12. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  13. trigger_deprecation('symfony/security-core''5.3''The "%s" class is deprecated, use "%s" instead.'MessageDigestPasswordEncoder::class, MessageDigestPasswordHasher::class);
  14. /**
  15.  * MessageDigestPasswordEncoder uses a message digest algorithm.
  16.  *
  17.  * @author Fabien Potencier <[email protected]>
  18.  *
  19.  * @deprecated since Symfony 5.3, use {@link MessageDigestPasswordHasher} instead
  20.  */
  21. class MessageDigestPasswordEncoder extends BasePasswordEncoder
  22. {
  23.     private $algorithm;
  24.     private $encodeHashAsBase64;
  25.     private $iterations 1;
  26.     private $encodedLength = -1;
  27.     /**
  28.      * @param string $algorithm          The digest algorithm to use
  29.      * @param bool   $encodeHashAsBase64 Whether to base64 encode the password hash
  30.      * @param int    $iterations         The number of iterations to use to stretch the password hash
  31.      */
  32.     public function __construct(string $algorithm 'sha512'bool $encodeHashAsBase64 trueint $iterations 5000)
  33.     {
  34.         $this->algorithm $algorithm;
  35.         $this->encodeHashAsBase64 $encodeHashAsBase64;
  36.         try {
  37.             $this->encodedLength \strlen($this->encodePassword('''salt'));
  38.         } catch (\LogicException $e) {
  39.             // ignore algorithm not supported
  40.         }
  41.         $this->iterations $iterations;
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function encodePassword(string $raw, ?string $salt)
  47.     {
  48.         if ($this->isPasswordTooLong($raw)) {
  49.             throw new BadCredentialsException('Invalid password.');
  50.         }
  51.         if (!\in_array($this->algorithmhash_algos(), true)) {
  52.             throw new \LogicException(sprintf('The algorithm "%s" is not supported.'$this->algorithm));
  53.         }
  54.         $salted $this->mergePasswordAndSalt($raw$salt);
  55.         $digest hash($this->algorithm$saltedtrue);
  56.         // "stretch" hash
  57.         for ($i 1$i $this->iterations; ++$i) {
  58.             $digest hash($this->algorithm$digest.$saltedtrue);
  59.         }
  60.         return $this->encodeHashAsBase64 base64_encode($digest) : bin2hex($digest);
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function isPasswordValid(string $encodedstring $raw, ?string $salt)
  66.     {
  67.         if (\strlen($encoded) !== $this->encodedLength || str_contains($encoded'$')) {
  68.             return false;
  69.         }
  70.         return !$this->isPasswordTooLong($raw) && $this->comparePasswords($encoded$this->encodePassword($raw$salt));
  71.     }
  72. }