PBEKDF1::derive()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 4
crap 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