| Total Complexity | 20 |
| Total Lines | 124 |
| Duplicated Lines | 0 % |
| Coverage | 98.04% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 23 | trait CipherDataFormatsTrait |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Internal method for converting format after encryption operations. |
||
| 27 | * |
||
| 28 | * @param string $bytes The bytes for conversion. |
||
| 29 | * |
||
| 30 | * @internal The parameter is passed via reference from the main logical method for performance reasons. |
||
| 31 | */ |
||
| 32 | 120 | protected function encryptionFormat(&$bytes) |
|
| 33 | { |
||
| 34 | 120 | switch ($this->cipherFormat) { |
|
| 35 | 120 | case self::ENCRYPTION_OUTPUT_HEX_LOWER: |
|
| 36 | 12 | $bytes = bin2hex($bytes); |
|
| 37 | 12 | break; |
|
| 38 | 120 | case self::ENCRYPTION_OUTPUT_HEX_UPPER: |
|
| 39 | 12 | $bytes = StringBuilder::stringToUpper(bin2hex($bytes)); |
|
| 40 | 12 | break; |
|
| 41 | 120 | case self::ENCRYPTION_OUTPUT_BASE_64: |
|
| 42 | 12 | $bytes = base64_encode($bytes); |
|
| 43 | 12 | break; |
|
| 44 | 120 | case self::ENCRYPTION_OUTPUT_BASE_64_URL: |
|
| 45 | 120 | $bytes = StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], base64_encode($bytes)); |
|
| 46 | 120 | break; |
|
| 47 | default: // Then nothing |
||
| 48 | break; |
||
| 49 | } |
||
| 50 | 120 | } |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Internal method for converting format after decryption operations. |
||
| 54 | * |
||
| 55 | * @param string $bytes The bytes for conversion. |
||
| 56 | * |
||
| 57 | * @internal The parameter is passed via reference from the main logical method for performance reasons. |
||
| 58 | */ |
||
| 59 | 96 | protected function decryptionFormat(&$bytes) |
|
| 60 | { |
||
| 61 | $isHex = ( |
||
| 62 | 96 | $this->cipherFormat === self::ENCRYPTION_OUTPUT_HEX_LOWER || |
|
|
1 ignored issue
–
show
|
|||
| 63 | 96 | $this->cipherFormat === self::ENCRYPTION_OUTPUT_HEX_UPPER |
|
|
1 ignored issue
–
show
|
|||
| 64 | ); |
||
| 65 | |||
| 66 | 96 | if ($isHex) { |
|
| 67 | 12 | if (preg_match('/^[a-f0-9]+$/', StringBuilder::stringToLower($bytes))) { |
|
| 68 | 12 | $bytes = hex2bin(StringBuilder::stringToLower($bytes)); |
|
| 69 | } |
||
| 70 | 96 | } elseif ($this->cipherFormat === self::ENCRYPTION_OUTPUT_BASE_64) { |
|
|
1 ignored issue
–
show
|
|||
| 71 | 12 | if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $bytes) && StringBuilder::stringLength($bytes) % 4 === 0) { |
|
| 72 | 12 | $bytes = base64_decode($bytes); |
|
| 73 | } |
||
| 74 | 96 | } elseif ($this->cipherFormat === self::ENCRYPTION_OUTPUT_BASE_64_URL) { |
|
|
1 ignored issue
–
show
|
|||
| 75 | 96 | if (preg_match('/^[a-zA-Z0-9_-]+$/', $bytes)) { |
|
| 76 | 84 | $bytes = StringBuilder::stringReplace(['-', '_'], ['+', '/'], $bytes); |
|
| 77 | 84 | $bytes .= str_repeat('=', StringBuilder::stringLength($bytes) % 4); |
|
| 78 | 84 | $bytes = base64_decode($bytes); |
|
| 79 | } |
||
| 80 | } |
||
| 81 | 96 | } |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Internal method for converting the output format representation via the chosen format. |
||
| 85 | * |
||
| 86 | * @param string $bytes The bytes for conversion. |
||
| 87 | * @param bool|int|null $direction Flag for encryption direction (encrypt => `true` or decrypt => `false`). |
||
| 88 | * |
||
| 89 | * @return string The formatted bytes. |
||
| 90 | */ |
||
| 91 | 156 | protected function changeOutputFormat($bytes, $direction = true) |
|
| 92 | { |
||
| 93 | 156 | if ($this->cipherFormat === self::ENCRYPTION_OUTPUT_RAW) { |
|
| 94 | 36 | return $bytes; |
|
| 95 | } |
||
| 96 | |||
| 97 | 132 | if ($direction == true) { |
|
| 98 | 120 | $this->encryptionFormat($bytes); |
|
| 99 | } else { |
||
| 100 | 96 | $this->decryptionFormat($bytes); |
|
| 101 | } |
||
| 102 | |||
| 103 | 132 | return $bytes; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Setter for the output cipher format code property. |
||
| 108 | * |
||
| 109 | * @param int $cipherFormat The output cipher format code. |
||
| 110 | * |
||
| 111 | * @return $this The encryption algorithm object. |
||
| 112 | * @throws \Exception Validation errors. |
||
| 113 | */ |
||
| 114 | 48 | public function setCipherFormat($cipherFormat) |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Getter for the output cipher format code property. |
||
| 141 | * |
||
| 142 | * @return int The output cipher format code. |
||
| 143 | */ |
||
| 144 | 48 | public function getCipherFormat() |
|
| 149 |