| Total Complexity | 19 |
| Total Lines | 111 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 23 | trait CipherDataFormatsTrait |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Internal method for converting the output format representation via the chosen format. |
||
| 27 | * |
||
| 28 | * @param string $bytes The bytes for conversion. |
||
| 29 | * @param bool|int|null $direction Flag for encryption direction (encrypt => `true` or decrypt => `false`). |
||
| 30 | * |
||
| 31 | * @return string The formatted bytes. |
||
| 32 | */ |
||
| 33 | protected function changeOutputFormat($bytes, $direction = true) |
||
| 34 | { |
||
| 35 | if ($this->cipherFormat === self::ENCRYPTION_OUTPUT_RAW) { |
||
|
1 ignored issue
–
show
|
|||
| 36 | return $bytes; |
||
| 37 | } |
||
| 38 | |||
| 39 | if ($direction == true) { |
||
| 40 | switch ($this->cipherFormat) { |
||
| 41 | case self::ENCRYPTION_OUTPUT_HEX_LOWER: |
||
|
1 ignored issue
–
show
|
|||
| 42 | $bytes = bin2hex($bytes); |
||
| 43 | break; |
||
| 44 | case self::ENCRYPTION_OUTPUT_HEX_UPPER: |
||
|
1 ignored issue
–
show
|
|||
| 45 | $bytes = StringBuilder::stringToUpper(bin2hex($bytes)); |
||
| 46 | break; |
||
| 47 | case self::ENCRYPTION_OUTPUT_BASE_64: |
||
|
1 ignored issue
–
show
|
|||
| 48 | $bytes = base64_encode($bytes); |
||
| 49 | break; |
||
| 50 | case self::ENCRYPTION_OUTPUT_BASE_64_URL: |
||
|
1 ignored issue
–
show
|
|||
| 51 | default: |
||
| 52 | $bytes = base64_encode($bytes); |
||
| 53 | $bytes = StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], $bytes); |
||
| 54 | break; |
||
| 55 | } |
||
| 56 | } else { |
||
| 57 | $hexCasePattern = '/^[a-f0-9]+$/'; |
||
| 58 | $base64Pattern = '%^[a-zA-Z0-9/+]*={0,2}$%'; |
||
| 59 | $base64UrlFriendlyPattern = '/^[a-zA-Z0-9_-]+$/'; |
||
| 60 | |||
| 61 | switch ($this->cipherFormat) { |
||
| 62 | case self::ENCRYPTION_OUTPUT_HEX_LOWER: |
||
| 63 | case self::ENCRYPTION_OUTPUT_HEX_UPPER: |
||
| 64 | if (preg_match($hexCasePattern, StringBuilder::stringToLower($bytes))) { |
||
| 65 | $bytes = hex2bin(StringBuilder::stringToLower($bytes)); |
||
| 66 | } |
||
| 67 | break; |
||
| 68 | case self::ENCRYPTION_OUTPUT_BASE_64: |
||
| 69 | if (preg_match($base64Pattern, $bytes) && StringBuilder::stringLength($bytes) % 4 === 0) { |
||
| 70 | $bytes = base64_decode($bytes); |
||
| 71 | } |
||
| 72 | break; |
||
| 73 | case self::ENCRYPTION_OUTPUT_BASE_64_URL: |
||
| 74 | default: |
||
| 75 | if (preg_match($base64UrlFriendlyPattern, $bytes)) { |
||
| 76 | $bytes = StringBuilder::stringReplace(['-', '_'], ['+', '/'], $bytes); |
||
| 77 | $times = StringBuilder::stringLength($bytes) % 4; |
||
| 78 | |||
| 79 | // Instead of str_pad for encoding friendly appending |
||
| 80 | for ($i = 0; $i < $times; $i++) { |
||
| 81 | $bytes .= '='; |
||
| 82 | } |
||
| 83 | |||
| 84 | $bytes = base64_decode($bytes); |
||
| 85 | } |
||
| 86 | break; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | return $bytes; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Setter for the output cipher format code property. |
||
| 95 | * |
||
| 96 | * @param int $cipherFormat The output cipher format code. |
||
| 97 | * |
||
| 98 | * @return $this The encryption algorithm object. |
||
| 99 | * @throws \Exception Validation errors. |
||
| 100 | */ |
||
| 101 | public function setCipherFormat($cipherFormat) |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Getter for the output cipher format code property. |
||
| 128 | * |
||
| 129 | * @return int The output cipher format code. |
||
| 130 | */ |
||
| 131 | public function getCipherFormat() |
||
| 136 |