Completed
Pull Request — master (#69)
by Robbie
15:17 queued 02:20
created

PBKDF2::encrypt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace CWP\Core\PasswordEncryptor;
4
5
use Exception;
6
use SilverStripe\Security\PasswordEncryptor_PHPHash;
7
8
/**
9
 * Provides Password-Based Key Derivation Function hashing for passwords, using the provided algorithm (default
10
 * is SHA512), which is NZISM compliant under version 3.2 section 17.2.
11
 */
12
class PBKDF2 extends PasswordEncryptor_PHPHash
13
{
14
    /**
15
     * The number of internal iterations for hash_pbkdf2() to perform for the derivation.
16
     *
17
     * @var int
18
     */
19
    protected $iterations = 10000;
20
21
    /**
22
     * @param string $algorithm
23
     * @param int|null $iterations
24
     * @throws Exception If the provided algorithm is not available in the current environment
25
     */
26
    public function __construct(string $algorithm, int $iterations = null)
27
    {
28
        parent::__construct($algorithm);
29
30
        if ($iterations !== null) {
31
            $this->iterations = $iterations;
32
        }
33
    }
34
35
    /**
36
     * @return int
37
     */
38
    public function getIterations(): int
39
    {
40
        return $this->iterations;
41
    }
42
43
    public function encrypt($password, $salt = null, $member = null)
44
    {
45
        return hash_pbkdf2($this->getAlgorithm(), $password, $salt, $this->getIterations());
46
    }
47
}
48