| Total Complexity | 13 |
| Total Lines | 130 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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
|
|||
| 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
|
|||
| 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
|
|||
| 76 | self::CFB_MODE, |
||
|
1 ignored issue
–
show
|
|||
| 77 | self::OFB_MODE, |
||
|
1 ignored issue
–
show
|
|||
| 78 | self::CTR_MODE, |
||
|
1 ignored issue
–
show
|
|||
| 79 | self::ECB_MODE |
||
|
1 ignored issue
–
show
|
|||
| 80 | ]; |
||
| 81 | |||
| 82 | if (!is_string($mode) || !in_array(strtoupper($mode), $validModes, true)) { |
||
|
1 ignored issue
–
show
|
|||
| 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
|
|||
| 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) |
||
| 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() |
||
| 154 | } |
||
| 155 | } |
||
| 156 |