Completed
Push — 1 ( 09a010...9d556f )
by Robbie
05:58 queued 02:30
created

PasswordEncryptor_PBKDF2   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 36
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterations() 0 3 1
A __construct() 0 6 2
A encrypt() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * Provides Password-Based Key Derivation Function hashing for passwords, using the provided algorithm (default
5
 * is SHA512), which is NZISM compliant under version 3.2 section 17.2.
6
 */
7
class PasswordEncryptor_PBKDF2 extends PasswordEncryptor_PHPHash
8
{
9
	/**
10
	 * The number of internal iterations for hash_pbkdf2() to perform for the derivation. Please note that if you
11
	 * change this from the default value you will break existing hashes stored in the database, so these would
12
	 * need to be regenerated.
13
	 *
14
	 * @var int
15
	 */
16
	protected $iterations = 10000;
17
18
	/**
19
	 * @param string $algorithm
20
	 * @param int|null $iterations
21
	 * @throws Exception If the provided algorithm is not available in the current environment
22
	 */
23
	public function __construct(string $algorithm, int $iterations = null)
24
	{
25
		parent::__construct($algorithm);
26
27
		if ($iterations !== null) {
28
			$this->iterations = $iterations;
29
		}
30
	}
31
32
	/**
33
	 * @return int
34
	 */
35
	public function getIterations(): int
36
	{
37
		return $this->iterations;
38
	}
39
40
	public function encrypt($password, $salt = null, $member = null)
41
	{
42
		return hash_pbkdf2($this->getAlgorithm(), (string) $password, (string) $salt, $this->getIterations());
43
	}
44
}
45