PBEKDF1   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 33
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A derive() 0 11 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\PKCS5\PBEKD;
6
7
use Sop\PKCS5\HashFunc\HashFunc;
8
9
/**
10
 * Implements key derivation function #1 used in password-based cryptography.
11
 *
12
 * @see https://tools.ietf.org/html/rfc2898#section-5.1
13
 */
14
class PBEKDF1 extends PBEKDF
15
{
16
    /**
17
     * Hash functor.
18
     *
19
     * @var HashFunc
20
     */
21
    protected $_hashFunc;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param HashFunc $hashfunc
27
     */
28 10
    public function __construct(HashFunc $hashfunc)
29
    {
30 10
        $this->_hashFunc = $hashfunc;
31 10
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 11
    public function derive(string $password, string $salt, int $count,
37
        int $length): string
38
    {
39 11
        if ($length > $this->_hashFunc->length()) {
40 1
            throw new \LogicException('Derived key too long.');
41
        }
42 10
        $key = $password . $salt;
43 10
        for ($i = 0; $i < $count; ++$i) {
44 10
            $key = $this->_hashFunc->__invoke($key);
45
        }
46 10
        return substr($key, 0, $length);
47
    }
48
}
49