StringUtils.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Util;
  11. @trigger_error('The '.__NAMESPACE__.'\\StringUtils class is deprecated since Symfony 2.8 and will be removed in 3.0. Use hash_equals() instead.', E_USER_DEPRECATED);
  12. use Symfony\Polyfill\Util\Binary;
  13. /**
  14. * String utility functions.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. *
  18. * @deprecated since 2.8, to be removed in 3.0.
  19. */
  20. class StringUtils
  21. {
  22. /**
  23. * This class should not be instantiated.
  24. */
  25. private function __construct()
  26. {
  27. }
  28. /**
  29. * Compares two strings.
  30. *
  31. * This method implements a constant-time algorithm to compare strings.
  32. * Regardless of the used implementation, it will leak length information.
  33. *
  34. * @param string $knownString The string of known length to compare against
  35. * @param string $userInput The string that the user can control
  36. *
  37. * @return bool true if the two strings are the same, false otherwise
  38. */
  39. public static function equals($knownString, $userInput)
  40. {
  41. // Avoid making unnecessary duplications of secret data
  42. if (!\is_string($knownString)) {
  43. $knownString = (string) $knownString;
  44. }
  45. if (!\is_string($userInput)) {
  46. $userInput = (string) $userInput;
  47. }
  48. return hash_equals($knownString, $userInput);
  49. }
  50. /**
  51. * Returns the number of bytes in a string.
  52. *
  53. * @param string $string The string whose length we wish to obtain
  54. *
  55. * @return int
  56. */
  57. public static function safeStrlen($string)
  58. {
  59. return Binary::strlen($string);
  60. }
  61. }