xref: /dokuwiki/vendor/paragonie/constant_time_encoding/src/Encoding.php (revision a0d03045791d1a7a2ae869fea7b7f932f20405da)
1<?php
2declare(strict_types=1);
3namespace ParagonIE\ConstantTime;
4
5use 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 */
34abstract 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(string $str): string
44    {
45        return Base32::encode($str);
46    }
47
48    /**
49     * RFC 4648 Base32 encoding
50     *
51     * @param string $str
52     * @return string
53     * @throws TypeError
54     */
55    public static function base32EncodeUpper(string $str): string
56    {
57        return Base32::encodeUpper($str);
58    }
59
60    /**
61     * RFC 4648 Base32 decoding
62     *
63     * @param string $str
64     * @return string
65     * @throws TypeError
66     */
67    public static function base32Decode(string $str): string
68    {
69        return Base32::decode($str);
70    }
71
72    /**
73     * RFC 4648 Base32 decoding
74     *
75     * @param string $str
76     * @return string
77     * @throws TypeError
78     */
79    public static function base32DecodeUpper(string $str): string
80    {
81        return Base32::decodeUpper($str);
82    }
83
84    /**
85     * RFC 4648 Base32 encoding
86     *
87     * @param string $str
88     * @return string
89     * @throws TypeError
90     */
91    public static function base32HexEncode(string $str): string
92    {
93        return Base32Hex::encode($str);
94    }
95
96    /**
97     * RFC 4648 Base32Hex encoding
98     *
99     * @param string $str
100     * @return string
101     * @throws TypeError
102     */
103    public static function base32HexEncodeUpper(string $str): string
104    {
105        return Base32Hex::encodeUpper($str);
106    }
107
108    /**
109     * RFC 4648 Base32Hex decoding
110     *
111     * @param string $str
112     * @return string
113     * @throws TypeError
114     */
115    public static function base32HexDecode(string $str): string
116    {
117        return Base32Hex::decode($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 base32HexDecodeUpper(string $str): string
128    {
129        return Base32Hex::decodeUpper($str);
130    }
131
132    /**
133     * RFC 4648 Base64 encoding
134     *
135     * @param string $str
136     * @return string
137     * @throws TypeError
138     */
139    public static function base64Encode(string $str): string
140    {
141        return Base64::encode($str);
142    }
143
144    /**
145     * RFC 4648 Base64 decoding
146     *
147     * @param string $str
148     * @return string
149     * @throws TypeError
150     */
151    public static function base64Decode(string $str): string
152    {
153        return Base64::decode($str);
154    }
155
156    /**
157     * Encode into Base64
158     *
159     * Base64 character set "./[A-Z][a-z][0-9]"
160     * @param string $str
161     * @return string
162     * @throws TypeError
163     */
164    public static function base64EncodeDotSlash(string $str): string
165    {
166        return Base64DotSlash::encode($str);
167    }
168
169    /**
170     * Decode from base64 to raw binary
171     *
172     * Base64 character set "./[A-Z][a-z][0-9]"
173     *
174     * @param string $str
175     * @return string
176     * @throws \RangeException
177     * @throws TypeError
178     */
179    public static function base64DecodeDotSlash(string $str): string
180    {
181        return Base64DotSlash::decode($str);
182    }
183
184    /**
185     * Encode into Base64
186     *
187     * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
188     * @param string $str
189     * @return string
190     * @throws TypeError
191     */
192    public static function base64EncodeDotSlashOrdered(string $str): string
193    {
194        return Base64DotSlashOrdered::encode($str);
195    }
196
197    /**
198     * Decode from base64 to raw binary
199     *
200     * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
201     *
202     * @param string $str
203     * @return string
204     * @throws \RangeException
205     * @throws TypeError
206     */
207    public static function base64DecodeDotSlashOrdered(string $str): string
208    {
209        return Base64DotSlashOrdered::decode($str);
210    }
211
212    /**
213     * Convert a binary string into a hexadecimal string without cache-timing
214     * leaks
215     *
216     * @param string $bin_string (raw binary)
217     * @return string
218     * @throws TypeError
219     */
220    public static function hexEncode(string $bin_string): string
221    {
222        return Hex::encode($bin_string);
223    }
224
225    /**
226     * Convert a hexadecimal string into a binary string without cache-timing
227     * leaks
228     *
229     * @param string $hex_string
230     * @return string (raw binary)
231     * @throws \RangeException
232     */
233    public static function hexDecode(string $hex_string): string
234    {
235        return Hex::decode($hex_string);
236    }
237
238    /**
239     * Convert a binary string into a hexadecimal string without cache-timing
240     * leaks
241     *
242     * @param string $bin_string (raw binary)
243     * @return string
244     * @throws TypeError
245     */
246    public static function hexEncodeUpper(string $bin_string): string
247    {
248        return Hex::encodeUpper($bin_string);
249    }
250
251    /**
252     * Convert a binary string into a hexadecimal string without cache-timing
253     * leaks
254     *
255     * @param string $bin_string (raw binary)
256     * @return string
257     */
258    public static function hexDecodeUpper(string $bin_string): string
259    {
260        return Hex::decode($bin_string);
261    }
262}
263