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

KeyDerivation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 32
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B compute() 0 19 6
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