Completed
Push — master ( 8055c8...3e2656 )
by Tony Karavasilev (Тони
16:45
created

BlockOperationsTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 39
c 1
b 0
f 0
dl 0
loc 130
ccs 0
cts 63
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setPaddingStandard() 0 23 2
A getPaddingStandard() 0 3 1
A setBlockOperationMode() 0 29 4
A getBlockOperationMode() 0 3 1
A setInitializationVector() 0 18 4
A getInitializationVector() 0 3 1
1
<?php
2
3
/**
4
 * Trait implementation of the block cipher capabilities and actions for symmetric encryption algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageEncryption;
8
9
use \CryptoManana\Core\Interfaces\MessageEncryption\BlockOperationsInterface as BlockOperationsSpecification;
10
11
/**
12
 * Trait BlockOperationsTrait - Reusable implementation of `BlockOperationsInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\BlockOperationsInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\MessageEncryption
17
 *
18
 * @property string $iv The initialization vector (IV) string property storage.
19
 * @property string $mode The block encryption operation mode string property.
20
 * @property int $padding The final block padding operation property.
21
 *
22
 * @mixin BlockOperationsSpecification
23
 */
24
trait BlockOperationsTrait
25
{
26
    /**
27
     * Setter for the initialization vector (IV) string property.
28
     *
29
     * @param string $iv The initialization vector (IV) string.
30
     *
31
     * @return $this The encryption algorithm object.
32
     * @throws \Exception Validation errors.
33
     */
34
    public function setInitializationVector($iv)
35
    {
36
        if (!is_string($iv)) {
1 ignored issue
show
introduced by
The condition is_string($iv) is always true.
Loading history...
37
            throw new \InvalidArgumentException('The initialization vector must be a string or a binary string.');
38
        }
39
40
        /**
41
         * {@internal The encryption standard is 8-bit wise (don not use StringBuilder) and utilizes performance. }}
42
         */
43
        if (strlen($iv) > static::IV_SIZE) {
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...perationsTrait::IV_SIZE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
44
            $iv = hash_hkdf('sha256', $iv, static::IV_SIZE, 'CryptoMañana', '');
45
        } elseif (strlen($iv) < static::IV_SIZE) {
46
            $iv = str_pad($iv, static::IV_SIZE, "\x0", STR_PAD_RIGHT);
47
        }
48
49
        $this->iv = $iv;
50
51
        return $this;
52
    }
53
54
    /**
55
     * Getter for the initialization vector (IV) string property.
56
     *
57
     * @return string The initialization vector (IV) string.
58
     */
59
    public function getInitializationVector()
60
    {
61
        return $this->iv;
62
    }
63
64
    /**
65
     * Setter for the block encryption operation mode string property.
66
     *
67
     * @param string $mode The block operation mode string.
68
     *
69
     * @return $this The encryption algorithm object.
70
     * @throws \Exception Validation errors.
71
     */
72
    public function setBlockOperationMode($mode)
73
    {
74
        $validModes = [
75
            self::CBC_MODE,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...erationsTrait::CBC_MODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
76
            self::CFB_MODE,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...erationsTrait::CFB_MODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
77
            self::OFB_MODE,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...erationsTrait::OFB_MODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
            self::CTR_MODE,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...erationsTrait::CTR_MODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
79
            self::ECB_MODE
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...erationsTrait::ECB_MODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
80
        ];
81
82
        if (!is_string($mode) || !in_array(strtoupper($mode), $validModes, true)) {
1 ignored issue
show
introduced by
The condition is_string($mode) is always true.
Loading history...
83
            throw new \InvalidArgumentException(
84
                'The mode of operation must be a string and be a standardized block mode name.'
85
            );
86
        }
87
88
        $newMethodName = static::ALGORITHM_NAME . '-' . (static::KEY_SIZE * 8) . '-' . $mode;
2 ignored issues
show
Bug introduced by
The constant CryptoManana\Core\Traits...erationsTrait::KEY_SIZE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant CryptoManana\Core\Traits...nsTrait::ALGORITHM_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
89
90
        // @codeCoverageIgnoreStart
91
        if (!in_array($newMethodName, openssl_get_cipher_methods(), true)) {
92
            throw new \RuntimeException(
93
                'The algorithm `' . $newMethodName . '`is not supported under the current system configuration.'
94
            );
95
        }
96
        // @codeCoverageIgnoreEnd
97
98
        $this->mode = strtoupper($mode);
99
100
        return $this;
101
    }
102
103
    /**
104
     * Getter for the block encryption operation mode string property.
105
     *
106
     * @return string The block operation mode string.
107
     */
108
    public function getBlockOperationMode()
109
    {
110
        return $this->mode;
111
    }
112
113
    /**
114
     * Setter for the final block padding operation property.
115
     *
116
     * @param int $padding The padding standard integer code value.
117
     *
118
     * @return $this The encryption algorithm object.
119
     * @throws \Exception Validation errors.
120
     */
121
    public function setPaddingStandard($padding)
122
    {
123
        $padding = filter_var(
124
            $padding,
125
            FILTER_VALIDATE_INT,
126
            [
127
                "options" => [
128
                    "min_range" => self::PKCS7_PADDING,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...onsTrait::PKCS7_PADDING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
129
                    "max_range" => self::ZERO_PADDING,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...ionsTrait::ZERO_PADDING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
130
                ],
131
            ]
132
        );
133
134
        if ($padding === false) {
135
            throw new \InvalidArgumentException(
136
                'The padding standard must must be a valid integer between ' .
137
                self::PKCS7_PADDING . ' and ' . self::ZERO_PADDING . '.'
138
            );
139
        }
140
141
        $this->padding = $padding;
142
143
        return $this;
144
    }
145
146
    /**
147
     * Getter for the final block padding operation property.
148
     *
149
     * @return string The padding standard integer code value.
150
     */
151
    public function getPaddingStandard()
152
    {
153
        return $this->padding;
154
    }
155
}
156