KeyDerivation::generateSalt()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.2
c 1
b 0
f 0
cc 4
eloc 4
nc 2
nop 1
crap 4.25
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