Completed
Push — master ( 893d0a...ad37f7 )
by Michael
01:56
created

OpensslKey::_testKeyEntropy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * OpensslKey.php.
7
 *
8
 * PHP version 7
9
 *
10
 * @category Dcrypt
11
 *
12
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
13
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
14
 *
15
 * @link     https://github.com/mmeyer2k/dcrypt
16
 */
17
18
namespace Dcrypt;
19
20
use Dcrypt\Exceptions\InvalidKeyException;
21
22
/**
23
 * Provides key derivation functions.
24
 *
25
 * @category Dcrypt
26
 *
27
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
28
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
29
 *
30
 * @link     https://github.com/mmeyer2k/dcrypt
31
 */
32
final class OpensslKey
33
{
34
    /**
35
     * High entropy key.
36
     *
37
     * @var string
38
     */
39
    private $_key;
40
41
    /**
42
     * Algo string.
43
     *
44
     * @var string
45
     */
46
    private $_algo;
47
48
    /**
49
     * High entropy salt.
50
     *
51
     * @var string
52
     */
53
    private $_ivr;
54
55
    /**
56
     * OpensslKey constructor.
57
     *
58
     * @param string $algo Algo to use for HKDF
59
     * @param string $key  Key to use for encryption
60
     * @param string $ivr  Initialization vector
61
     *
62
     * @throws InvalidKeyException
63
     */
64 44
    public function __construct(
65
        string $algo,
66
        string $key,
67
        string $ivr = ''
68
    ) {
69
        // Store the key as what was supplied
70 44
        $this->_key = \base64_decode($key);
71
72 44
        if (Str::strlen($this->_key) < 32) {
73 8
            throw new InvalidKeyException(InvalidKeyException::KEYLENGTH);
74
        }
75
76
        // Store algo in object
77 36
        $this->_algo = $algo;
78
79
        // Store init vector in object
80 36
        $this->_ivr = $ivr;
81 36
    }
82
83
    /**
84
     * Generate the authentication key.
85
     *
86
     * @param string $info The extra info parameter for hash_hkdf
87
     *
88
     * @return string
89
     */
90 33
    public function authenticationKey(string $info): string
91
    {
92 33
        return $this->deriveKey(__FUNCTION__ . '|' . $info);
93
    }
94
95
    /**
96
     * Generate the encryption key.
97
     *
98
     * @param string $info The extra info parameter for hash_hkdf
99
     *
100
     * @return string
101
     */
102 34
    public function encryptionKey(string $info): string
103
    {
104 34
        return $this->deriveKey(__FUNCTION__ . '|' . $info);
105
    }
106
107
    /**
108
     * Derive a key with differing info string parameters.
109
     *
110
     * @param string $info Info parameter to provide to hash_hkdf
111
     *
112
     * @return string
113
     */
114 36
    public function deriveKey(string $info): string
115
    {
116 36
        return \hash_hkdf($this->_algo, $this->_key, 0, $info, $this->_ivr);
117
    }
118
119
    /**
120
     * Generate a new key that meets requirements for dcrypt.
121
     *
122
     * @param int $bytes Size of key in bytes
123
     *
124
     * @throws InvalidKeyException
125
     *
126
     * @return string
127
     */
128 28
    public static function create(int $bytes = 32): string
129
    {
130 28
        if ($bytes < 32) {
131 1
            throw new InvalidKeyException(InvalidKeyException::KEYLENGTH);
132
        }
133
134 28
        return \base64_encode(\random_bytes($bytes));
135
    }
136
}
137