summaryrefslogtreecommitdiffstats
path: root/Encoder/MessageDigestPasswordEncoder.php
blob: 4d0edd8ad1c1734079de0dbdf12b67639624c13f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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 $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
    }
}