Completed
Push — master ( 539cfe...32b8af )
by Michael
03:42 queued 02:23
created

OpensslWrapper::openssl_encrypt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 5
1
<?php
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
class OpensslWrapper
18
{
19
20
    /**
21
     * OpenSSL encrypt wrapper function
22
     *
23
     * @param string $input  Data to decrypt
24
     * @param string $method Cipher method to use
25
     * @param string $key    Key string
26
     * @param string $iv     Initialization vector
27
     * @return string
28
     */
29 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...
30
    {
31
        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...
32
            $ret = \openssl_encrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv, $tag, '', 4);
33
        } else {
34
            $ret = \openssl_encrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv);
35
        }
36
37
        return self::returnOrException($ret);
38
    }
39
40
    /**
41
     * OpenSSL decrypt wrapper function
42
     *
43
     * @param string $input  Data to decrypt
44
     * @param string $method Cipher method to use
45
     * @param string $key    Key string
46
     * @param string $iv     Initialization vector
47
     * @return string
48
     */
49 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...
50
    {
51
        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...
52
            $ret = \openssl_decrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv, $tag, '');
53
        } else {
54
            $ret = \openssl_decrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv);
55
        }
56
57
        return self::returnOrException($ret);
58
    }
59
60
    /**
61
     * Throw an exception if openssl function returns false
62
     *
63
     * @param string|bool $data
64
     * @return string
65
     * @throws \Exception
66
     */
67
    protected static function returnOrException($data): string
68
    {
69
        if ($data === false) {
70
            throw new \Exception('OpenSSL failed to encrypt/decrypt message.');
71
        }
72
73
        return $data;
74
    }
75
76
    /**
77
     * Get IV size for specified CIPHER.
78
     *
79
     * @param string $cipher
80
     * @return int
81
     * @throws \Exception
82
     */
83
    protected static function ivsize(string $cipher): int
84
    {
85
        $ret = \openssl_cipher_iv_length($cipher);
86
87
        if ($ret === false) {
88
            throw new \Exception("Failed to determine correct IV size.");
89
        }
90
91
        return $ret;
92
    }
93
94
    /**
95
     * Determines if the provided cipher requires a tag
96
     *
97
     * @param string $cipher
98
     * @return bool
99
     */
100
    protected static function tagRequired(string $cipher): bool
101
    {
102
        $cipher = strtolower($cipher);
103
104
        $needle_tips = [
105
            '-gcm',
106
            '-ccm',
107
        ];
108
109
        foreach ($needle_tips as $needle) {
110
            if (strpos($cipher, $needle)) {
111
                return true;
112
            }
113
        }
114
115
        return false;
116
    }
117
}
118