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