1927933f5SAndreas Gohr<?php 2927933f5SAndreas Gohrdeclare(strict_types=1); 3927933f5SAndreas Gohrnamespace ParagonIE\ConstantTime; 4927933f5SAndreas Gohr 5*8e88a29bSAndreas Gohruse RangeException; 6*8e88a29bSAndreas Gohruse SensitiveParameter; 7927933f5SAndreas Gohruse TypeError; 8927933f5SAndreas Gohr 9927933f5SAndreas Gohr/** 10927933f5SAndreas Gohr * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. 11927933f5SAndreas Gohr * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) 12927933f5SAndreas Gohr * 13927933f5SAndreas Gohr * Permission is hereby granted, free of charge, to any person obtaining a copy 14927933f5SAndreas Gohr * of this software and associated documentation files (the "Software"), to deal 15927933f5SAndreas Gohr * in the Software without restriction, including without limitation the rights 16927933f5SAndreas Gohr * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17927933f5SAndreas Gohr * copies of the Software, and to permit persons to whom the Software is 18927933f5SAndreas Gohr * furnished to do so, subject to the following conditions: 19927933f5SAndreas Gohr * 20927933f5SAndreas Gohr * The above copyright notice and this permission notice shall be included in all 21927933f5SAndreas Gohr * copies or substantial portions of the Software. 22927933f5SAndreas Gohr * 23927933f5SAndreas Gohr * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24927933f5SAndreas Gohr * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25927933f5SAndreas Gohr * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26927933f5SAndreas Gohr * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27927933f5SAndreas Gohr * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28927933f5SAndreas Gohr * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 29927933f5SAndreas Gohr * SOFTWARE. 30927933f5SAndreas Gohr */ 31927933f5SAndreas Gohr 32927933f5SAndreas Gohr/** 33927933f5SAndreas Gohr * Class Encoding 34927933f5SAndreas Gohr * @package ParagonIE\ConstantTime 35*8e88a29bSAndreas Gohr * @api 36927933f5SAndreas Gohr */ 37927933f5SAndreas Gohrabstract class Encoding 38927933f5SAndreas Gohr{ 39927933f5SAndreas Gohr /** 40927933f5SAndreas Gohr * RFC 4648 Base32 encoding 41927933f5SAndreas Gohr * 42927933f5SAndreas Gohr * @param string $str 43927933f5SAndreas Gohr * @return string 44927933f5SAndreas Gohr * @throws TypeError 45927933f5SAndreas Gohr */ 46850e6620SAndreas Gohr public static function base32Encode( 47*8e88a29bSAndreas Gohr #[SensitiveParameter] 48850e6620SAndreas Gohr string $str 49850e6620SAndreas Gohr ): string { 50927933f5SAndreas Gohr return Base32::encode($str); 51927933f5SAndreas Gohr } 52927933f5SAndreas Gohr 53927933f5SAndreas Gohr /** 54927933f5SAndreas Gohr * RFC 4648 Base32 encoding 55927933f5SAndreas Gohr * 56927933f5SAndreas Gohr * @param string $str 57927933f5SAndreas Gohr * @return string 58927933f5SAndreas Gohr * @throws TypeError 59927933f5SAndreas Gohr */ 60850e6620SAndreas Gohr public static function base32EncodeUpper( 61*8e88a29bSAndreas Gohr #[SensitiveParameter] 62850e6620SAndreas Gohr string $str 63850e6620SAndreas Gohr ): string { 64927933f5SAndreas Gohr return Base32::encodeUpper($str); 65927933f5SAndreas Gohr } 66927933f5SAndreas Gohr 67927933f5SAndreas Gohr /** 68927933f5SAndreas Gohr * RFC 4648 Base32 decoding 69927933f5SAndreas Gohr * 70927933f5SAndreas Gohr * @param string $str 71927933f5SAndreas Gohr * @return string 72927933f5SAndreas Gohr * @throws TypeError 73927933f5SAndreas Gohr */ 74850e6620SAndreas Gohr public static function base32Decode( 75*8e88a29bSAndreas Gohr #[SensitiveParameter] 76850e6620SAndreas Gohr string $str 77850e6620SAndreas Gohr ): string { 78927933f5SAndreas Gohr return Base32::decode($str); 79927933f5SAndreas Gohr } 80927933f5SAndreas Gohr 81927933f5SAndreas Gohr /** 82927933f5SAndreas Gohr * RFC 4648 Base32 decoding 83927933f5SAndreas Gohr * 84927933f5SAndreas Gohr * @param string $str 85927933f5SAndreas Gohr * @return string 86927933f5SAndreas Gohr * @throws TypeError 87927933f5SAndreas Gohr */ 88850e6620SAndreas Gohr public static function base32DecodeUpper( 89*8e88a29bSAndreas Gohr #[SensitiveParameter] 90850e6620SAndreas Gohr string $str 91850e6620SAndreas Gohr ): string { 92927933f5SAndreas Gohr return Base32::decodeUpper($str); 93927933f5SAndreas Gohr } 94927933f5SAndreas Gohr 95927933f5SAndreas Gohr /** 96927933f5SAndreas Gohr * RFC 4648 Base32 encoding 97927933f5SAndreas Gohr * 98927933f5SAndreas Gohr * @param string $str 99927933f5SAndreas Gohr * @return string 100927933f5SAndreas Gohr * @throws TypeError 101927933f5SAndreas Gohr */ 102850e6620SAndreas Gohr public static function base32HexEncode( 103*8e88a29bSAndreas Gohr #[SensitiveParameter] 104850e6620SAndreas Gohr string $str 105850e6620SAndreas Gohr ): string { 106927933f5SAndreas Gohr return Base32Hex::encode($str); 107927933f5SAndreas Gohr } 108927933f5SAndreas Gohr 109927933f5SAndreas Gohr /** 110927933f5SAndreas Gohr * RFC 4648 Base32Hex encoding 111927933f5SAndreas Gohr * 112927933f5SAndreas Gohr * @param string $str 113927933f5SAndreas Gohr * @return string 114927933f5SAndreas Gohr * @throws TypeError 115927933f5SAndreas Gohr */ 116850e6620SAndreas Gohr public static function base32HexEncodeUpper( 117*8e88a29bSAndreas Gohr #[SensitiveParameter] 118850e6620SAndreas Gohr string $str 119850e6620SAndreas Gohr ): string { 120927933f5SAndreas Gohr return Base32Hex::encodeUpper($str); 121927933f5SAndreas Gohr } 122927933f5SAndreas Gohr 123927933f5SAndreas Gohr /** 124927933f5SAndreas Gohr * RFC 4648 Base32Hex decoding 125927933f5SAndreas Gohr * 126927933f5SAndreas Gohr * @param string $str 127927933f5SAndreas Gohr * @return string 128927933f5SAndreas Gohr * @throws TypeError 129927933f5SAndreas Gohr */ 130850e6620SAndreas Gohr public static function base32HexDecode( 131*8e88a29bSAndreas Gohr #[SensitiveParameter] 132850e6620SAndreas Gohr string $str 133850e6620SAndreas Gohr ): string { 134927933f5SAndreas Gohr return Base32Hex::decode($str); 135927933f5SAndreas Gohr } 136927933f5SAndreas Gohr 137927933f5SAndreas Gohr /** 138927933f5SAndreas Gohr * RFC 4648 Base32Hex decoding 139927933f5SAndreas Gohr * 140927933f5SAndreas Gohr * @param string $str 141927933f5SAndreas Gohr * @return string 142927933f5SAndreas Gohr * @throws TypeError 143927933f5SAndreas Gohr */ 144850e6620SAndreas Gohr public static function base32HexDecodeUpper( 145*8e88a29bSAndreas Gohr #[SensitiveParameter] 146850e6620SAndreas Gohr string $str 147850e6620SAndreas Gohr ): string { 148927933f5SAndreas Gohr return Base32Hex::decodeUpper($str); 149927933f5SAndreas Gohr } 150927933f5SAndreas Gohr 151927933f5SAndreas Gohr /** 152927933f5SAndreas Gohr * RFC 4648 Base64 encoding 153927933f5SAndreas Gohr * 154927933f5SAndreas Gohr * @param string $str 155927933f5SAndreas Gohr * @return string 156927933f5SAndreas Gohr * @throws TypeError 157927933f5SAndreas Gohr */ 158850e6620SAndreas Gohr public static function base64Encode( 159*8e88a29bSAndreas Gohr #[SensitiveParameter] 160850e6620SAndreas Gohr string $str 161850e6620SAndreas Gohr ): string { 162927933f5SAndreas Gohr return Base64::encode($str); 163927933f5SAndreas Gohr } 164927933f5SAndreas Gohr 165927933f5SAndreas Gohr /** 166927933f5SAndreas Gohr * RFC 4648 Base64 decoding 167927933f5SAndreas Gohr * 168927933f5SAndreas Gohr * @param string $str 169927933f5SAndreas Gohr * @return string 170927933f5SAndreas Gohr * @throws TypeError 171927933f5SAndreas Gohr */ 172850e6620SAndreas Gohr public static function base64Decode( 173*8e88a29bSAndreas Gohr #[SensitiveParameter] 174850e6620SAndreas Gohr string $str 175850e6620SAndreas Gohr ): string { 176927933f5SAndreas Gohr return Base64::decode($str); 177927933f5SAndreas Gohr } 178927933f5SAndreas Gohr 179927933f5SAndreas Gohr /** 180927933f5SAndreas Gohr * Encode into Base64 181927933f5SAndreas Gohr * 182927933f5SAndreas Gohr * Base64 character set "./[A-Z][a-z][0-9]" 183927933f5SAndreas Gohr * @param string $str 184927933f5SAndreas Gohr * @return string 185927933f5SAndreas Gohr * @throws TypeError 186927933f5SAndreas Gohr */ 187850e6620SAndreas Gohr public static function base64EncodeDotSlash( 188*8e88a29bSAndreas Gohr #[SensitiveParameter] 189850e6620SAndreas Gohr string $str 190850e6620SAndreas Gohr ): string { 191927933f5SAndreas Gohr return Base64DotSlash::encode($str); 192927933f5SAndreas Gohr } 193927933f5SAndreas Gohr 194927933f5SAndreas Gohr /** 195927933f5SAndreas Gohr * Decode from base64 to raw binary 196927933f5SAndreas Gohr * 197927933f5SAndreas Gohr * Base64 character set "./[A-Z][a-z][0-9]" 198927933f5SAndreas Gohr * 199927933f5SAndreas Gohr * @param string $str 200927933f5SAndreas Gohr * @return string 201*8e88a29bSAndreas Gohr * @throws RangeException 202927933f5SAndreas Gohr * @throws TypeError 203927933f5SAndreas Gohr */ 204850e6620SAndreas Gohr public static function base64DecodeDotSlash( 205*8e88a29bSAndreas Gohr #[SensitiveParameter] 206850e6620SAndreas Gohr string $str 207850e6620SAndreas Gohr ): string { 208927933f5SAndreas Gohr return Base64DotSlash::decode($str); 209927933f5SAndreas Gohr } 210927933f5SAndreas Gohr 211927933f5SAndreas Gohr /** 212927933f5SAndreas Gohr * Encode into Base64 213927933f5SAndreas Gohr * 214927933f5SAndreas Gohr * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]" 215927933f5SAndreas Gohr * @param string $str 216927933f5SAndreas Gohr * @return string 217927933f5SAndreas Gohr * @throws TypeError 218927933f5SAndreas Gohr */ 219850e6620SAndreas Gohr public static function base64EncodeDotSlashOrdered( 220*8e88a29bSAndreas Gohr #[SensitiveParameter] 221850e6620SAndreas Gohr string $str 222850e6620SAndreas Gohr ): string { 223927933f5SAndreas Gohr return Base64DotSlashOrdered::encode($str); 224927933f5SAndreas Gohr } 225927933f5SAndreas Gohr 226927933f5SAndreas Gohr /** 227927933f5SAndreas Gohr * Decode from base64 to raw binary 228927933f5SAndreas Gohr * 229927933f5SAndreas Gohr * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]" 230927933f5SAndreas Gohr * 231927933f5SAndreas Gohr * @param string $str 232927933f5SAndreas Gohr * @return string 233*8e88a29bSAndreas Gohr * @throws RangeException 234927933f5SAndreas Gohr * @throws TypeError 235927933f5SAndreas Gohr */ 236850e6620SAndreas Gohr public static function base64DecodeDotSlashOrdered( 237*8e88a29bSAndreas Gohr #[SensitiveParameter] 238850e6620SAndreas Gohr string $str 239850e6620SAndreas Gohr ): string { 240927933f5SAndreas Gohr return Base64DotSlashOrdered::decode($str); 241927933f5SAndreas Gohr } 242927933f5SAndreas Gohr 243927933f5SAndreas Gohr /** 244927933f5SAndreas Gohr * Convert a binary string into a hexadecimal string without cache-timing 245927933f5SAndreas Gohr * leaks 246927933f5SAndreas Gohr * 247927933f5SAndreas Gohr * @param string $bin_string (raw binary) 248927933f5SAndreas Gohr * @return string 249927933f5SAndreas Gohr * @throws TypeError 250927933f5SAndreas Gohr */ 251850e6620SAndreas Gohr public static function hexEncode( 252*8e88a29bSAndreas Gohr #[SensitiveParameter] 253850e6620SAndreas Gohr string $bin_string 254850e6620SAndreas Gohr ): string { 255927933f5SAndreas Gohr return Hex::encode($bin_string); 256927933f5SAndreas Gohr } 257927933f5SAndreas Gohr 258927933f5SAndreas Gohr /** 259927933f5SAndreas Gohr * Convert a hexadecimal string into a binary string without cache-timing 260927933f5SAndreas Gohr * leaks 261927933f5SAndreas Gohr * 262927933f5SAndreas Gohr * @param string $hex_string 263927933f5SAndreas Gohr * @return string (raw binary) 264*8e88a29bSAndreas Gohr * @throws RangeException 265927933f5SAndreas Gohr */ 266850e6620SAndreas Gohr public static function hexDecode( 267*8e88a29bSAndreas Gohr #[SensitiveParameter] 268850e6620SAndreas Gohr string $hex_string 269850e6620SAndreas Gohr ): string { 270927933f5SAndreas Gohr return Hex::decode($hex_string); 271927933f5SAndreas Gohr } 272927933f5SAndreas Gohr 273927933f5SAndreas Gohr /** 274927933f5SAndreas Gohr * Convert a binary string into a hexadecimal string without cache-timing 275927933f5SAndreas Gohr * leaks 276927933f5SAndreas Gohr * 277927933f5SAndreas Gohr * @param string $bin_string (raw binary) 278927933f5SAndreas Gohr * @return string 279927933f5SAndreas Gohr * @throws TypeError 280927933f5SAndreas Gohr */ 281850e6620SAndreas Gohr public static function hexEncodeUpper( 282*8e88a29bSAndreas Gohr #[SensitiveParameter] 283850e6620SAndreas Gohr string $bin_string 284850e6620SAndreas Gohr ): string { 285927933f5SAndreas Gohr return Hex::encodeUpper($bin_string); 286927933f5SAndreas Gohr } 287927933f5SAndreas Gohr 288927933f5SAndreas Gohr /** 289927933f5SAndreas Gohr * Convert a binary string into a hexadecimal string without cache-timing 290927933f5SAndreas Gohr * leaks 291927933f5SAndreas Gohr * 292927933f5SAndreas Gohr * @param string $bin_string (raw binary) 293927933f5SAndreas Gohr * @return string 294927933f5SAndreas Gohr */ 295850e6620SAndreas Gohr public static function hexDecodeUpper( 296*8e88a29bSAndreas Gohr #[SensitiveParameter] 297850e6620SAndreas Gohr string $bin_string 298850e6620SAndreas Gohr ): string { 299927933f5SAndreas Gohr return Hex::decode($bin_string); 300927933f5SAndreas Gohr } 301927933f5SAndreas Gohr} 302