1927933f5SAndreas Gohr<?php 2927933f5SAndreas Gohrdeclare(strict_types=1); 3927933f5SAndreas Gohrnamespace ParagonIE\ConstantTime; 4927933f5SAndreas Gohr 5927933f5SAndreas Gohruse TypeError; 6927933f5SAndreas Gohr 7927933f5SAndreas Gohr/** 8927933f5SAndreas Gohr * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. 9927933f5SAndreas Gohr * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) 10927933f5SAndreas Gohr * 11927933f5SAndreas Gohr * Permission is hereby granted, free of charge, to any person obtaining a copy 12927933f5SAndreas Gohr * of this software and associated documentation files (the "Software"), to deal 13927933f5SAndreas Gohr * in the Software without restriction, including without limitation the rights 14927933f5SAndreas Gohr * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15927933f5SAndreas Gohr * copies of the Software, and to permit persons to whom the Software is 16927933f5SAndreas Gohr * furnished to do so, subject to the following conditions: 17927933f5SAndreas Gohr * 18927933f5SAndreas Gohr * The above copyright notice and this permission notice shall be included in all 19927933f5SAndreas Gohr * copies or substantial portions of the Software. 20927933f5SAndreas Gohr * 21927933f5SAndreas Gohr * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22927933f5SAndreas Gohr * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23927933f5SAndreas Gohr * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24927933f5SAndreas Gohr * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25927933f5SAndreas Gohr * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26927933f5SAndreas Gohr * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27927933f5SAndreas Gohr * SOFTWARE. 28927933f5SAndreas Gohr */ 29927933f5SAndreas Gohr 30927933f5SAndreas Gohr/** 31927933f5SAndreas Gohr * Class RFC4648 32927933f5SAndreas Gohr * 33927933f5SAndreas Gohr * This class conforms strictly to the RFC 34927933f5SAndreas Gohr * 35927933f5SAndreas Gohr * @package ParagonIE\ConstantTime 36927933f5SAndreas Gohr */ 37927933f5SAndreas Gohrabstract class RFC4648 38927933f5SAndreas Gohr{ 39927933f5SAndreas Gohr /** 40927933f5SAndreas Gohr * RFC 4648 Base64 encoding 41927933f5SAndreas Gohr * 42927933f5SAndreas Gohr * "foo" -> "Zm9v" 43927933f5SAndreas Gohr * 44927933f5SAndreas Gohr * @param string $str 45927933f5SAndreas Gohr * @return string 46927933f5SAndreas Gohr * 47927933f5SAndreas Gohr * @throws TypeError 48927933f5SAndreas Gohr */ 49*850e6620SAndreas Gohr public static function base64Encode( 50*850e6620SAndreas Gohr #[\SensitiveParameter] 51*850e6620SAndreas Gohr string $str 52*850e6620SAndreas Gohr ): string { 53927933f5SAndreas Gohr return Base64::encode($str); 54927933f5SAndreas Gohr } 55927933f5SAndreas Gohr 56927933f5SAndreas Gohr /** 57927933f5SAndreas Gohr * RFC 4648 Base64 decoding 58927933f5SAndreas Gohr * 59927933f5SAndreas Gohr * "Zm9v" -> "foo" 60927933f5SAndreas Gohr * 61927933f5SAndreas Gohr * @param string $str 62927933f5SAndreas Gohr * @return string 63927933f5SAndreas Gohr * 64927933f5SAndreas Gohr * @throws TypeError 65927933f5SAndreas Gohr */ 66*850e6620SAndreas Gohr public static function base64Decode( 67*850e6620SAndreas Gohr #[\SensitiveParameter] 68*850e6620SAndreas Gohr string $str 69*850e6620SAndreas Gohr ): string { 70927933f5SAndreas Gohr return Base64::decode($str, true); 71927933f5SAndreas Gohr } 72927933f5SAndreas Gohr 73927933f5SAndreas Gohr /** 74927933f5SAndreas Gohr * RFC 4648 Base64 (URL Safe) encoding 75927933f5SAndreas Gohr * 76927933f5SAndreas Gohr * "foo" -> "Zm9v" 77927933f5SAndreas Gohr * 78927933f5SAndreas Gohr * @param string $str 79927933f5SAndreas Gohr * @return string 80927933f5SAndreas Gohr * 81927933f5SAndreas Gohr * @throws TypeError 82927933f5SAndreas Gohr */ 83*850e6620SAndreas Gohr public static function base64UrlSafeEncode( 84*850e6620SAndreas Gohr #[\SensitiveParameter] 85*850e6620SAndreas Gohr string $str 86*850e6620SAndreas Gohr ): string { 87927933f5SAndreas Gohr return Base64UrlSafe::encode($str); 88927933f5SAndreas Gohr } 89927933f5SAndreas Gohr 90927933f5SAndreas Gohr /** 91927933f5SAndreas Gohr * RFC 4648 Base64 (URL Safe) decoding 92927933f5SAndreas Gohr * 93927933f5SAndreas Gohr * "Zm9v" -> "foo" 94927933f5SAndreas Gohr * 95927933f5SAndreas Gohr * @param string $str 96927933f5SAndreas Gohr * @return string 97927933f5SAndreas Gohr * 98927933f5SAndreas Gohr * @throws TypeError 99927933f5SAndreas Gohr */ 100*850e6620SAndreas Gohr public static function base64UrlSafeDecode( 101*850e6620SAndreas Gohr #[\SensitiveParameter] 102*850e6620SAndreas Gohr string $str 103*850e6620SAndreas Gohr ): string { 104927933f5SAndreas Gohr return Base64UrlSafe::decode($str, true); 105927933f5SAndreas Gohr } 106927933f5SAndreas Gohr 107927933f5SAndreas Gohr /** 108927933f5SAndreas Gohr * RFC 4648 Base32 encoding 109927933f5SAndreas Gohr * 110927933f5SAndreas Gohr * "foo" -> "MZXW6===" 111927933f5SAndreas Gohr * 112927933f5SAndreas Gohr * @param string $str 113927933f5SAndreas Gohr * @return string 114927933f5SAndreas Gohr * 115927933f5SAndreas Gohr * @throws TypeError 116927933f5SAndreas Gohr */ 117*850e6620SAndreas Gohr public static function base32Encode( 118*850e6620SAndreas Gohr #[\SensitiveParameter] 119*850e6620SAndreas Gohr string $str 120*850e6620SAndreas Gohr ): string { 121927933f5SAndreas Gohr return Base32::encodeUpper($str); 122927933f5SAndreas Gohr } 123927933f5SAndreas Gohr 124927933f5SAndreas Gohr /** 125927933f5SAndreas Gohr * RFC 4648 Base32 encoding 126927933f5SAndreas Gohr * 127927933f5SAndreas Gohr * "MZXW6===" -> "foo" 128927933f5SAndreas Gohr * 129927933f5SAndreas Gohr * @param string $str 130927933f5SAndreas Gohr * @return string 131927933f5SAndreas Gohr * 132927933f5SAndreas Gohr * @throws TypeError 133927933f5SAndreas Gohr */ 134*850e6620SAndreas Gohr public static function base32Decode( 135*850e6620SAndreas Gohr #[\SensitiveParameter] 136*850e6620SAndreas Gohr string $str 137*850e6620SAndreas Gohr ): string { 138927933f5SAndreas Gohr return Base32::decodeUpper($str, true); 139927933f5SAndreas Gohr } 140927933f5SAndreas Gohr 141927933f5SAndreas Gohr /** 142927933f5SAndreas Gohr * RFC 4648 Base32-Hex encoding 143927933f5SAndreas Gohr * 144927933f5SAndreas Gohr * "foo" -> "CPNMU===" 145927933f5SAndreas Gohr * 146927933f5SAndreas Gohr * @param string $str 147927933f5SAndreas Gohr * @return string 148927933f5SAndreas Gohr * 149927933f5SAndreas Gohr * @throws TypeError 150927933f5SAndreas Gohr */ 151*850e6620SAndreas Gohr public static function base32HexEncode( 152*850e6620SAndreas Gohr #[\SensitiveParameter] 153*850e6620SAndreas Gohr string $str 154*850e6620SAndreas Gohr ): string { 155927933f5SAndreas Gohr return Base32::encodeUpper($str); 156927933f5SAndreas Gohr } 157927933f5SAndreas Gohr 158927933f5SAndreas Gohr /** 159927933f5SAndreas Gohr * RFC 4648 Base32-Hex decoding 160927933f5SAndreas Gohr * 161927933f5SAndreas Gohr * "CPNMU===" -> "foo" 162927933f5SAndreas Gohr * 163927933f5SAndreas Gohr * @param string $str 164927933f5SAndreas Gohr * @return string 165927933f5SAndreas Gohr * 166927933f5SAndreas Gohr * @throws TypeError 167927933f5SAndreas Gohr */ 168*850e6620SAndreas Gohr public static function base32HexDecode( 169*850e6620SAndreas Gohr #[\SensitiveParameter] 170*850e6620SAndreas Gohr string $str 171*850e6620SAndreas Gohr ): string { 172927933f5SAndreas Gohr return Base32::decodeUpper($str, true); 173927933f5SAndreas Gohr } 174927933f5SAndreas Gohr 175927933f5SAndreas Gohr /** 176927933f5SAndreas Gohr * RFC 4648 Base16 decoding 177927933f5SAndreas Gohr * 178927933f5SAndreas Gohr * "foo" -> "666F6F" 179927933f5SAndreas Gohr * 180927933f5SAndreas Gohr * @param string $str 181927933f5SAndreas Gohr * @return string 182927933f5SAndreas Gohr * 183927933f5SAndreas Gohr * @throws TypeError 184927933f5SAndreas Gohr */ 185*850e6620SAndreas Gohr public static function base16Encode( 186*850e6620SAndreas Gohr #[\SensitiveParameter] 187*850e6620SAndreas Gohr string $str 188*850e6620SAndreas Gohr ): string { 189927933f5SAndreas Gohr return Hex::encodeUpper($str); 190927933f5SAndreas Gohr } 191927933f5SAndreas Gohr 192927933f5SAndreas Gohr /** 193927933f5SAndreas Gohr * RFC 4648 Base16 decoding 194927933f5SAndreas Gohr * 195927933f5SAndreas Gohr * "666F6F" -> "foo" 196927933f5SAndreas Gohr * 197927933f5SAndreas Gohr * @param string $str 198927933f5SAndreas Gohr * @return string 199927933f5SAndreas Gohr */ 200*850e6620SAndreas Gohr public static function base16Decode( 201*850e6620SAndreas Gohr #[\SensitiveParameter] 202*850e6620SAndreas Gohr string $str 203*850e6620SAndreas Gohr ): string { 204927933f5SAndreas Gohr return Hex::decode($str, true); 205927933f5SAndreas Gohr } 206927933f5SAndreas Gohr} 207