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

OpensslWrapper::returnOrException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
1
<?php declare(strict_types=1);
2
3
/**
4
 * OpensslWrapper.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
/**
18
 * A wrapper around any openssl_* functions.
19
 *
20
 * @category Dcrypt
21
 * @package  Dcrypt
22
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
23
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
24
 * @link     https://github.com/mmeyer2k/dcrypt
25
 */
26
class OpensslWrapper
27
{
28
    /**
29
     * OpenSSL encrypt wrapper function
30
     *
31
     * @param string $input  Data to decrypt
32
     * @param string $method Cipher method to use
33
     * @param string $key    Key string
34
     * @param string $iv     Initialization vector
35
     * @return string
36
     */
37 17 View Code Duplication
    protected static function openssl_encrypt(string $input, string $method, string $key, string $iv, 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...
38
    {
39 17
        if (OpensslStatic::tagRequired($method)) {
0 ignored issues
show
Bug introduced by
The method tagRequired() cannot be called from this context as it is declared protected in class Dcrypt\OpensslWrapper.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
40 5
            return \openssl_encrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv, $tag, '', 4);
41
        } else {
42 14
            return \openssl_encrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv);
43
        }
44
    }
45
46
    /**
47
     * OpenSSL decrypt wrapper function
48
     *
49
     * @param string $input  Data to decrypt
50
     * @param string $method Cipher method to use
51
     * @param string $key    Key string
52
     * @param string $iv     Initialization vector
53
     * @return string
54
     */
55 24 View Code Duplication
    protected static function openssl_decrypt(string $input, string $method, string $key, string $iv, 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...
56
    {
57 24
        if (OpensslStatic::tagRequired($method)) {
0 ignored issues
show
Bug introduced by
The method tagRequired() cannot be called from this context as it is declared protected in class Dcrypt\OpensslWrapper.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
58 8
            return \openssl_decrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv, $tag, '');
59
        } else {
60 19
            return \openssl_decrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv);
61
        }
62
    }
63
64
    /**
65
     * Get IV size for specified CIPHER.
66
     *
67
     * @param string $cipher
68
     * @return int
69
     */
70 31
    protected static function ivSize(string $cipher): int
71
    {
72 31
        $ret = \openssl_cipher_iv_length($cipher);
73
74 30
        return $ret;
75
    }
76
77
    /**
78
     * Get a correctly sized IV for the specified cipher
79
     *
80
     * @param string $cipher
81
     * @return string
82
     * @throws \Exception
83
     */
84 24
    protected static function ivGenerate(string $cipher): string
85
    {
86 24
        $size = self::ivSize($cipher);
87
88 23
        if ($size === 0) {
89 6
            return '';
90
        }
91
92 19
        return \random_bytes($size);
93
    }
94
95
    /**
96
     * Determines if the provided cipher requires a tag
97
     *
98
     * @param string $cipher
99
     * @return bool
100
     */
101 24
    protected static function tagRequired(string $cipher): bool
102
    {
103 24
        $cipher = strtolower($cipher);
104
105
        $needle_tips = [
106 24
            '-gcm',
107
            '-ccm',
108
        ];
109
110 24
        foreach ($needle_tips as $needle) {
111 24
            if (strpos($cipher, $needle)) {
112 24
                return true;
113
            }
114
        }
115
116 19
        return false;
117
    }
118
}
119