summaryrefslogtreecommitdiffstats
path: root/Encoder/MessageDigestPasswordEncoder.php
diff options
context:
space:
mode:
authorFabien Potencier <fabien.potencier@gmail.com>2010-10-19 13:06:43 +0200
committerFabien Potencier <fabien.potencier@gmail.com>2010-10-19 13:33:17 +0200
commit3fec93d3ff1f6a31f078e5558a15a75539bf5185 (patch)
tree1a6229643289d9d0ca55871bab9497035a6e49f1 /Encoder/MessageDigestPasswordEncoder.php
downloadsymfony-security-3fec93d3ff1f6a31f078e5558a15a75539bf5185.zip
symfony-security-3fec93d3ff1f6a31f078e5558a15a75539bf5185.tar.gz
symfony-security-3fec93d3ff1f6a31f078e5558a15a75539bf5185.tar.bz2
added the Security Component and its integration into the MVC framework
Happy birthday symfony!
Diffstat (limited to 'Encoder/MessageDigestPasswordEncoder.php')
-rw-r--r--Encoder/MessageDigestPasswordEncoder.php61
1 files changed, 61 insertions, 0 deletions
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);
+ }
+}