xref: /dokuwiki/vendor/paragonie/constant_time_encoding/src/RFC4648.php (revision 4c1ed9da814741b3dfa44d8b656e85cbd71ec064)
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 RFC4648
32 *
33 * This class conforms strictly to the RFC
34 *
35 * @package ParagonIE\ConstantTime
36 */
37abstract class RFC4648
38{
39    /**
40     * RFC 4648 Base64 encoding
41     *
42     * "foo" -> "Zm9v"
43     *
44     * @param string $str
45     * @return string
46     *
47     * @throws TypeError
48     */
49    public static function base64Encode(string $str): string
50    {
51        return Base64::encode($str);
52    }
53
54    /**
55     * RFC 4648 Base64 decoding
56     *
57     * "Zm9v" -> "foo"
58     *
59     * @param string $str
60     * @return string
61     *
62     * @throws TypeError
63     */
64    public static function base64Decode(string $str): string
65    {
66        return Base64::decode($str, true);
67    }
68
69    /**
70     * RFC 4648 Base64 (URL Safe) encoding
71     *
72     * "foo" -> "Zm9v"
73     *
74     * @param string $str
75     * @return string
76     *
77     * @throws TypeError
78     */
79    public static function base64UrlSafeEncode(string $str): string
80    {
81        return Base64UrlSafe::encode($str);
82    }
83
84    /**
85     * RFC 4648 Base64 (URL Safe) decoding
86     *
87     * "Zm9v" -> "foo"
88     *
89     * @param string $str
90     * @return string
91     *
92     * @throws TypeError
93     */
94    public static function base64UrlSafeDecode(string $str): string
95    {
96        return Base64UrlSafe::decode($str, true);
97    }
98
99    /**
100     * RFC 4648 Base32 encoding
101     *
102     * "foo" -> "MZXW6==="
103     *
104     * @param string $str
105     * @return string
106     *
107     * @throws TypeError
108     */
109    public static function base32Encode(string $str): string
110    {
111        return Base32::encodeUpper($str);
112    }
113
114    /**
115     * RFC 4648 Base32 encoding
116     *
117     * "MZXW6===" -> "foo"
118     *
119     * @param string $str
120     * @return string
121     *
122     * @throws TypeError
123     */
124    public static function base32Decode(string $str): string
125    {
126        return Base32::decodeUpper($str, true);
127    }
128
129    /**
130     * RFC 4648 Base32-Hex encoding
131     *
132     * "foo" -> "CPNMU==="
133     *
134     * @param string $str
135     * @return string
136     *
137     * @throws TypeError
138     */
139    public static function base32HexEncode(string $str): string
140    {
141        return Base32::encodeUpper($str);
142    }
143
144    /**
145     * RFC 4648 Base32-Hex decoding
146     *
147     * "CPNMU===" -> "foo"
148     *
149     * @param string $str
150     * @return string
151     *
152     * @throws TypeError
153     */
154    public static function base32HexDecode(string $str): string
155    {
156        return Base32::decodeUpper($str, true);
157    }
158
159    /**
160     * RFC 4648 Base16 decoding
161     *
162     * "foo" -> "666F6F"
163     *
164     * @param string $str
165     * @return string
166     *
167     * @throws TypeError
168     */
169    public static function base16Encode(string $str): string
170    {
171        return Hex::encodeUpper($str);
172    }
173
174    /**
175     * RFC 4648 Base16 decoding
176     *
177     * "666F6F" -> "foo"
178     *
179     * @param string $str
180     * @return string
181     */
182    public static function base16Decode(string $str): string
183    {
184        return Hex::decode($str, true);
185    }
186}