Completed
Branch master (95c3c5)
by
unknown
05:09
created

KeyDerivation::compute()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 5
nop 3
1
<?php
2
3
namespace Blocktrail\SDK\V3Crypt;
4
5
use BitWasp\Buffertools\Buffer;
6
use BitWasp\Buffertools\BufferInterface;
7
8
class KeyDerivation
9
{
10
    const HASHER = 'sha512';
11
    const DEFAULT_ITERATIONS = 35000;
12
    const KEYLEN_BITS = 256;
13
14
    /**
15
     * @param BufferInterface $password
16
     * @param BufferInterface $salt
17
     * @param int $iterations
18
     * @return BufferInterface
19
     */
20
    public static function compute(BufferInterface $password, BufferInterface $salt, $iterations) {
21
        if (!($iterations >= 0 && $iterations < pow(2, 32))) {
22
            throw new \RuntimeException('Iterations must be a number between 1 and 2^32');
23
        }
24
25
        if ($iterations < 512) {
26
            throw new \RuntimeException('Iteration count should be at least 512');
27
        }
28
29
        if ($salt->getSize() === 0) {
30
            throw new \RuntimeException('Salt must not be empty');
31
        }
32
33
        if ($salt->getSize() > 0x80) {
34
            throw new \RuntimeException('Sanity check: Invalid salt, length can never be greater than 128');
35
        }
36
37
        return new Buffer(hash_pbkdf2(self::HASHER, $password->getBinary(), $salt->getBinary(), $iterations, self::KEYLEN_BITS / 8, true));
0 ignored issues
show
Bug introduced by
It seems like hash_pbkdf2(self::HASHER...:KEYLEN_BITS / 8, true) targeting hash_pbkdf2() can also be of type false or null; however, BitWasp\Buffertools\Buffer::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
38
    }
39
}
40