Total Complexity | 21 |
Total Lines | 153 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
23 | trait SignatureDataFormatsTrait |
||
24 | { |
||
25 | /** |
||
26 | * Internal method for converting format after signing 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 | 112 | protected function signingFormat(&$bytes) |
|
33 | { |
||
34 | 112 | switch ($this->signatureFormat) { |
|
35 | 112 | case self::SIGNATURE_OUTPUT_HEX_LOWER: |
|
1 ignored issue
–
show
|
|||
36 | 24 | $bytes = bin2hex($bytes); |
|
37 | 24 | break; |
|
38 | 96 | case self::SIGNATURE_OUTPUT_HEX_UPPER: |
|
1 ignored issue
–
show
|
|||
39 | 96 | $bytes = StringBuilder::stringToUpper(bin2hex($bytes)); |
|
40 | 96 | break; |
|
41 | 8 | case self::SIGNATURE_OUTPUT_BASE_64: |
|
1 ignored issue
–
show
|
|||
42 | 8 | $bytes = base64_encode($bytes); |
|
43 | 8 | break; |
|
44 | 8 | case self::SIGNATURE_OUTPUT_BASE_64_URL: |
|
1 ignored issue
–
show
|
|||
45 | 8 | $bytes = StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], base64_encode($bytes)); |
|
46 | 8 | break; |
|
47 | } |
||
48 | 112 | } |
|
49 | |||
50 | /** |
||
51 | * Internal method for converting from HEX formatted string after verification operations. |
||
52 | * |
||
53 | * @param string $bytes The bytes for conversion. |
||
54 | * |
||
55 | * @internal The parameter is passed via reference from the main logical method for performance reasons. |
||
56 | */ |
||
57 | 72 | protected function verificationFormatHex(&$bytes) |
|
58 | { |
||
59 | 72 | if (preg_match('/^[a-f0-9]+$/', $bytes)) { |
|
60 | 24 | $bytes = hex2bin($bytes); |
|
61 | 56 | } elseif (preg_match('/^[A-F0-9]+$/', $bytes)) { |
|
62 | 48 | $bytes = hex2bin(StringBuilder::stringToLower($bytes)); |
|
63 | } |
||
64 | 72 | } |
|
65 | |||
66 | /** |
||
67 | * Internal method for converting from Base64 formatted string after verification operations. |
||
68 | * |
||
69 | * @param string $bytes The bytes for conversion. |
||
70 | * |
||
71 | * @internal The parameter is passed via reference from the main logical method for performance reasons. |
||
72 | */ |
||
73 | 8 | protected function verificationFormatBase64(&$bytes) |
|
81 | } |
||
82 | 8 | } |
|
83 | |||
84 | /** |
||
85 | * Internal method for converting format after verification operations. |
||
86 | * |
||
87 | * @param string $bytes The bytes for conversion. |
||
88 | * |
||
89 | * @internal The parameter is passed via reference from the main logical method for performance reasons. |
||
90 | */ |
||
91 | 72 | protected function verificationFormat(&$bytes) |
|
109 | } |
||
110 | 72 | } |
|
111 | |||
112 | /** |
||
113 | * Internal method for converting the output format representation via the chosen format. |
||
114 | * |
||
115 | * @param string $bytes The bytes for conversion. |
||
116 | * @param bool|int|null $direction Flag for signing direction (sign => `true` or verify => `false`). |
||
117 | * |
||
118 | * @return string The formatted bytes. |
||
119 | */ |
||
120 | 120 | protected function changeOutputFormat($bytes, $direction = true) |
|
121 | { |
||
122 | 120 | if ($this->signatureFormat === self::SIGNATURE_OUTPUT_RAW) { |
|
1 ignored issue
–
show
|
|||
123 | 8 | return $bytes; |
|
124 | } |
||
125 | |||
126 | 120 | if ($direction == true) { |
|
127 | 112 | $this->signingFormat($bytes); |
|
128 | } else { |
||
129 | 72 | $this->verificationFormat($bytes); |
|
130 | } |
||
131 | |||
132 | 120 | return $bytes; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Setter for the output signature format code property. |
||
137 | * |
||
138 | * @param int $signatureFormat The output signature format code. |
||
139 | * |
||
140 | * @return $this The signature algorithm object. |
||
141 | * @throws \Exception Validation errors. |
||
142 | */ |
||
143 | 32 | public function setSignatureFormat($signatureFormat) |
|
144 | { |
||
145 | 32 | $signatureFormat = filter_var( |
|
146 | 32 | $signatureFormat, |
|
147 | 32 | FILTER_VALIDATE_INT, |
|
148 | [ |
||
149 | "options" => [ |
||
150 | 32 | "min_range" => self::SIGNATURE_OUTPUT_RAW, |
|
1 ignored issue
–
show
|
|||
151 | 32 | "max_range" => self::SIGNATURE_OUTPUT_BASE_64_URL, |
|
1 ignored issue
–
show
|
|||
152 | ], |
||
153 | ] |
||
154 | ); |
||
155 | |||
156 | 32 | if ($signatureFormat === false) { |
|
157 | 8 | throw new \InvalidArgumentException( |
|
158 | 'The output format mode must be an integer between ' . |
||
159 | 8 | self::SIGNATURE_OUTPUT_RAW . ' and ' . self::SIGNATURE_OUTPUT_BASE_64_URL . '.' |
|
160 | ); |
||
161 | } |
||
162 | |||
163 | 24 | $this->signatureFormat = $signatureFormat; |
|
164 | |||
165 | 24 | return $this; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Getter for the output signature format code property. |
||
170 | * |
||
171 | * @return int The output signature format code. |
||
172 | */ |
||
173 | 24 | public function getSignatureFormat() |
|
176 | } |
||
177 | } |
||
178 |