summaryrefslogtreecommitdiffstats
path: root/Core/Encoder/BasePasswordEncoder.php
diff options
context:
space:
mode:
authorJohannes M. Schmitt <schmittjoh@gmail.com>2011-01-26 21:34:11 +0100
committerFabien Potencier <fabien.potencier@gmail.com>2011-01-26 22:23:20 +0100
commitbebc09870cb0a7720e2c6a8c5c74585e69e8bb24 (patch)
tree0c399647cdbe504be405017e7cc04c70c53482f2 /Core/Encoder/BasePasswordEncoder.php
parentc85f3d708d2c9b00d73ca1234ccfaf50336d94b1 (diff)
downloadsymfony-security-bebc09870cb0a7720e2c6a8c5c74585e69e8bb24.zip
symfony-security-bebc09870cb0a7720e2c6a8c5c74585e69e8bb24.tar.gz
symfony-security-bebc09870cb0a7720e2c6a8c5c74585e69e8bb24.tar.bz2
namespace changes
Symfony\Component\Security -> Symfony\Component\Security\Core Symfony\Component\Security\Acl remains unchanged Symfony\Component\HttpKernel\Security -> Symfony\Component\Security\Http
Diffstat (limited to 'Core/Encoder/BasePasswordEncoder.php')
-rw-r--r--Core/Encoder/BasePasswordEncoder.php91
1 files changed, 91 insertions, 0 deletions
diff --git a/Core/Encoder/BasePasswordEncoder.php b/Core/Encoder/BasePasswordEncoder.php
new file mode 100644
index 0000000..01f471c
--- /dev/null
+++ b/Core/Encoder/BasePasswordEncoder.php
@@ -0,0 +1,91 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Core\Encoder;
+
+/**
+ * BasePasswordEncoder is the base class for all password encoders.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+abstract class BasePasswordEncoder implements PasswordEncoderInterface
+{
+ /**
+ * Demerges a merge password and salt string.
+ *
+ * @param string $mergedPasswordSalt The merged password and salt string
+ *
+ * @return array An array where the first element is the password and the second the salt
+ */
+ protected function demergePasswordAndSalt($mergedPasswordSalt)
+ {
+ if (empty($mergedPasswordSalt)) {
+ return array('', '');
+ }
+
+ $password = $mergedPasswordSalt;
+ $salt = '';
+ $saltBegins = strrpos($mergedPasswordSalt, '{');
+
+ if (false !== $saltBegins && $saltBegins + 1 < strlen($mergedPasswordSalt)) {
+ $salt = substr($mergedPasswordSalt, $saltBegins + 1, -1);
+ $password = substr($mergedPasswordSalt, 0, $saltBegins);
+ }
+
+ return array($password, $salt);
+ }
+
+ /**
+ * Merges a password and a salt.
+ *
+ * @param string $password the password to be used
+ * @param string $salt the salt to be used
+ *
+ * @return string a merged password and salt
+ */
+ protected function mergePasswordAndSalt($password, $salt)
+ {
+ if (empty($salt)) {
+ return $password;
+ }
+
+ if (false !== strrpos($salt, '{') || false !== strrpos($salt, '}')) {
+ throw new \InvalidArgumentException('Cannot use { or } in salt.');
+ }
+
+ return $password.'{'.$salt.'}';
+ }
+
+ /**
+ * Compares two passwords.
+ *
+ * This method implements a constant-time algorithm to compare passwords to
+ * avoid (remote) timing attacks.
+ *
+ * @param string $password1 The first password
+ * @param string $password2 The second password
+ *
+ * @return Boolean true if the two passwords are the same, false otherwise
+ */
+ protected function comparePasswords($password1, $password2)
+ {
+ if (strlen($password1) !== strlen($password2)) {
+ return false;
+ }
+
+ $result = 0;
+ for ($i = 0; $i < strlen($password1); $i++) {
+ $result |= ord($password1[$i]) ^ ord($password2[$i]);
+ }
+
+ return 0 === $result;
+ }
+}