vendor/symfony/security-core/Encoder/BasePasswordEncoder.php line 16

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\CheckPasswordLengthTrait;
  12. trigger_deprecation('symfony/security-core', '5.3', 'The "%s" class is deprecated, use "%s" instead.', BasePasswordEncoder::class, CheckPasswordLengthTrait::class);
  13. /**
  14. * BasePasswordEncoder is the base class for all password encoders.
  15. *
  16. * @author Fabien Potencier <[email protected]>
  17. *
  18. * @deprecated since Symfony 5.3, use CheckPasswordLengthTrait instead
  19. */
  20. abstract class BasePasswordEncoder implements PasswordEncoderInterface
  21. {
  22. public const MAX_PASSWORD_LENGTH = 4096;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function needsRehash(string $encoded): bool
  27. {
  28. return false;
  29. }
  30. /**
  31. * Demerges a merge password and salt string.
  32. *
  33. * @return array An array where the first element is the password and the second the salt
  34. */
  35. protected function demergePasswordAndSalt(string $mergedPasswordSalt)
  36. {
  37. if (empty($mergedPasswordSalt)) {
  38. return ['', ''];
  39. }
  40. $password = $mergedPasswordSalt;
  41. $salt = '';
  42. $saltBegins = strrpos($mergedPasswordSalt, '{');
  43. if (false !== $saltBegins && $saltBegins + 1 < \strlen($mergedPasswordSalt)) {
  44. $salt = substr($mergedPasswordSalt, $saltBegins + 1, -1);
  45. $password = substr($mergedPasswordSalt, 0, $saltBegins);
  46. }
  47. return [$password, $salt];
  48. }
  49. /**
  50. * Merges a password and a salt.
  51. *
  52. * @return string
  53. *
  54. * @throws \InvalidArgumentException
  55. */
  56. protected function mergePasswordAndSalt(string $password, ?string $salt)
  57. {
  58. if (empty($salt)) {
  59. return $password;
  60. }
  61. if (false !== strrpos($salt, '{') || false !== strrpos($salt, '}')) {
  62. throw new \InvalidArgumentException('Cannot use { or } in salt.');
  63. }
  64. return $password.'{'.$salt.'}';
  65. }
  66. /**
  67. * Compares two passwords.
  68. *
  69. * This method implements a constant-time algorithm to compare passwords to
  70. * avoid (remote) timing attacks.
  71. *
  72. * @return bool
  73. */
  74. protected function comparePasswords(string $password1, string $password2)
  75. {
  76. return hash_equals($password1, $password2);
  77. }
  78. /**
  79. * Checks if the password is too long.
  80. *
  81. * @return bool
  82. */
  83. protected function isPasswordTooLong(string $password)
  84. {
  85. return \strlen($password) > static::MAX_PASSWORD_LENGTH;
  86. }
  87. }