summaryrefslogtreecommitdiffstats
path: root/Encoder
diff options
context:
space:
mode:
Diffstat (limited to 'Encoder')
-rw-r--r--Encoder/BasePasswordEncoder.php65
-rw-r--r--Encoder/MessageDigestPasswordEncoder.php61
-rw-r--r--Encoder/PasswordEncoderInterface.php41
-rw-r--r--Encoder/PlaintextPasswordEncoder.php49
4 files changed, 216 insertions, 0 deletions
diff --git a/Encoder/BasePasswordEncoder.php b/Encoder/BasePasswordEncoder.php
new file mode 100644
index 0000000..1378d56
--- /dev/null
+++ b/Encoder/BasePasswordEncoder.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace Symfony\Component\Security\Encoder;
+
+/*
+ * 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.
+ */
+
+/**
+ * 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, strlen($mergedPasswordSalt) - 1);
+ $password = substr($mergedPasswordSalt, 0, $saltBegins);
+ }
+
+ return array($password, $salt);
+ }
+
+ /**
+ * Merges a password and a salt.
+ *
+ * @param password the password to be used (can be <code>null</code>)
+ * @param salt the salt to be used (can be <code>null</code>)
+ *
+ * @return a merged password and salt <code>String</code>
+ */
+ 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.'}';
+ }
+}
diff --git a/Encoder/MessageDigestPasswordEncoder.php b/Encoder/MessageDigestPasswordEncoder.php
new file mode 100644
index 0000000..08efe00
--- /dev/null
+++ b/Encoder/MessageDigestPasswordEncoder.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace Symfony\Component\Security\Encoder;
+
+/*
+ * 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.
+ */
+
+/**
+ * MessageDigestPasswordEncoder uses a message digest algorithm.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class MessageDigestPasswordEncoder extends BasePasswordEncoder
+{
+ protected $algorithm;
+ protected $encodeHashAsBase64;
+
+ /**
+ * Constructor.
+ *
+ * @param string $algorithm The digest algorithm to use
+ * @param Boolean $encodeHashAsBase64 Whether to base64 encode the password
+ * @param integer $iterations The number of iterations to use to stretch the password
+ */
+ public function __construct($algorithm = 'sha1', $encodeHashAsBase64 = false, $iterations = 1)
+ {
+ $this->algorithm = $algorithm;
+ $this->encodeHashAsBase64 = $encodeHashAsBase64;
+ $this->iterations = $iterations;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function encodePassword($raw, $salt)
+ {
+ $salted = $this->mergePasswordAndSalt($raw, $salt);
+ $digest = call_user_func($this->algorithm, $salted);
+
+ // "stretch" the encoded value
+ for ($i = 1; $i < $this->iterations; $i++) {
+ $digest = call_user_func($this->algorithm, $digest);
+ }
+
+ return $this->encodeHashAsBase64 ? base64_encode($digest) : $digest;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function isPasswordValid($encoded, $raw, $salt)
+ {
+ return $encoded === $this->encodePassword($raw, $salt);
+ }
+}
diff --git a/Encoder/PasswordEncoderInterface.php b/Encoder/PasswordEncoderInterface.php
new file mode 100644
index 0000000..c81ec25
--- /dev/null
+++ b/Encoder/PasswordEncoderInterface.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Symfony\Component\Security\Encoder;
+
+/*
+ * 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.
+ */
+
+/**
+ * PasswordEncoderInterface is the interface for all encoders.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+interface PasswordEncoderInterface
+{
+ /**
+ * Encodes the raw password.
+ *
+ * @param string $raw The password to encode
+ * @param stirng $salt The salt
+ *
+ * @return string The encoded password
+ */
+ function encodePassword($raw, $salt);
+
+ /**
+ * Checks a raw password against an encoded password.
+ *
+ * @param string $encoded An encoded password
+ * @param string $raw A raw password
+ * @param string $salt The salt
+ *
+ * @return Boolean true if the password is valid, false otherwise
+ */
+ function isPasswordValid($encoded, $raw, $salt);
+}
diff --git a/Encoder/PlaintextPasswordEncoder.php b/Encoder/PlaintextPasswordEncoder.php
new file mode 100644
index 0000000..256a4eb
--- /dev/null
+++ b/Encoder/PlaintextPasswordEncoder.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Symfony\Component\Security\Encoder;
+
+/*
+ * 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.
+ */
+
+/**
+ * PlaintextPasswordEncoder does not do any encoding.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class PlaintextPasswordEncoder extends BasePasswordEncoder
+{
+ protected $ignorePasswordCase;
+
+ public function __construct($ignorePasswordCase = false)
+ {
+ $this->ignorePasswordCase = $ignorePasswordCase;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function encodePassword($raw, $salt)
+ {
+ return $this->mergePasswordAndSalt($raw, $salt);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function isPasswordValid($encoded, $raw, $salt)
+ {
+ $pass2 = $this->mergePasswordAndSalt($raw, $salt);
+
+ if (!$this->ignorePasswordCase) {
+ return $encoded === $pass2;
+ } else {
+ return strtolower($encoded) === strtolower($pass2);
+ }
+ }
+}