/vendor/symfony/security-core/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php

https://gitlab.com/patrickmwanga/crdf · PHP · 100 lines · 39 code · 13 blank · 48 comment · 6 complexity · c3368de8d9b181cfb5a272d967d2c39d MD5 · raw file

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