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