Completed
Push — master ( 7e41c9...ce0f57 )
by Michael
04:14 queued 49s
created

OpensslKey::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * OpensslKey.php
5
 *
6
 * PHP version 7
7
 *
8
 * @category Dcrypt
9
 * @package  Dcrypt
10
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
11
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
12
 * @link     https://github.com/mmeyer2k/dcrypt
13
 */
14
15
namespace Dcrypt;
16
17
use Dcrypt\Exceptions\InvalidKeyException;
18
19
/**
20
 * Provides key derivation functions
21
 *
22
 * @category Dcrypt
23
 * @package  Dcrypt
24
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
25
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
26
 * @link     https://github.com/mmeyer2k/dcrypt
27
 */
28
final class OpensslKey
29
{
30
    /**
31
     * @var string
32
     */
33
    private $key;
34
35
    /**
36
     * @var string
37
     */
38
    private $algo;
39
40
    /**
41
     * @var string
42
     */
43
    private $ivr;
44
45
    /**
46
     * OpensslKey constructor.
47
     *
48
     * @param string $algo Algo to use for HKDF
49
     * @param string $key  Key
50
     * @param string $ivr  Initialization vactor
51
     * @throws InvalidKeyException
52
     */
53 34
    public function __construct(string $algo, string $key, string $ivr)
54
    {
55
        // Store the key as what was supplied
56 34
        $this->key = \base64_decode($key);
57
58
        // Make sure key was properly decoded and meets minimum required length
59 34
        if (!is_string($this->key) || Str::strlen($this->key) < 2048) {
60 5
            throw new InvalidKeyException("Key must be at least 256 bytes and base64 encoded.");
61
        }
62
63
        // Make sure key meets minimum entropy requirement
64 29
        if (\count(\array_unique(\str_split($this->key))) < 250) {
65 1
            throw new InvalidKeyException("Key does not contain the minimum amount of entropy.");
66
        }
67
68
        // Store algo in object
69 28
        $this->algo = $algo;
70
71
        // Store init vector in object
72 28
        $this->ivr = $ivr;
73 28
    }
74
75
    /**
76
     * Generate the authentication key
77
     *
78
     * @param string $info
79
     * @return string
80
     */
81 25
    public function authenticationKey(string $info): string
82
    {
83 25
        return $this->deriveKey(__FUNCTION__ . '|' . $info);
84
    }
85
86
    /**
87
     * Generate the encryption key
88
     *
89
     * @param string $info
90
     * @return string
91
     */
92 26
    public function encryptionKey(string $info): string
93
    {
94 26
        return $this->deriveKey(__FUNCTION__ . '|' . $info);
95
    }
96
97
    /**
98
     * Derive a key with differing authinfo strings
99
     *
100
     * @param string $info Info parameter to provide to hash_hkdf
101
     * @return string
102
     */
103 28
    public function deriveKey(string $info): string
104
    {
105 28
        $key = \hash_hkdf($this->algo, $this->key, 0, $info, $this->ivr);
106
107 27
        return $key;
108
    }
109
110
    /**
111
     * Generate a new key that meets requirements for dcrypt
112
     *
113
     * @param int $size Size of key in bytes
0 ignored issues
show
Bug introduced by
There is no parameter named $size. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
114
     * @return string
115
     * @throws InvalidKeyException
116
     */
117 23
    public static function create(int $bytes = 2048): string
118
    {
119 23
        if ($bytes < 2048) {
120 1
            throw new InvalidKeyException('Keys must be at least 2048 bytes long.');
121
        }
122
123 23
        return \base64_encode(\random_bytes($bytes));
124
    }
125
}