xref: /dokuwiki/vendor/paragonie/constant_time_encoding/src/RFC4648.php (revision 8e88a29b81301f78509349ab1152bb09c229123e)
1<?php
2declare(strict_types=1);
3namespace ParagonIE\ConstantTime;
4
5use SensitiveParameter;
6use TypeError;
7
8/**
9 *  Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
10 *  Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
11 *
12 *  Permission is hereby granted, free of charge, to any person obtaining a copy
13 *  of this software and associated documentation files (the "Software"), to deal
14 *  in the Software without restriction, including without limitation the rights
15 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 *  copies of the Software, and to permit persons to whom the Software is
17 *  furnished to do so, subject to the following conditions:
18 *
19 *  The above copyright notice and this permission notice shall be included in all
20 *  copies or substantial portions of the Software.
21 *
22 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 *  SOFTWARE.
29 */
30
31/**
32 * Class RFC4648
33 *
34 * This class conforms strictly to the RFC
35 *
36 * @package ParagonIE\ConstantTime
37 * @api
38 */
39abstract class RFC4648
40{
41    /**
42     * RFC 4648 Base64 encoding
43     *
44     * "foo" -> "Zm9v"
45     *
46     * @param string $str
47     * @return string
48     *
49     * @throws TypeError
50     */
51    public static function base64Encode(
52        #[SensitiveParameter]
53        string $str
54    ): string {
55        return Base64::encode($str);
56    }
57
58    /**
59     * RFC 4648 Base64 decoding
60     *
61     * "Zm9v" -> "foo"
62     *
63     * @param string $str
64     * @return string
65     *
66     * @throws TypeError
67     */
68    public static function base64Decode(
69        #[SensitiveParameter]
70        string $str
71    ): string {
72        return Base64::decode($str, true);
73    }
74
75    /**
76     * RFC 4648 Base64 (URL Safe) encoding
77     *
78     * "foo" -> "Zm9v"
79     *
80     * @param string $str
81     * @return string
82     *
83     * @throws TypeError
84     */
85    public static function base64UrlSafeEncode(
86        #[SensitiveParameter]
87        string $str
88    ): string {
89        return Base64UrlSafe::encode($str);
90    }
91
92    /**
93     * RFC 4648 Base64 (URL Safe) decoding
94     *
95     * "Zm9v" -> "foo"
96     *
97     * @param string $str
98     * @return string
99     *
100     * @throws TypeError
101     */
102    public static function base64UrlSafeDecode(
103        #[SensitiveParameter]
104        string $str
105    ): string {
106        return Base64UrlSafe::decode($str, true);
107    }
108
109    /**
110     * RFC 4648 Base32 encoding
111     *
112     * "foo" -> "MZXW6==="
113     *
114     * @param string $str
115     * @return string
116     *
117     * @throws TypeError
118     */
119    public static function base32Encode(
120        #[SensitiveParameter]
121        string $str
122    ): string {
123        return Base32::encodeUpper($str);
124    }
125
126    /**
127     * RFC 4648 Base32 encoding
128     *
129     * "MZXW6===" -> "foo"
130     *
131     * @param string $str
132     * @return string
133     *
134     * @throws TypeError
135     */
136    public static function base32Decode(
137        #[SensitiveParameter]
138        string $str
139    ): string {
140        return Base32::decodeUpper($str, true);
141    }
142
143    /**
144     * RFC 4648 Base32-Hex encoding
145     *
146     * "foo" -> "CPNMU==="
147     *
148     * @param string $str
149     * @return string
150     *
151     * @throws TypeError
152     */
153    public static function base32HexEncode(
154        #[SensitiveParameter]
155        string $str
156    ): string {
157        return Base32::encodeUpper($str);
158    }
159
160    /**
161     * RFC 4648 Base32-Hex decoding
162     *
163     * "CPNMU===" -> "foo"
164     *
165     * @param string $str
166     * @return string
167     *
168     * @throws TypeError
169     */
170    public static function base32HexDecode(
171        #[SensitiveParameter]
172        string $str
173    ): string {
174        return Base32::decodeUpper($str, true);
175    }
176
177    /**
178     * RFC 4648 Base16 decoding
179     *
180     * "foo" -> "666F6F"
181     *
182     * @param string $str
183     * @return string
184     *
185     * @throws TypeError
186     */
187    public static function base16Encode(
188        #[SensitiveParameter]
189        string $str
190    ): string {
191        return Hex::encodeUpper($str);
192    }
193
194    /**
195     * RFC 4648 Base16 decoding
196     *
197     * "666F6F" -> "foo"
198     *
199     * @param string $str
200     * @return string
201     */
202    public static function base16Decode(
203        #[SensitiveParameter]
204        string $str
205    ): string {
206        return Hex::decode($str, true);
207    }
208}
209