Completed
Push — master ( e42700...ce8c78 )
by Michael
04:37 queued 03:09
created

OpensslKey::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.6333
cc 3
nc 2
nop 4
crap 3
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
     * @var string
47
     */
48
    private $cipher;
49
50
    /**
51
     * OpensslKey constructor.
52
     *
53
     * @param string $algo   Algo to use for HKDF
54
     * @param string $key    Key
55
     * @param string $cipher Openssl cipher
56
     * @param string $ivr    Initialization vactor
57
     * @throws Exceptions\InvalidKeyException
58
     */
59 32
    public function __construct(string $algo, string $key, string $cipher, string $ivr)
60
    {
61
        // Store the key as what was supplied
62 32
        $this->key = \base64_decode($key);
63
64
        // Make sure key was properly decoded and meets minimum required length
65 32
        if (!is_string($this->key) || Str::strlen($this->key) < 256) {
66 5
            throw new InvalidKeyException("Key must be at least 256 bytes and base64 encoded.");
67
        }
68
69
        // Store the cipher string
70 27
        $this->cipher = $cipher;
71
72
        // Store algo in object
73 27
        $this->algo = $algo;
74
75
        // Store init vector in object
76 27
        $this->ivr = $ivr;
77 27
    }
78
79
    /**
80
     * Generate the authentication key
81
     *
82
     * @return string
83
     */
84 24
    public function authenticationKey(): string
85
    {
86 24
        return $this->deriveKey(__FUNCTION__);
87
    }
88
89
    /**
90
     * Generate the encryption key
91
     *
92
     * @return string
93
     */
94 25
    public function encryptionKey(): string
95
    {
96 25
        return $this->deriveKey(__FUNCTION__);
97
    }
98
99
    /**
100
     * Derive a key with differing authinfo strings
101
     *
102
     * @param string $info Info parameter to provide to hash_hkdf
103
     * @return string
104
     */
105 27
    public function deriveKey(string $info): string
106
    {
107 27
        $info = $info . '|' . $this->cipher;
108
109 27
        $key = \hash_hkdf($this->algo, $this->key, 0, $info, $this->ivr);
110
111 26
        return $key;
112
    }
113
114
    /**
115
     * Generate a new key that meets requirements for dcrypt
116
     *
117
     * @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...
118
     * @return string
119
     * @throws Exceptions\InvalidKeyException
120
     */
121 21
    public static function newKey(int $bytes = 256): string
122
    {
123 21
        if ($bytes < 256) {
124 1
            throw new InvalidKeyException('Keys must be at least 256 bytes long.');
125
        }
126
127 21
        return \base64_encode(\random_bytes($bytes));
128
    }
129
}