xref: /dokuwiki/vendor/paragonie/constant_time_encoding/src/Encoding.php (revision 850e662095111529d7d330745fee3207907c4aee)
1927933f5SAndreas Gohr<?php
2927933f5SAndreas Gohrdeclare(strict_types=1);
3927933f5SAndreas Gohrnamespace ParagonIE\ConstantTime;
4927933f5SAndreas Gohr
5927933f5SAndreas Gohruse TypeError;
6927933f5SAndreas Gohr
7927933f5SAndreas Gohr/**
8927933f5SAndreas Gohr *  Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
9927933f5SAndreas Gohr *  Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
10927933f5SAndreas Gohr *
11927933f5SAndreas Gohr *  Permission is hereby granted, free of charge, to any person obtaining a copy
12927933f5SAndreas Gohr *  of this software and associated documentation files (the "Software"), to deal
13927933f5SAndreas Gohr *  in the Software without restriction, including without limitation the rights
14927933f5SAndreas Gohr *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15927933f5SAndreas Gohr *  copies of the Software, and to permit persons to whom the Software is
16927933f5SAndreas Gohr *  furnished to do so, subject to the following conditions:
17927933f5SAndreas Gohr *
18927933f5SAndreas Gohr *  The above copyright notice and this permission notice shall be included in all
19927933f5SAndreas Gohr *  copies or substantial portions of the Software.
20927933f5SAndreas Gohr *
21927933f5SAndreas Gohr *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22927933f5SAndreas Gohr *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23927933f5SAndreas Gohr *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24927933f5SAndreas Gohr *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25927933f5SAndreas Gohr *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26927933f5SAndreas Gohr *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27927933f5SAndreas Gohr *  SOFTWARE.
28927933f5SAndreas Gohr */
29927933f5SAndreas Gohr
30927933f5SAndreas Gohr/**
31927933f5SAndreas Gohr * Class Encoding
32927933f5SAndreas Gohr * @package ParagonIE\ConstantTime
33927933f5SAndreas Gohr */
34927933f5SAndreas Gohrabstract class Encoding
35927933f5SAndreas Gohr{
36927933f5SAndreas Gohr    /**
37927933f5SAndreas Gohr     * RFC 4648 Base32 encoding
38927933f5SAndreas Gohr     *
39927933f5SAndreas Gohr     * @param string $str
40927933f5SAndreas Gohr     * @return string
41927933f5SAndreas Gohr     * @throws TypeError
42927933f5SAndreas Gohr     */
43*850e6620SAndreas Gohr    public static function base32Encode(
44*850e6620SAndreas Gohr        #[\SensitiveParameter]
45*850e6620SAndreas Gohr        string $str
46*850e6620SAndreas Gohr    ): string {
47927933f5SAndreas Gohr        return Base32::encode($str);
48927933f5SAndreas Gohr    }
49927933f5SAndreas Gohr
50927933f5SAndreas Gohr    /**
51927933f5SAndreas Gohr     * RFC 4648 Base32 encoding
52927933f5SAndreas Gohr     *
53927933f5SAndreas Gohr     * @param string $str
54927933f5SAndreas Gohr     * @return string
55927933f5SAndreas Gohr     * @throws TypeError
56927933f5SAndreas Gohr     */
57*850e6620SAndreas Gohr    public static function base32EncodeUpper(
58*850e6620SAndreas Gohr        #[\SensitiveParameter]
59*850e6620SAndreas Gohr        string $str
60*850e6620SAndreas Gohr    ): string {
61927933f5SAndreas Gohr        return Base32::encodeUpper($str);
62927933f5SAndreas Gohr    }
63927933f5SAndreas Gohr
64927933f5SAndreas Gohr    /**
65927933f5SAndreas Gohr     * RFC 4648 Base32 decoding
66927933f5SAndreas Gohr     *
67927933f5SAndreas Gohr     * @param string $str
68927933f5SAndreas Gohr     * @return string
69927933f5SAndreas Gohr     * @throws TypeError
70927933f5SAndreas Gohr     */
71*850e6620SAndreas Gohr    public static function base32Decode(
72*850e6620SAndreas Gohr        #[\SensitiveParameter]
73*850e6620SAndreas Gohr        string $str
74*850e6620SAndreas Gohr    ): string {
75927933f5SAndreas Gohr        return Base32::decode($str);
76927933f5SAndreas Gohr    }
77927933f5SAndreas Gohr
78927933f5SAndreas Gohr    /**
79927933f5SAndreas Gohr     * RFC 4648 Base32 decoding
80927933f5SAndreas Gohr     *
81927933f5SAndreas Gohr     * @param string $str
82927933f5SAndreas Gohr     * @return string
83927933f5SAndreas Gohr     * @throws TypeError
84927933f5SAndreas Gohr     */
85*850e6620SAndreas Gohr    public static function base32DecodeUpper(
86*850e6620SAndreas Gohr        #[\SensitiveParameter]
87*850e6620SAndreas Gohr        string $str
88*850e6620SAndreas Gohr    ): string {
89927933f5SAndreas Gohr        return Base32::decodeUpper($str);
90927933f5SAndreas Gohr    }
91927933f5SAndreas Gohr
92927933f5SAndreas Gohr    /**
93927933f5SAndreas Gohr     * RFC 4648 Base32 encoding
94927933f5SAndreas Gohr     *
95927933f5SAndreas Gohr     * @param string $str
96927933f5SAndreas Gohr     * @return string
97927933f5SAndreas Gohr     * @throws TypeError
98927933f5SAndreas Gohr     */
99*850e6620SAndreas Gohr    public static function base32HexEncode(
100*850e6620SAndreas Gohr        #[\SensitiveParameter]
101*850e6620SAndreas Gohr        string $str
102*850e6620SAndreas Gohr    ): string {
103927933f5SAndreas Gohr        return Base32Hex::encode($str);
104927933f5SAndreas Gohr    }
105927933f5SAndreas Gohr
106927933f5SAndreas Gohr    /**
107927933f5SAndreas Gohr     * RFC 4648 Base32Hex encoding
108927933f5SAndreas Gohr     *
109927933f5SAndreas Gohr     * @param string $str
110927933f5SAndreas Gohr     * @return string
111927933f5SAndreas Gohr     * @throws TypeError
112927933f5SAndreas Gohr     */
113*850e6620SAndreas Gohr    public static function base32HexEncodeUpper(
114*850e6620SAndreas Gohr        #[\SensitiveParameter]
115*850e6620SAndreas Gohr        string $str
116*850e6620SAndreas Gohr    ): string {
117927933f5SAndreas Gohr        return Base32Hex::encodeUpper($str);
118927933f5SAndreas Gohr    }
119927933f5SAndreas Gohr
120927933f5SAndreas Gohr    /**
121927933f5SAndreas Gohr     * RFC 4648 Base32Hex decoding
122927933f5SAndreas Gohr     *
123927933f5SAndreas Gohr     * @param string $str
124927933f5SAndreas Gohr     * @return string
125927933f5SAndreas Gohr     * @throws TypeError
126927933f5SAndreas Gohr     */
127*850e6620SAndreas Gohr    public static function base32HexDecode(
128*850e6620SAndreas Gohr        #[\SensitiveParameter]
129*850e6620SAndreas Gohr        string $str
130*850e6620SAndreas Gohr    ): string {
131927933f5SAndreas Gohr        return Base32Hex::decode($str);
132927933f5SAndreas Gohr    }
133927933f5SAndreas Gohr
134927933f5SAndreas Gohr    /**
135927933f5SAndreas Gohr     * RFC 4648 Base32Hex decoding
136927933f5SAndreas Gohr     *
137927933f5SAndreas Gohr     * @param string $str
138927933f5SAndreas Gohr     * @return string
139927933f5SAndreas Gohr     * @throws TypeError
140927933f5SAndreas Gohr     */
141*850e6620SAndreas Gohr    public static function base32HexDecodeUpper(
142*850e6620SAndreas Gohr        #[\SensitiveParameter]
143*850e6620SAndreas Gohr        string $str
144*850e6620SAndreas Gohr    ): string {
145927933f5SAndreas Gohr        return Base32Hex::decodeUpper($str);
146927933f5SAndreas Gohr    }
147927933f5SAndreas Gohr
148927933f5SAndreas Gohr    /**
149927933f5SAndreas Gohr     * RFC 4648 Base64 encoding
150927933f5SAndreas Gohr     *
151927933f5SAndreas Gohr     * @param string $str
152927933f5SAndreas Gohr     * @return string
153927933f5SAndreas Gohr     * @throws TypeError
154927933f5SAndreas Gohr     */
155*850e6620SAndreas Gohr    public static function base64Encode(
156*850e6620SAndreas Gohr        #[\SensitiveParameter]
157*850e6620SAndreas Gohr        string $str
158*850e6620SAndreas Gohr    ): string {
159927933f5SAndreas Gohr        return Base64::encode($str);
160927933f5SAndreas Gohr    }
161927933f5SAndreas Gohr
162927933f5SAndreas Gohr    /**
163927933f5SAndreas Gohr     * RFC 4648 Base64 decoding
164927933f5SAndreas Gohr     *
165927933f5SAndreas Gohr     * @param string $str
166927933f5SAndreas Gohr     * @return string
167927933f5SAndreas Gohr     * @throws TypeError
168927933f5SAndreas Gohr     */
169*850e6620SAndreas Gohr    public static function base64Decode(
170*850e6620SAndreas Gohr        #[\SensitiveParameter]
171*850e6620SAndreas Gohr        string $str
172*850e6620SAndreas Gohr    ): string {
173927933f5SAndreas Gohr        return Base64::decode($str);
174927933f5SAndreas Gohr    }
175927933f5SAndreas Gohr
176927933f5SAndreas Gohr    /**
177927933f5SAndreas Gohr     * Encode into Base64
178927933f5SAndreas Gohr     *
179927933f5SAndreas Gohr     * Base64 character set "./[A-Z][a-z][0-9]"
180927933f5SAndreas Gohr     * @param string $str
181927933f5SAndreas Gohr     * @return string
182927933f5SAndreas Gohr     * @throws TypeError
183927933f5SAndreas Gohr     */
184*850e6620SAndreas Gohr    public static function base64EncodeDotSlash(
185*850e6620SAndreas Gohr        #[\SensitiveParameter]
186*850e6620SAndreas Gohr        string $str
187*850e6620SAndreas Gohr    ): string {
188927933f5SAndreas Gohr        return Base64DotSlash::encode($str);
189927933f5SAndreas Gohr    }
190927933f5SAndreas Gohr
191927933f5SAndreas Gohr    /**
192927933f5SAndreas Gohr     * Decode from base64 to raw binary
193927933f5SAndreas Gohr     *
194927933f5SAndreas Gohr     * Base64 character set "./[A-Z][a-z][0-9]"
195927933f5SAndreas Gohr     *
196927933f5SAndreas Gohr     * @param string $str
197927933f5SAndreas Gohr     * @return string
198927933f5SAndreas Gohr     * @throws \RangeException
199927933f5SAndreas Gohr     * @throws TypeError
200927933f5SAndreas Gohr     */
201*850e6620SAndreas Gohr    public static function base64DecodeDotSlash(
202*850e6620SAndreas Gohr        #[\SensitiveParameter]
203*850e6620SAndreas Gohr        string $str
204*850e6620SAndreas Gohr    ): string {
205927933f5SAndreas Gohr        return Base64DotSlash::decode($str);
206927933f5SAndreas Gohr    }
207927933f5SAndreas Gohr
208927933f5SAndreas Gohr    /**
209927933f5SAndreas Gohr     * Encode into Base64
210927933f5SAndreas Gohr     *
211927933f5SAndreas Gohr     * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
212927933f5SAndreas Gohr     * @param string $str
213927933f5SAndreas Gohr     * @return string
214927933f5SAndreas Gohr     * @throws TypeError
215927933f5SAndreas Gohr     */
216*850e6620SAndreas Gohr    public static function base64EncodeDotSlashOrdered(
217*850e6620SAndreas Gohr        #[\SensitiveParameter]
218*850e6620SAndreas Gohr        string $str
219*850e6620SAndreas Gohr    ): string {
220927933f5SAndreas Gohr        return Base64DotSlashOrdered::encode($str);
221927933f5SAndreas Gohr    }
222927933f5SAndreas Gohr
223927933f5SAndreas Gohr    /**
224927933f5SAndreas Gohr     * Decode from base64 to raw binary
225927933f5SAndreas Gohr     *
226927933f5SAndreas Gohr     * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
227927933f5SAndreas Gohr     *
228927933f5SAndreas Gohr     * @param string $str
229927933f5SAndreas Gohr     * @return string
230927933f5SAndreas Gohr     * @throws \RangeException
231927933f5SAndreas Gohr     * @throws TypeError
232927933f5SAndreas Gohr     */
233*850e6620SAndreas Gohr    public static function base64DecodeDotSlashOrdered(
234*850e6620SAndreas Gohr        #[\SensitiveParameter]
235*850e6620SAndreas Gohr        string $str
236*850e6620SAndreas Gohr    ): string {
237927933f5SAndreas Gohr        return Base64DotSlashOrdered::decode($str);
238927933f5SAndreas Gohr    }
239927933f5SAndreas Gohr
240927933f5SAndreas Gohr    /**
241927933f5SAndreas Gohr     * Convert a binary string into a hexadecimal string without cache-timing
242927933f5SAndreas Gohr     * leaks
243927933f5SAndreas Gohr     *
244927933f5SAndreas Gohr     * @param string $bin_string (raw binary)
245927933f5SAndreas Gohr     * @return string
246927933f5SAndreas Gohr     * @throws TypeError
247927933f5SAndreas Gohr     */
248*850e6620SAndreas Gohr    public static function hexEncode(
249*850e6620SAndreas Gohr        #[\SensitiveParameter]
250*850e6620SAndreas Gohr        string $bin_string
251*850e6620SAndreas Gohr    ): string {
252927933f5SAndreas Gohr        return Hex::encode($bin_string);
253927933f5SAndreas Gohr    }
254927933f5SAndreas Gohr
255927933f5SAndreas Gohr    /**
256927933f5SAndreas Gohr     * Convert a hexadecimal string into a binary string without cache-timing
257927933f5SAndreas Gohr     * leaks
258927933f5SAndreas Gohr     *
259927933f5SAndreas Gohr     * @param string $hex_string
260927933f5SAndreas Gohr     * @return string (raw binary)
261927933f5SAndreas Gohr     * @throws \RangeException
262927933f5SAndreas Gohr     */
263*850e6620SAndreas Gohr    public static function hexDecode(
264*850e6620SAndreas Gohr        #[\SensitiveParameter]
265*850e6620SAndreas Gohr        string $hex_string
266*850e6620SAndreas Gohr    ): string {
267927933f5SAndreas Gohr        return Hex::decode($hex_string);
268927933f5SAndreas Gohr    }
269927933f5SAndreas Gohr
270927933f5SAndreas Gohr    /**
271927933f5SAndreas Gohr     * Convert a binary string into a hexadecimal string without cache-timing
272927933f5SAndreas Gohr     * leaks
273927933f5SAndreas Gohr     *
274927933f5SAndreas Gohr     * @param string $bin_string (raw binary)
275927933f5SAndreas Gohr     * @return string
276927933f5SAndreas Gohr     * @throws TypeError
277927933f5SAndreas Gohr     */
278*850e6620SAndreas Gohr    public static function hexEncodeUpper(
279*850e6620SAndreas Gohr        #[\SensitiveParameter]
280*850e6620SAndreas Gohr        string $bin_string
281*850e6620SAndreas Gohr    ): string {
282927933f5SAndreas Gohr        return Hex::encodeUpper($bin_string);
283927933f5SAndreas Gohr    }
284927933f5SAndreas Gohr
285927933f5SAndreas Gohr    /**
286927933f5SAndreas Gohr     * Convert a binary string into a hexadecimal string without cache-timing
287927933f5SAndreas Gohr     * leaks
288927933f5SAndreas Gohr     *
289927933f5SAndreas Gohr     * @param string $bin_string (raw binary)
290927933f5SAndreas Gohr     * @return string
291927933f5SAndreas Gohr     */
292*850e6620SAndreas Gohr    public static function hexDecodeUpper(
293*850e6620SAndreas Gohr        #[\SensitiveParameter]
294*850e6620SAndreas Gohr        string $bin_string
295*850e6620SAndreas Gohr    ): string {
296927933f5SAndreas Gohr        return Hex::decode($bin_string);
297927933f5SAndreas Gohr    }
298927933f5SAndreas Gohr}
299