KeyDerivation   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 33
ccs 6
cts 7
cp 0.8571
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateSalt() 0 8 4
A compute() 0 5 1
1
<?php
2
3
namespace Btccom\JustEncrypt;
4
5
use BitWasp\Buffertools\Buffer;
6
use BitWasp\Buffertools\BufferInterface;
7
8
class KeyDerivation
9
{
10
    const HASHER = 'sha512';
11
    const DEFAULT_SALTLEN = 10;
12
    const DEFAULT_ITERATIONS = 35000;
13
    const SUBKEY_ITERATIONS = 1;
14
    const KEYLEN_BITS = 256;
15
16
    /**
17
     * @param int $length
18
     * @return BufferInterface
19
     */
20 1
    public static function generateSalt($length = self::DEFAULT_SALTLEN)
21
    {
22 1
        if (!is_int($length) || $length < 0 || $length > 128) {
23
            throw new \RuntimeException('Invalid salt length, should be between 0 - 128 bytes');
24
        }
25
26 1
        return new Buffer(random_bytes($length));
27
    }
28
29
    /**
30
     * @param BufferInterface $password
31
     * @param BufferInterface $salt
32
     * @param int $iterations
33
     * @return BufferInterface
34
     */
35 5
    public static function compute(BufferInterface $password, BufferInterface $salt, $iterations = self::DEFAULT_ITERATIONS)
36
    {
37 5
        $header = new HeaderBlob($salt->getSize(), $salt, $iterations);
38 5
        return $header->deriveKey($password);
39
    }
40
}
41