Completed
Pull Request — master (#32)
by Michael
01:13
created

OpensslKey::messageChecksum()   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
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
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\InvalidKeyEncodingException;
21
use Dcrypt\Exceptions\InvalidKeyLengthException;
22
use Dcrypt\Exceptions\InvalidPropertyAccessException;
23
use Exception;
24
25
/**
26
 * Provides key derivation functions.
27
 *
28
 * @category Dcrypt
29
 *
30
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
31
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
32
 *
33
 * @link     https://github.com/mmeyer2k/dcrypt
34
 */
35
final class OpensslKey
36
{
37
    /**
38
     * High entropy key.
39
     *
40
     * @var string
41
     */
42
    private $_key;
43
44
    /**
45
     * Algo string.
46
     *
47
     * @var string
48
     */
49
    private $_algo;
50
51
    /**
52
     * High entropy salt.
53
     *
54
     * @var string
55
     */
56
    private $_iv;
57
58
    /**
59
     * Name of cipher.
60
     *
61
     * @var string
62
     */
63
    private $_cipher;
64
65
    /**
66
     * OpensslKey constructor.
67
     *
68
     * @param string $key    Key to use for encryption
69
     * @param string $algo   Algo to use for HKDF
70
     * @param string $cipher Name of cipher
71
     * @param string $iv     Initialization vector
72
     *
73
     * @throws InvalidKeyLengthException
74
     * @throws InvalidKeyEncodingException
75
     */
76 48
    public function __construct(
77
        string $key,
78
        string $algo,
79
        string $cipher = '',
80
        string $iv = ''
81
    ) {
82
        // Store the key as what was supplied
83 48
        $this->_key = base64_decode($key, true);
84
85
        // If key was not proper base64, bail out
86 48
        if ($this->_key === false) {
87 9
            throw new InvalidKeyEncodingException();
88
        }
89
90
        // If key was to short, bail out
91 39
        if (Str::strlen($this->_key) < 32) {
92
            throw new InvalidKeyLengthException();
93
        }
94
95
        // Store algo in object
96 39
        $this->_algo = $algo;
97
98
        // Store init vector in object
99 39
        $this->_iv = $iv;
100
101
        // Store the cipher name
102 39
        $this->_cipher = $cipher;
103 39
    }
104
105
    /**
106
     * Generate the authentication key.
107
     *
108
     * @return string
109
     */
110 36
    public function authenticationKey(): string
111
    {
112 36
        return $this->deriveKey(__FUNCTION__ . '|' . $this->_cipher);
113
    }
114
115
    /**
116
     * Generate the encryption key.
117
     *
118
     * @return string
119
     */
120 37
    public function encryptionKey(): string
121
    {
122 37
        return $this->deriveKey(__FUNCTION__ . '|' . $this->_cipher);
123
    }
124
125
    /**
126
     * Derive a key with differing info string parameters.
127
     *
128
     * @param string $info Info parameter to provide to hash_hkdf
129
     *
130
     * @return string
131
     */
132 39
    public function deriveKey(string $info): string
133
    {
134 39
        return hash_hkdf($this->_algo, $this->_key, 0, $info, $this->_iv);
135
    }
136
137
    /**
138
     * Calculates a given message HMAC.
139
     *
140
     * @param string $message
141
     *
142
     * @return string
143
     */
144 36
    public function messageChecksum(string $message): string
145
    {
146 36
        return hash_hmac($this->_algo, $message, $this->authenticationKey(), true);
147
    }
148
149
    /**
150
     * Allows read only access to the internal variables needed by the openssl wrapper.
151
     *
152
     * @return array
153
     */
154 37
    public function wrapperVariables(): array
155
    {
156
        return [
157 37
            $this->_iv,
158 37
            $this->encryptionKey(),
159 36
            $this->_cipher,
160
        ];
161
    }
162
163
    /**
164
     * Generate a new key.
165
     *
166
     * @param int $bytes Size of key in bytes
167
     *
168
     * @throws Exception
169
     * @throws InvalidKeyLengthException
170
     *
171
     * @return string
172
     */
173 31
    public static function create(int $bytes = 32): string
174
    {
175 31
        if ($bytes < 32) {
176 1
            throw new InvalidKeyLengthException();
177
        }
178
179 31
        return base64_encode(random_bytes($bytes));
180
    }
181
}
182