Pbkdf2PasswordEncoder.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  12. /**
  13. * Pbkdf2PasswordEncoder uses the PBKDF2 (Password-Based Key Derivation Function 2).
  14. *
  15. * Providing a high level of Cryptographic security,
  16. * PBKDF2 is recommended by the National Institute of Standards and Technology (NIST).
  17. *
  18. * But also warrants a warning, using PBKDF2 (with a high number of iterations) slows down the process.
  19. * PBKDF2 should be used with caution and care.
  20. *
  21. * @author Sebastiaan Stok <s.stok@rollerscapes.net>
  22. * @author Andrew Johnson
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class Pbkdf2PasswordEncoder extends BasePasswordEncoder
  26. {
  27. private $algorithm;
  28. private $encodeHashAsBase64;
  29. private $iterations = 1;
  30. private $length;
  31. private $encodedLength = -1;
  32. /**
  33. * @param string $algorithm The digest algorithm to use
  34. * @param bool $encodeHashAsBase64 Whether to base64 encode the password hash
  35. * @param int $iterations The number of iterations to use to stretch the password hash
  36. * @param int $length Length of derived key to create
  37. */
  38. public function __construct(string $algorithm = 'sha512', bool $encodeHashAsBase64 = true, int $iterations = 1000, int $length = 40)
  39. {
  40. $this->algorithm = $algorithm;
  41. $this->encodeHashAsBase64 = $encodeHashAsBase64;
  42. $this->length = $length;
  43. try {
  44. $this->encodedLength = \strlen($this->encodePassword('', 'salt'));
  45. } catch (\LogicException $e) {
  46. // ignore algorithm not supported
  47. }
  48. $this->iterations = $iterations;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. *
  53. * @throws \LogicException when the algorithm is not supported
  54. */
  55. public function encodePassword(string $raw, ?string $salt)
  56. {
  57. if ($this->isPasswordTooLong($raw)) {
  58. throw new BadCredentialsException('Invalid password.');
  59. }
  60. if (!\in_array($this->algorithm, hash_algos(), true)) {
  61. throw new \LogicException(sprintf('The algorithm "%s" is not supported.', $this->algorithm));
  62. }
  63. $digest = hash_pbkdf2($this->algorithm, $raw, $salt, $this->iterations, $this->length, true);
  64. return $this->encodeHashAsBase64 ? base64_encode($digest) : bin2hex($digest);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function isPasswordValid(string $encoded, string $raw, ?string $salt)
  70. {
  71. if (\strlen($encoded) !== $this->encodedLength || false !== strpos($encoded, '$')) {
  72. return false;
  73. }
  74. return !$this->isPasswordTooLong($raw) && $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
  75. }
  76. }