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 Encoding 32*927933f5SAndreas Gohr * @package ParagonIE\ConstantTime 33*927933f5SAndreas Gohr */ 34*927933f5SAndreas Gohrabstract class Encoding 35*927933f5SAndreas Gohr{ 36*927933f5SAndreas Gohr /** 37*927933f5SAndreas Gohr * RFC 4648 Base32 encoding 38*927933f5SAndreas Gohr * 39*927933f5SAndreas Gohr * @param string $str 40*927933f5SAndreas Gohr * @return string 41*927933f5SAndreas Gohr * @throws TypeError 42*927933f5SAndreas Gohr */ 43*927933f5SAndreas Gohr public static function base32Encode(string $str): string 44*927933f5SAndreas Gohr { 45*927933f5SAndreas Gohr return Base32::encode($str); 46*927933f5SAndreas Gohr } 47*927933f5SAndreas Gohr 48*927933f5SAndreas Gohr /** 49*927933f5SAndreas Gohr * RFC 4648 Base32 encoding 50*927933f5SAndreas Gohr * 51*927933f5SAndreas Gohr * @param string $str 52*927933f5SAndreas Gohr * @return string 53*927933f5SAndreas Gohr * @throws TypeError 54*927933f5SAndreas Gohr */ 55*927933f5SAndreas Gohr public static function base32EncodeUpper(string $str): string 56*927933f5SAndreas Gohr { 57*927933f5SAndreas Gohr return Base32::encodeUpper($str); 58*927933f5SAndreas Gohr } 59*927933f5SAndreas Gohr 60*927933f5SAndreas Gohr /** 61*927933f5SAndreas Gohr * RFC 4648 Base32 decoding 62*927933f5SAndreas Gohr * 63*927933f5SAndreas Gohr * @param string $str 64*927933f5SAndreas Gohr * @return string 65*927933f5SAndreas Gohr * @throws TypeError 66*927933f5SAndreas Gohr */ 67*927933f5SAndreas Gohr public static function base32Decode(string $str): string 68*927933f5SAndreas Gohr { 69*927933f5SAndreas Gohr return Base32::decode($str); 70*927933f5SAndreas Gohr } 71*927933f5SAndreas Gohr 72*927933f5SAndreas Gohr /** 73*927933f5SAndreas Gohr * RFC 4648 Base32 decoding 74*927933f5SAndreas Gohr * 75*927933f5SAndreas Gohr * @param string $str 76*927933f5SAndreas Gohr * @return string 77*927933f5SAndreas Gohr * @throws TypeError 78*927933f5SAndreas Gohr */ 79*927933f5SAndreas Gohr public static function base32DecodeUpper(string $str): string 80*927933f5SAndreas Gohr { 81*927933f5SAndreas Gohr return Base32::decodeUpper($str); 82*927933f5SAndreas Gohr } 83*927933f5SAndreas Gohr 84*927933f5SAndreas Gohr /** 85*927933f5SAndreas Gohr * RFC 4648 Base32 encoding 86*927933f5SAndreas Gohr * 87*927933f5SAndreas Gohr * @param string $str 88*927933f5SAndreas Gohr * @return string 89*927933f5SAndreas Gohr * @throws TypeError 90*927933f5SAndreas Gohr */ 91*927933f5SAndreas Gohr public static function base32HexEncode(string $str): string 92*927933f5SAndreas Gohr { 93*927933f5SAndreas Gohr return Base32Hex::encode($str); 94*927933f5SAndreas Gohr } 95*927933f5SAndreas Gohr 96*927933f5SAndreas Gohr /** 97*927933f5SAndreas Gohr * RFC 4648 Base32Hex encoding 98*927933f5SAndreas Gohr * 99*927933f5SAndreas Gohr * @param string $str 100*927933f5SAndreas Gohr * @return string 101*927933f5SAndreas Gohr * @throws TypeError 102*927933f5SAndreas Gohr */ 103*927933f5SAndreas Gohr public static function base32HexEncodeUpper(string $str): string 104*927933f5SAndreas Gohr { 105*927933f5SAndreas Gohr return Base32Hex::encodeUpper($str); 106*927933f5SAndreas Gohr } 107*927933f5SAndreas Gohr 108*927933f5SAndreas Gohr /** 109*927933f5SAndreas Gohr * RFC 4648 Base32Hex decoding 110*927933f5SAndreas Gohr * 111*927933f5SAndreas Gohr * @param string $str 112*927933f5SAndreas Gohr * @return string 113*927933f5SAndreas Gohr * @throws TypeError 114*927933f5SAndreas Gohr */ 115*927933f5SAndreas Gohr public static function base32HexDecode(string $str): string 116*927933f5SAndreas Gohr { 117*927933f5SAndreas Gohr return Base32Hex::decode($str); 118*927933f5SAndreas Gohr } 119*927933f5SAndreas Gohr 120*927933f5SAndreas Gohr /** 121*927933f5SAndreas Gohr * RFC 4648 Base32Hex decoding 122*927933f5SAndreas Gohr * 123*927933f5SAndreas Gohr * @param string $str 124*927933f5SAndreas Gohr * @return string 125*927933f5SAndreas Gohr * @throws TypeError 126*927933f5SAndreas Gohr */ 127*927933f5SAndreas Gohr public static function base32HexDecodeUpper(string $str): string 128*927933f5SAndreas Gohr { 129*927933f5SAndreas Gohr return Base32Hex::decodeUpper($str); 130*927933f5SAndreas Gohr } 131*927933f5SAndreas Gohr 132*927933f5SAndreas Gohr /** 133*927933f5SAndreas Gohr * RFC 4648 Base64 encoding 134*927933f5SAndreas Gohr * 135*927933f5SAndreas Gohr * @param string $str 136*927933f5SAndreas Gohr * @return string 137*927933f5SAndreas Gohr * @throws TypeError 138*927933f5SAndreas Gohr */ 139*927933f5SAndreas Gohr public static function base64Encode(string $str): string 140*927933f5SAndreas Gohr { 141*927933f5SAndreas Gohr return Base64::encode($str); 142*927933f5SAndreas Gohr } 143*927933f5SAndreas Gohr 144*927933f5SAndreas Gohr /** 145*927933f5SAndreas Gohr * RFC 4648 Base64 decoding 146*927933f5SAndreas Gohr * 147*927933f5SAndreas Gohr * @param string $str 148*927933f5SAndreas Gohr * @return string 149*927933f5SAndreas Gohr * @throws TypeError 150*927933f5SAndreas Gohr */ 151*927933f5SAndreas Gohr public static function base64Decode(string $str): string 152*927933f5SAndreas Gohr { 153*927933f5SAndreas Gohr return Base64::decode($str); 154*927933f5SAndreas Gohr } 155*927933f5SAndreas Gohr 156*927933f5SAndreas Gohr /** 157*927933f5SAndreas Gohr * Encode into Base64 158*927933f5SAndreas Gohr * 159*927933f5SAndreas Gohr * Base64 character set "./[A-Z][a-z][0-9]" 160*927933f5SAndreas Gohr * @param string $str 161*927933f5SAndreas Gohr * @return string 162*927933f5SAndreas Gohr * @throws TypeError 163*927933f5SAndreas Gohr */ 164*927933f5SAndreas Gohr public static function base64EncodeDotSlash(string $str): string 165*927933f5SAndreas Gohr { 166*927933f5SAndreas Gohr return Base64DotSlash::encode($str); 167*927933f5SAndreas Gohr } 168*927933f5SAndreas Gohr 169*927933f5SAndreas Gohr /** 170*927933f5SAndreas Gohr * Decode from base64 to raw binary 171*927933f5SAndreas Gohr * 172*927933f5SAndreas Gohr * Base64 character set "./[A-Z][a-z][0-9]" 173*927933f5SAndreas Gohr * 174*927933f5SAndreas Gohr * @param string $str 175*927933f5SAndreas Gohr * @return string 176*927933f5SAndreas Gohr * @throws \RangeException 177*927933f5SAndreas Gohr * @throws TypeError 178*927933f5SAndreas Gohr */ 179*927933f5SAndreas Gohr public static function base64DecodeDotSlash(string $str): string 180*927933f5SAndreas Gohr { 181*927933f5SAndreas Gohr return Base64DotSlash::decode($str); 182*927933f5SAndreas Gohr } 183*927933f5SAndreas Gohr 184*927933f5SAndreas Gohr /** 185*927933f5SAndreas Gohr * Encode into Base64 186*927933f5SAndreas Gohr * 187*927933f5SAndreas Gohr * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]" 188*927933f5SAndreas Gohr * @param string $str 189*927933f5SAndreas Gohr * @return string 190*927933f5SAndreas Gohr * @throws TypeError 191*927933f5SAndreas Gohr */ 192*927933f5SAndreas Gohr public static function base64EncodeDotSlashOrdered(string $str): string 193*927933f5SAndreas Gohr { 194*927933f5SAndreas Gohr return Base64DotSlashOrdered::encode($str); 195*927933f5SAndreas Gohr } 196*927933f5SAndreas Gohr 197*927933f5SAndreas Gohr /** 198*927933f5SAndreas Gohr * Decode from base64 to raw binary 199*927933f5SAndreas Gohr * 200*927933f5SAndreas Gohr * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]" 201*927933f5SAndreas Gohr * 202*927933f5SAndreas Gohr * @param string $str 203*927933f5SAndreas Gohr * @return string 204*927933f5SAndreas Gohr * @throws \RangeException 205*927933f5SAndreas Gohr * @throws TypeError 206*927933f5SAndreas Gohr */ 207*927933f5SAndreas Gohr public static function base64DecodeDotSlashOrdered(string $str): string 208*927933f5SAndreas Gohr { 209*927933f5SAndreas Gohr return Base64DotSlashOrdered::decode($str); 210*927933f5SAndreas Gohr } 211*927933f5SAndreas Gohr 212*927933f5SAndreas Gohr /** 213*927933f5SAndreas Gohr * Convert a binary string into a hexadecimal string without cache-timing 214*927933f5SAndreas Gohr * leaks 215*927933f5SAndreas Gohr * 216*927933f5SAndreas Gohr * @param string $bin_string (raw binary) 217*927933f5SAndreas Gohr * @return string 218*927933f5SAndreas Gohr * @throws TypeError 219*927933f5SAndreas Gohr */ 220*927933f5SAndreas Gohr public static function hexEncode(string $bin_string): string 221*927933f5SAndreas Gohr { 222*927933f5SAndreas Gohr return Hex::encode($bin_string); 223*927933f5SAndreas Gohr } 224*927933f5SAndreas Gohr 225*927933f5SAndreas Gohr /** 226*927933f5SAndreas Gohr * Convert a hexadecimal string into a binary string without cache-timing 227*927933f5SAndreas Gohr * leaks 228*927933f5SAndreas Gohr * 229*927933f5SAndreas Gohr * @param string $hex_string 230*927933f5SAndreas Gohr * @return string (raw binary) 231*927933f5SAndreas Gohr * @throws \RangeException 232*927933f5SAndreas Gohr */ 233*927933f5SAndreas Gohr public static function hexDecode(string $hex_string): string 234*927933f5SAndreas Gohr { 235*927933f5SAndreas Gohr return Hex::decode($hex_string); 236*927933f5SAndreas Gohr } 237*927933f5SAndreas Gohr 238*927933f5SAndreas Gohr /** 239*927933f5SAndreas Gohr * Convert a binary string into a hexadecimal string without cache-timing 240*927933f5SAndreas Gohr * leaks 241*927933f5SAndreas Gohr * 242*927933f5SAndreas Gohr * @param string $bin_string (raw binary) 243*927933f5SAndreas Gohr * @return string 244*927933f5SAndreas Gohr * @throws TypeError 245*927933f5SAndreas Gohr */ 246*927933f5SAndreas Gohr public static function hexEncodeUpper(string $bin_string): string 247*927933f5SAndreas Gohr { 248*927933f5SAndreas Gohr return Hex::encodeUpper($bin_string); 249*927933f5SAndreas Gohr } 250*927933f5SAndreas Gohr 251*927933f5SAndreas Gohr /** 252*927933f5SAndreas Gohr * Convert a binary string into a hexadecimal string without cache-timing 253*927933f5SAndreas Gohr * leaks 254*927933f5SAndreas Gohr * 255*927933f5SAndreas Gohr * @param string $bin_string (raw binary) 256*927933f5SAndreas Gohr * @return string 257*927933f5SAndreas Gohr */ 258*927933f5SAndreas Gohr public static function hexDecodeUpper(string $bin_string): string 259*927933f5SAndreas Gohr { 260*927933f5SAndreas Gohr return Hex::decode($bin_string); 261*927933f5SAndreas Gohr } 262*927933f5SAndreas Gohr} 263