Total Complexity | 21 |
Total Lines | 135 |
Duplicated Lines | 0 % |
Coverage | 100% |
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: |
|
1 ignored issue
–
show
|
|||
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: |
|
1 ignored issue
–
show
|
|||
45 | default: |
||
46 | 120 | $bytes = base64_encode($bytes); |
|
47 | 120 | $bytes = StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], $bytes); |
|
48 | 120 | 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 | 96 | $hexCasePattern = '/^[a-f0-9]+$/'; |
|
62 | 96 | $base64Pattern = '%^[a-zA-Z0-9/+]*={0,2}$%'; |
|
63 | 96 | $base64UrlFriendlyPattern = '/^[a-zA-Z0-9_-]+$/'; |
|
64 | |||
65 | 96 | switch ($this->cipherFormat) { |
|
66 | 96 | case self::ENCRYPTION_OUTPUT_HEX_LOWER: |
|
67 | 96 | case self::ENCRYPTION_OUTPUT_HEX_UPPER: |
|
1 ignored issue
–
show
|
|||
68 | 12 | if (preg_match($hexCasePattern, StringBuilder::stringToLower($bytes))) { |
|
69 | 12 | $bytes = hex2bin(StringBuilder::stringToLower($bytes)); |
|
70 | } |
||
71 | 12 | break; |
|
72 | 96 | case self::ENCRYPTION_OUTPUT_BASE_64: |
|
1 ignored issue
–
show
|
|||
73 | 12 | if (preg_match($base64Pattern, $bytes) && StringBuilder::stringLength($bytes) % 4 === 0) { |
|
74 | 12 | $bytes = base64_decode($bytes); |
|
75 | } |
||
76 | 12 | break; |
|
77 | 96 | case self::ENCRYPTION_OUTPUT_BASE_64_URL: |
|
78 | default: |
||
79 | 96 | if (preg_match($base64UrlFriendlyPattern, $bytes)) { |
|
80 | 84 | $bytes = StringBuilder::stringReplace(['-', '_'], ['+', '/'], $bytes); |
|
81 | 84 | $times = StringBuilder::stringLength($bytes) % 4; |
|
82 | |||
83 | // Instead of str_pad for encoding friendly appending |
||
84 | 84 | for ($i = 0; $i < $times; $i++) { |
|
85 | 72 | $bytes .= '='; |
|
86 | } |
||
87 | |||
88 | 84 | $bytes = base64_decode($bytes); |
|
89 | } |
||
90 | 96 | break; |
|
91 | } |
||
92 | 96 | } |
|
93 | |||
94 | /** |
||
95 | * Internal method for converting the output format representation via the chosen format. |
||
96 | * |
||
97 | * @param string $bytes The bytes for conversion. |
||
98 | * @param bool|int|null $direction Flag for encryption direction (encrypt => `true` or decrypt => `false`). |
||
99 | * |
||
100 | * @return string The formatted bytes. |
||
101 | */ |
||
102 | 156 | protected function changeOutputFormat($bytes, $direction = true) |
|
103 | { |
||
104 | 156 | if ($this->cipherFormat === self::ENCRYPTION_OUTPUT_RAW) { |
|
105 | 36 | return $bytes; |
|
106 | } |
||
107 | |||
108 | 132 | if ($direction == true) { |
|
109 | 120 | $this->encryptionFormat($bytes); |
|
110 | } else { |
||
111 | 96 | $this->decryptionFormat($bytes); |
|
112 | } |
||
113 | |||
114 | 132 | return $bytes; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Setter for the output cipher format code property. |
||
119 | * |
||
120 | * @param int $cipherFormat The output cipher format code. |
||
121 | * |
||
122 | * @return $this The encryption algorithm object. |
||
123 | * @throws \Exception Validation errors. |
||
124 | */ |
||
125 | 48 | public function setCipherFormat($cipherFormat) |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Getter for the output cipher format code property. |
||
152 | * |
||
153 | * @return int The output cipher format code. |
||
154 | */ |
||
155 | 48 | public function getCipherFormat() |
|
160 |