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