PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/symfony2/vendor/symfony/src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php

http://github.com/eryx/php-framework-benchmark
PHP | 91 lines | 40 code | 12 blank | 39 comment | 8 complexity | 8682595f9c74b2da93a324c7a28b9444 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  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. /**
  12. * BasePasswordEncoder is the base class for all password encoders.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. abstract class BasePasswordEncoder implements PasswordEncoderInterface
  17. {
  18. /**
  19. * Demerges a merge password and salt string.
  20. *
  21. * @param string $mergedPasswordSalt The merged password and salt string
  22. *
  23. * @return array An array where the first element is the password and the second the salt
  24. */
  25. protected function demergePasswordAndSalt($mergedPasswordSalt)
  26. {
  27. if (empty($mergedPasswordSalt)) {
  28. return array('', '');
  29. }
  30. $password = $mergedPasswordSalt;
  31. $salt = '';
  32. $saltBegins = strrpos($mergedPasswordSalt, '{');
  33. if (false !== $saltBegins && $saltBegins + 1 < strlen($mergedPasswordSalt)) {
  34. $salt = substr($mergedPasswordSalt, $saltBegins + 1, -1);
  35. $password = substr($mergedPasswordSalt, 0, $saltBegins);
  36. }
  37. return array($password, $salt);
  38. }
  39. /**
  40. * Merges a password and a salt.
  41. *
  42. * @param string $password the password to be used
  43. * @param string $salt the salt to be used
  44. *
  45. * @return string a merged password and salt
  46. */
  47. protected function mergePasswordAndSalt($password, $salt)
  48. {
  49. if (empty($salt)) {
  50. return $password;
  51. }
  52. if (false !== strrpos($salt, '{') || false !== strrpos($salt, '}')) {
  53. throw new \InvalidArgumentException('Cannot use { or } in salt.');
  54. }
  55. return $password.'{'.$salt.'}';
  56. }
  57. /**
  58. * Compares two passwords.
  59. *
  60. * This method implements a constant-time algorithm to compare passwords to
  61. * avoid (remote) timing attacks.
  62. *
  63. * @param string $password1 The first password
  64. * @param string $password2 The second password
  65. *
  66. * @return Boolean true if the two passwords are the same, false otherwise
  67. */
  68. protected function comparePasswords($password1, $password2)
  69. {
  70. if (strlen($password1) !== strlen($password2)) {
  71. return false;
  72. }
  73. $result = 0;
  74. for ($i = 0; $i < strlen($password1); $i++) {
  75. $result |= ord($password1[$i]) ^ ord($password2[$i]);
  76. }
  77. return 0 === $result;
  78. }
  79. }