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(
50        #[\SensitiveParameter]
51        string $str
52    ): string {
53        return Base64::encode($str);
54    }
55
56    /**
57     * RFC 4648 Base64 decoding
58     *
59     * "Zm9v" -> "foo"
60     *
61     * @param string $str
62     * @return string
63     *
64     * @throws TypeError
65     */
66    public static function base64Decode(
67        #[\SensitiveParameter]
68        string $str
69    ): string {
70        return Base64::decode($str, true);
71    }
72
73    /**
74     * RFC 4648 Base64 (URL Safe) encoding
75     *
76     * "foo" -> "Zm9v"
77     *
78     * @param string $str
79     * @return string
80     *
81     * @throws TypeError
82     */
83    public static function base64UrlSafeEncode(
84        #[\SensitiveParameter]
85        string $str
86    ): string {
87        return Base64UrlSafe::encode($str);
88    }
89
90    /**
91     * RFC 4648 Base64 (URL Safe) decoding
92     *
93     * "Zm9v" -> "foo"
94     *
95     * @param string $str
96     * @return string
97     *
98     * @throws TypeError
99     */
100    public static function base64UrlSafeDecode(
101        #[\SensitiveParameter]
102        string $str
103    ): string {
104        return Base64UrlSafe::decode($str, true);
105    }
106
107    /**
108     * RFC 4648 Base32 encoding
109     *
110     * "foo" -> "MZXW6==="
111     *
112     * @param string $str
113     * @return string
114     *
115     * @throws TypeError
116     */
117    public static function base32Encode(
118        #[\SensitiveParameter]
119        string $str
120    ): string {
121        return Base32::encodeUpper($str);
122    }
123
124    /**
125     * RFC 4648 Base32 encoding
126     *
127     * "MZXW6===" -> "foo"
128     *
129     * @param string $str
130     * @return string
131     *
132     * @throws TypeError
133     */
134    public static function base32Decode(
135        #[\SensitiveParameter]
136        string $str
137    ): string {
138        return Base32::decodeUpper($str, true);
139    }
140
141    /**
142     * RFC 4648 Base32-Hex encoding
143     *
144     * "foo" -> "CPNMU==="
145     *
146     * @param string $str
147     * @return string
148     *
149     * @throws TypeError
150     */
151    public static function base32HexEncode(
152        #[\SensitiveParameter]
153        string $str
154    ): string {
155        return Base32::encodeUpper($str);
156    }
157
158    /**
159     * RFC 4648 Base32-Hex decoding
160     *
161     * "CPNMU===" -> "foo"
162     *
163     * @param string $str
164     * @return string
165     *
166     * @throws TypeError
167     */
168    public static function base32HexDecode(
169        #[\SensitiveParameter]
170        string $str
171    ): string {
172        return Base32::decodeUpper($str, true);
173    }
174
175    /**
176     * RFC 4648 Base16 decoding
177     *
178     * "foo" -> "666F6F"
179     *
180     * @param string $str
181     * @return string
182     *
183     * @throws TypeError
184     */
185    public static function base16Encode(
186        #[\SensitiveParameter]
187        string $str
188    ): string {
189        return Hex::encodeUpper($str);
190    }
191
192    /**
193     * RFC 4648 Base16 decoding
194     *
195     * "666F6F" -> "foo"
196     *
197     * @param string $str
198     * @return string
199     */
200    public static function base16Decode(
201        #[\SensitiveParameter]
202        string $str
203    ): string {
204        return Hex::decode($str, true);
205    }
206}
207