1 | <?php
|
---|
2 | declare(strict_types=1);
|
---|
3 | namespace ParagonIE\ConstantTime;
|
---|
4 |
|
---|
5 | use TypeError;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
---|
9 | * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
---|
10 | *
|
---|
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
12 | * of this software and associated documentation files (the "Software"), to deal
|
---|
13 | * in the Software without restriction, including without limitation the rights
|
---|
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
15 | * copies of the Software, and to permit persons to whom the Software is
|
---|
16 | * furnished to do so, subject to the following conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be included in all
|
---|
19 | * copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
---|
27 | * SOFTWARE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Class Encoding
|
---|
32 | * @package ParagonIE\ConstantTime
|
---|
33 | */
|
---|
34 | abstract class Encoding
|
---|
35 | {
|
---|
36 | /**
|
---|
37 | * RFC 4648 Base32 encoding
|
---|
38 | *
|
---|
39 | * @param string $str
|
---|
40 | * @return string
|
---|
41 | * @throws TypeError
|
---|
42 | */
|
---|
43 | public static function base32Encode(
|
---|
44 | #[\SensitiveParameter]
|
---|
45 | string $str
|
---|
46 | ): string {
|
---|
47 | return Base32::encode($str);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * RFC 4648 Base32 encoding
|
---|
52 | *
|
---|
53 | * @param string $str
|
---|
54 | * @return string
|
---|
55 | * @throws TypeError
|
---|
56 | */
|
---|
57 | public static function base32EncodeUpper(
|
---|
58 | #[\SensitiveParameter]
|
---|
59 | string $str
|
---|
60 | ): string {
|
---|
61 | return Base32::encodeUpper($str);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * RFC 4648 Base32 decoding
|
---|
66 | *
|
---|
67 | * @param string $str
|
---|
68 | * @return string
|
---|
69 | * @throws TypeError
|
---|
70 | */
|
---|
71 | public static function base32Decode(
|
---|
72 | #[\SensitiveParameter]
|
---|
73 | string $str
|
---|
74 | ): string {
|
---|
75 | return Base32::decode($str);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * RFC 4648 Base32 decoding
|
---|
80 | *
|
---|
81 | * @param string $str
|
---|
82 | * @return string
|
---|
83 | * @throws TypeError
|
---|
84 | */
|
---|
85 | public static function base32DecodeUpper(
|
---|
86 | #[\SensitiveParameter]
|
---|
87 | string $str
|
---|
88 | ): string {
|
---|
89 | return Base32::decodeUpper($str);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * RFC 4648 Base32 encoding
|
---|
94 | *
|
---|
95 | * @param string $str
|
---|
96 | * @return string
|
---|
97 | * @throws TypeError
|
---|
98 | */
|
---|
99 | public static function base32HexEncode(
|
---|
100 | #[\SensitiveParameter]
|
---|
101 | string $str
|
---|
102 | ): string {
|
---|
103 | return Base32Hex::encode($str);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * RFC 4648 Base32Hex encoding
|
---|
108 | *
|
---|
109 | * @param string $str
|
---|
110 | * @return string
|
---|
111 | * @throws TypeError
|
---|
112 | */
|
---|
113 | public static function base32HexEncodeUpper(
|
---|
114 | #[\SensitiveParameter]
|
---|
115 | string $str
|
---|
116 | ): string {
|
---|
117 | return Base32Hex::encodeUpper($str);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * RFC 4648 Base32Hex decoding
|
---|
122 | *
|
---|
123 | * @param string $str
|
---|
124 | * @return string
|
---|
125 | * @throws TypeError
|
---|
126 | */
|
---|
127 | public static function base32HexDecode(
|
---|
128 | #[\SensitiveParameter]
|
---|
129 | string $str
|
---|
130 | ): string {
|
---|
131 | return Base32Hex::decode($str);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * RFC 4648 Base32Hex decoding
|
---|
136 | *
|
---|
137 | * @param string $str
|
---|
138 | * @return string
|
---|
139 | * @throws TypeError
|
---|
140 | */
|
---|
141 | public static function base32HexDecodeUpper(
|
---|
142 | #[\SensitiveParameter]
|
---|
143 | string $str
|
---|
144 | ): string {
|
---|
145 | return Base32Hex::decodeUpper($str);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * RFC 4648 Base64 encoding
|
---|
150 | *
|
---|
151 | * @param string $str
|
---|
152 | * @return string
|
---|
153 | * @throws TypeError
|
---|
154 | */
|
---|
155 | public static function base64Encode(
|
---|
156 | #[\SensitiveParameter]
|
---|
157 | string $str
|
---|
158 | ): string {
|
---|
159 | return Base64::encode($str);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * RFC 4648 Base64 decoding
|
---|
164 | *
|
---|
165 | * @param string $str
|
---|
166 | * @return string
|
---|
167 | * @throws TypeError
|
---|
168 | */
|
---|
169 | public static function base64Decode(
|
---|
170 | #[\SensitiveParameter]
|
---|
171 | string $str
|
---|
172 | ): string {
|
---|
173 | return Base64::decode($str);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Encode into Base64
|
---|
178 | *
|
---|
179 | * Base64 character set "./[A-Z][a-z][0-9]"
|
---|
180 | * @param string $str
|
---|
181 | * @return string
|
---|
182 | * @throws TypeError
|
---|
183 | */
|
---|
184 | public static function base64EncodeDotSlash(
|
---|
185 | #[\SensitiveParameter]
|
---|
186 | string $str
|
---|
187 | ): string {
|
---|
188 | return Base64DotSlash::encode($str);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Decode from base64 to raw binary
|
---|
193 | *
|
---|
194 | * Base64 character set "./[A-Z][a-z][0-9]"
|
---|
195 | *
|
---|
196 | * @param string $str
|
---|
197 | * @return string
|
---|
198 | * @throws \RangeException
|
---|
199 | * @throws TypeError
|
---|
200 | */
|
---|
201 | public static function base64DecodeDotSlash(
|
---|
202 | #[\SensitiveParameter]
|
---|
203 | string $str
|
---|
204 | ): string {
|
---|
205 | return Base64DotSlash::decode($str);
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Encode into Base64
|
---|
210 | *
|
---|
211 | * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
|
---|
212 | * @param string $str
|
---|
213 | * @return string
|
---|
214 | * @throws TypeError
|
---|
215 | */
|
---|
216 | public static function base64EncodeDotSlashOrdered(
|
---|
217 | #[\SensitiveParameter]
|
---|
218 | string $str
|
---|
219 | ): string {
|
---|
220 | return Base64DotSlashOrdered::encode($str);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Decode from base64 to raw binary
|
---|
225 | *
|
---|
226 | * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
|
---|
227 | *
|
---|
228 | * @param string $str
|
---|
229 | * @return string
|
---|
230 | * @throws \RangeException
|
---|
231 | * @throws TypeError
|
---|
232 | */
|
---|
233 | public static function base64DecodeDotSlashOrdered(
|
---|
234 | #[\SensitiveParameter]
|
---|
235 | string $str
|
---|
236 | ): string {
|
---|
237 | return Base64DotSlashOrdered::decode($str);
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Convert a binary string into a hexadecimal string without cache-timing
|
---|
242 | * leaks
|
---|
243 | *
|
---|
244 | * @param string $bin_string (raw binary)
|
---|
245 | * @return string
|
---|
246 | * @throws TypeError
|
---|
247 | */
|
---|
248 | public static function hexEncode(
|
---|
249 | #[\SensitiveParameter]
|
---|
250 | string $bin_string
|
---|
251 | ): string {
|
---|
252 | return Hex::encode($bin_string);
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Convert a hexadecimal string into a binary string without cache-timing
|
---|
257 | * leaks
|
---|
258 | *
|
---|
259 | * @param string $hex_string
|
---|
260 | * @return string (raw binary)
|
---|
261 | * @throws \RangeException
|
---|
262 | */
|
---|
263 | public static function hexDecode(
|
---|
264 | #[\SensitiveParameter]
|
---|
265 | string $hex_string
|
---|
266 | ): string {
|
---|
267 | return Hex::decode($hex_string);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Convert a binary string into a hexadecimal string without cache-timing
|
---|
272 | * leaks
|
---|
273 | *
|
---|
274 | * @param string $bin_string (raw binary)
|
---|
275 | * @return string
|
---|
276 | * @throws TypeError
|
---|
277 | */
|
---|
278 | public static function hexEncodeUpper(
|
---|
279 | #[\SensitiveParameter]
|
---|
280 | string $bin_string
|
---|
281 | ): string {
|
---|
282 | return Hex::encodeUpper($bin_string);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Convert a binary string into a hexadecimal string without cache-timing
|
---|
287 | * leaks
|
---|
288 | *
|
---|
289 | * @param string $bin_string (raw binary)
|
---|
290 | * @return string
|
---|
291 | */
|
---|
292 | public static function hexDecodeUpper(
|
---|
293 | #[\SensitiveParameter]
|
---|
294 | string $bin_string
|
---|
295 | ): string {
|
---|
296 | return Hex::decode($bin_string);
|
---|
297 | }
|
---|
298 | }
|
---|