Completed
Push — master ( e5d930...4cc314 )
by Michael
01:28
created

OpensslWrapper::ivGenerate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * OpensslWrapper.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
/**
21
 * A wrapper around any openssl_* functions.
22
 *
23
 * @category Dcrypt
24
 *
25
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
26
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
27
 *
28
 * @link     https://github.com/mmeyer2k/dcrypt
29
 */
30
class OpensslWrapper
31
{
32
    /**
33
     * OpenSSL encrypt wrapper function.
34
     *
35
     * @param string     $data Data to decrypt
36
     * @param OpensslKey $key  Key string
37
     * @param string     $tag  AAD tag
38
     *
39
     * @return string
40
     */
41 28 View Code Duplication
    protected static function opensslEncrypt(string $data, OpensslKey $key, string &$tag): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43 28
        list($iv, $enc, $cipher) = $key->wrapperVariables();
44
45 27
        if (self::tagLength($cipher) > 0) {
46 12
            return openssl_encrypt($data, $cipher, $enc, 1, $iv, $tag, '', 16);
47
        }
48
49 17
        return openssl_encrypt($data, $cipher, $enc, 1, $iv);
50
    }
51
52
    /**
53
     * OpenSSL decrypt wrapper function.
54
     *
55
     * @param string     $input Data to decrypt
56
     * @param OpensslKey $key   Key string
57
     * @param string     $tag   AAD authentication tag
58
     *
59
     * @return string
60
     */
61 35 View Code Duplication
    protected static function opensslDecrypt(string $input, OpensslKey $key, string $tag): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63 35
        list($iv, $enc, $cipher) = $key->wrapperVariables();
64
65 35
        if (self::tagLength($cipher) > 0) {
66 15
            return openssl_decrypt($input, $cipher, $enc, 1, $iv, $tag, '');
67
        }
68
69 23
        return openssl_decrypt($input, $cipher, $enc, 1, $iv);
70
    }
71
72
    /**
73
     * Get IV size for specified CIPHER.
74
     *
75
     * @param string $cipher Openssl cipher
76
     *
77
     * @return int
78
     */
79 46
    protected static function ivSize(string $cipher): int
80
    {
81 46
        return openssl_cipher_iv_length($cipher);
82
    }
83
84
    /**
85
     * Get a correctly sized IV for the specified cipher.
86
     *
87
     * @param string $cipher Openssl cipher
88
     *
89
     * @throws \Exception
90
     *
91
     * @return string
92
     */
93 37
    protected static function ivGenerate(string $cipher): string
94
    {
95 37
        $size = self::ivSize($cipher);
96
97 36
        if ($size === 0) {
98 6
            return '';
99
        }
100
101 32
        return random_bytes($size);
102
    }
103
104
    /**
105
     * Determines if the provided cipher requires a tag.
106
     *
107
     * @param string $cipher Openssl cipher
108
     *
109
     * @return int
110
     */
111 36
    protected static function tagLength(string $cipher): int
112
    {
113 36
        return stripos($cipher, '-gcm') || stripos($cipher, '-ccm') ? 16 : 0;
114
    }
115
}
116