1<?php 2declare(strict_types=1); 3namespace ParagonIE\ConstantTime; 4 5use RangeException; 6use SensitiveParameter; 7use TypeError; 8 9/** 10 * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. 11 * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining a copy 14 * of this software and associated documentation files (the "Software"), to deal 15 * in the Software without restriction, including without limitation the rights 16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 * copies of the Software, and to permit persons to whom the Software is 18 * furnished to do so, subject to the following conditions: 19 * 20 * The above copyright notice and this permission notice shall be included in all 21 * copies or substantial portions of the Software. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 29 * SOFTWARE. 30 */ 31 32/** 33 * Class Encoding 34 * @package ParagonIE\ConstantTime 35 * @api 36 */ 37abstract class Encoding 38{ 39 /** 40 * RFC 4648 Base32 encoding 41 * 42 * @param string $str 43 * @return string 44 * @throws TypeError 45 */ 46 public static function base32Encode( 47 #[SensitiveParameter] 48 string $str 49 ): string { 50 return Base32::encode($str); 51 } 52 53 /** 54 * RFC 4648 Base32 encoding 55 * 56 * @param string $str 57 * @return string 58 * @throws TypeError 59 */ 60 public static function base32EncodeUpper( 61 #[SensitiveParameter] 62 string $str 63 ): string { 64 return Base32::encodeUpper($str); 65 } 66 67 /** 68 * RFC 4648 Base32 decoding 69 * 70 * @param string $str 71 * @return string 72 * @throws TypeError 73 */ 74 public static function base32Decode( 75 #[SensitiveParameter] 76 string $str 77 ): string { 78 return Base32::decode($str); 79 } 80 81 /** 82 * RFC 4648 Base32 decoding 83 * 84 * @param string $str 85 * @return string 86 * @throws TypeError 87 */ 88 public static function base32DecodeUpper( 89 #[SensitiveParameter] 90 string $str 91 ): string { 92 return Base32::decodeUpper($str); 93 } 94 95 /** 96 * RFC 4648 Base32 encoding 97 * 98 * @param string $str 99 * @return string 100 * @throws TypeError 101 */ 102 public static function base32HexEncode( 103 #[SensitiveParameter] 104 string $str 105 ): string { 106 return Base32Hex::encode($str); 107 } 108 109 /** 110 * RFC 4648 Base32Hex encoding 111 * 112 * @param string $str 113 * @return string 114 * @throws TypeError 115 */ 116 public static function base32HexEncodeUpper( 117 #[SensitiveParameter] 118 string $str 119 ): string { 120 return Base32Hex::encodeUpper($str); 121 } 122 123 /** 124 * RFC 4648 Base32Hex decoding 125 * 126 * @param string $str 127 * @return string 128 * @throws TypeError 129 */ 130 public static function base32HexDecode( 131 #[SensitiveParameter] 132 string $str 133 ): string { 134 return Base32Hex::decode($str); 135 } 136 137 /** 138 * RFC 4648 Base32Hex decoding 139 * 140 * @param string $str 141 * @return string 142 * @throws TypeError 143 */ 144 public static function base32HexDecodeUpper( 145 #[SensitiveParameter] 146 string $str 147 ): string { 148 return Base32Hex::decodeUpper($str); 149 } 150 151 /** 152 * RFC 4648 Base64 encoding 153 * 154 * @param string $str 155 * @return string 156 * @throws TypeError 157 */ 158 public static function base64Encode( 159 #[SensitiveParameter] 160 string $str 161 ): string { 162 return Base64::encode($str); 163 } 164 165 /** 166 * RFC 4648 Base64 decoding 167 * 168 * @param string $str 169 * @return string 170 * @throws TypeError 171 */ 172 public static function base64Decode( 173 #[SensitiveParameter] 174 string $str 175 ): string { 176 return Base64::decode($str); 177 } 178 179 /** 180 * Encode into Base64 181 * 182 * Base64 character set "./[A-Z][a-z][0-9]" 183 * @param string $str 184 * @return string 185 * @throws TypeError 186 */ 187 public static function base64EncodeDotSlash( 188 #[SensitiveParameter] 189 string $str 190 ): string { 191 return Base64DotSlash::encode($str); 192 } 193 194 /** 195 * Decode from base64 to raw binary 196 * 197 * Base64 character set "./[A-Z][a-z][0-9]" 198 * 199 * @param string $str 200 * @return string 201 * @throws RangeException 202 * @throws TypeError 203 */ 204 public static function base64DecodeDotSlash( 205 #[SensitiveParameter] 206 string $str 207 ): string { 208 return Base64DotSlash::decode($str); 209 } 210 211 /** 212 * Encode into Base64 213 * 214 * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]" 215 * @param string $str 216 * @return string 217 * @throws TypeError 218 */ 219 public static function base64EncodeDotSlashOrdered( 220 #[SensitiveParameter] 221 string $str 222 ): string { 223 return Base64DotSlashOrdered::encode($str); 224 } 225 226 /** 227 * Decode from base64 to raw binary 228 * 229 * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]" 230 * 231 * @param string $str 232 * @return string 233 * @throws RangeException 234 * @throws TypeError 235 */ 236 public static function base64DecodeDotSlashOrdered( 237 #[SensitiveParameter] 238 string $str 239 ): string { 240 return Base64DotSlashOrdered::decode($str); 241 } 242 243 /** 244 * Convert a binary string into a hexadecimal string without cache-timing 245 * leaks 246 * 247 * @param string $bin_string (raw binary) 248 * @return string 249 * @throws TypeError 250 */ 251 public static function hexEncode( 252 #[SensitiveParameter] 253 string $bin_string 254 ): string { 255 return Hex::encode($bin_string); 256 } 257 258 /** 259 * Convert a hexadecimal string into a binary string without cache-timing 260 * leaks 261 * 262 * @param string $hex_string 263 * @return string (raw binary) 264 * @throws RangeException 265 */ 266 public static function hexDecode( 267 #[SensitiveParameter] 268 string $hex_string 269 ): string { 270 return Hex::decode($hex_string); 271 } 272 273 /** 274 * Convert a binary string into a hexadecimal string without cache-timing 275 * leaks 276 * 277 * @param string $bin_string (raw binary) 278 * @return string 279 * @throws TypeError 280 */ 281 public static function hexEncodeUpper( 282 #[SensitiveParameter] 283 string $bin_string 284 ): string { 285 return Hex::encodeUpper($bin_string); 286 } 287 288 /** 289 * Convert a binary string into a hexadecimal string without cache-timing 290 * leaks 291 * 292 * @param string $bin_string (raw binary) 293 * @return string 294 */ 295 public static function hexDecodeUpper( 296 #[SensitiveParameter] 297 string $bin_string 298 ): string { 299 return Hex::decode($bin_string); 300 } 301} 302