1927933f5SAndreas Gohr<?php 2927933f5SAndreas Gohrdeclare(strict_types=1); 3927933f5SAndreas Gohrnamespace ParagonIE\ConstantTime; 4927933f5SAndreas Gohr 5*8e88a29bSAndreas Gohruse SensitiveParameter; 6927933f5SAndreas Gohruse TypeError; 7927933f5SAndreas Gohr 8927933f5SAndreas Gohr/** 9927933f5SAndreas Gohr * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. 10927933f5SAndreas Gohr * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) 11927933f5SAndreas Gohr * 12927933f5SAndreas Gohr * Permission is hereby granted, free of charge, to any person obtaining a copy 13927933f5SAndreas Gohr * of this software and associated documentation files (the "Software"), to deal 14927933f5SAndreas Gohr * in the Software without restriction, including without limitation the rights 15927933f5SAndreas Gohr * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16927933f5SAndreas Gohr * copies of the Software, and to permit persons to whom the Software is 17927933f5SAndreas Gohr * furnished to do so, subject to the following conditions: 18927933f5SAndreas Gohr * 19927933f5SAndreas Gohr * The above copyright notice and this permission notice shall be included in all 20927933f5SAndreas Gohr * copies or substantial portions of the Software. 21927933f5SAndreas Gohr * 22927933f5SAndreas Gohr * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23927933f5SAndreas Gohr * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24927933f5SAndreas Gohr * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25927933f5SAndreas Gohr * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26927933f5SAndreas Gohr * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27927933f5SAndreas Gohr * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28927933f5SAndreas Gohr * SOFTWARE. 29927933f5SAndreas Gohr */ 30927933f5SAndreas Gohr 31927933f5SAndreas Gohr/** 32927933f5SAndreas Gohr * Class RFC4648 33927933f5SAndreas Gohr * 34927933f5SAndreas Gohr * This class conforms strictly to the RFC 35927933f5SAndreas Gohr * 36927933f5SAndreas Gohr * @package ParagonIE\ConstantTime 37*8e88a29bSAndreas Gohr * @api 38927933f5SAndreas Gohr */ 39927933f5SAndreas Gohrabstract class RFC4648 40927933f5SAndreas Gohr{ 41927933f5SAndreas Gohr /** 42927933f5SAndreas Gohr * RFC 4648 Base64 encoding 43927933f5SAndreas Gohr * 44927933f5SAndreas Gohr * "foo" -> "Zm9v" 45927933f5SAndreas Gohr * 46927933f5SAndreas Gohr * @param string $str 47927933f5SAndreas Gohr * @return string 48927933f5SAndreas Gohr * 49927933f5SAndreas Gohr * @throws TypeError 50927933f5SAndreas Gohr */ 51850e6620SAndreas Gohr public static function base64Encode( 52*8e88a29bSAndreas Gohr #[SensitiveParameter] 53850e6620SAndreas Gohr string $str 54850e6620SAndreas Gohr ): string { 55927933f5SAndreas Gohr return Base64::encode($str); 56927933f5SAndreas Gohr } 57927933f5SAndreas Gohr 58927933f5SAndreas Gohr /** 59927933f5SAndreas Gohr * RFC 4648 Base64 decoding 60927933f5SAndreas Gohr * 61927933f5SAndreas Gohr * "Zm9v" -> "foo" 62927933f5SAndreas Gohr * 63927933f5SAndreas Gohr * @param string $str 64927933f5SAndreas Gohr * @return string 65927933f5SAndreas Gohr * 66927933f5SAndreas Gohr * @throws TypeError 67927933f5SAndreas Gohr */ 68850e6620SAndreas Gohr public static function base64Decode( 69*8e88a29bSAndreas Gohr #[SensitiveParameter] 70850e6620SAndreas Gohr string $str 71850e6620SAndreas Gohr ): string { 72927933f5SAndreas Gohr return Base64::decode($str, true); 73927933f5SAndreas Gohr } 74927933f5SAndreas Gohr 75927933f5SAndreas Gohr /** 76927933f5SAndreas Gohr * RFC 4648 Base64 (URL Safe) encoding 77927933f5SAndreas Gohr * 78927933f5SAndreas Gohr * "foo" -> "Zm9v" 79927933f5SAndreas Gohr * 80927933f5SAndreas Gohr * @param string $str 81927933f5SAndreas Gohr * @return string 82927933f5SAndreas Gohr * 83927933f5SAndreas Gohr * @throws TypeError 84927933f5SAndreas Gohr */ 85850e6620SAndreas Gohr public static function base64UrlSafeEncode( 86*8e88a29bSAndreas Gohr #[SensitiveParameter] 87850e6620SAndreas Gohr string $str 88850e6620SAndreas Gohr ): string { 89927933f5SAndreas Gohr return Base64UrlSafe::encode($str); 90927933f5SAndreas Gohr } 91927933f5SAndreas Gohr 92927933f5SAndreas Gohr /** 93927933f5SAndreas Gohr * RFC 4648 Base64 (URL Safe) decoding 94927933f5SAndreas Gohr * 95927933f5SAndreas Gohr * "Zm9v" -> "foo" 96927933f5SAndreas Gohr * 97927933f5SAndreas Gohr * @param string $str 98927933f5SAndreas Gohr * @return string 99927933f5SAndreas Gohr * 100927933f5SAndreas Gohr * @throws TypeError 101927933f5SAndreas Gohr */ 102850e6620SAndreas Gohr public static function base64UrlSafeDecode( 103*8e88a29bSAndreas Gohr #[SensitiveParameter] 104850e6620SAndreas Gohr string $str 105850e6620SAndreas Gohr ): string { 106927933f5SAndreas Gohr return Base64UrlSafe::decode($str, true); 107927933f5SAndreas Gohr } 108927933f5SAndreas Gohr 109927933f5SAndreas Gohr /** 110927933f5SAndreas Gohr * RFC 4648 Base32 encoding 111927933f5SAndreas Gohr * 112927933f5SAndreas Gohr * "foo" -> "MZXW6===" 113927933f5SAndreas Gohr * 114927933f5SAndreas Gohr * @param string $str 115927933f5SAndreas Gohr * @return string 116927933f5SAndreas Gohr * 117927933f5SAndreas Gohr * @throws TypeError 118927933f5SAndreas Gohr */ 119850e6620SAndreas Gohr public static function base32Encode( 120*8e88a29bSAndreas Gohr #[SensitiveParameter] 121850e6620SAndreas Gohr string $str 122850e6620SAndreas Gohr ): string { 123927933f5SAndreas Gohr return Base32::encodeUpper($str); 124927933f5SAndreas Gohr } 125927933f5SAndreas Gohr 126927933f5SAndreas Gohr /** 127927933f5SAndreas Gohr * RFC 4648 Base32 encoding 128927933f5SAndreas Gohr * 129927933f5SAndreas Gohr * "MZXW6===" -> "foo" 130927933f5SAndreas Gohr * 131927933f5SAndreas Gohr * @param string $str 132927933f5SAndreas Gohr * @return string 133927933f5SAndreas Gohr * 134927933f5SAndreas Gohr * @throws TypeError 135927933f5SAndreas Gohr */ 136850e6620SAndreas Gohr public static function base32Decode( 137*8e88a29bSAndreas Gohr #[SensitiveParameter] 138850e6620SAndreas Gohr string $str 139850e6620SAndreas Gohr ): string { 140927933f5SAndreas Gohr return Base32::decodeUpper($str, true); 141927933f5SAndreas Gohr } 142927933f5SAndreas Gohr 143927933f5SAndreas Gohr /** 144927933f5SAndreas Gohr * RFC 4648 Base32-Hex encoding 145927933f5SAndreas Gohr * 146927933f5SAndreas Gohr * "foo" -> "CPNMU===" 147927933f5SAndreas Gohr * 148927933f5SAndreas Gohr * @param string $str 149927933f5SAndreas Gohr * @return string 150927933f5SAndreas Gohr * 151927933f5SAndreas Gohr * @throws TypeError 152927933f5SAndreas Gohr */ 153850e6620SAndreas Gohr public static function base32HexEncode( 154*8e88a29bSAndreas Gohr #[SensitiveParameter] 155850e6620SAndreas Gohr string $str 156850e6620SAndreas Gohr ): string { 157927933f5SAndreas Gohr return Base32::encodeUpper($str); 158927933f5SAndreas Gohr } 159927933f5SAndreas Gohr 160927933f5SAndreas Gohr /** 161927933f5SAndreas Gohr * RFC 4648 Base32-Hex decoding 162927933f5SAndreas Gohr * 163927933f5SAndreas Gohr * "CPNMU===" -> "foo" 164927933f5SAndreas Gohr * 165927933f5SAndreas Gohr * @param string $str 166927933f5SAndreas Gohr * @return string 167927933f5SAndreas Gohr * 168927933f5SAndreas Gohr * @throws TypeError 169927933f5SAndreas Gohr */ 170850e6620SAndreas Gohr public static function base32HexDecode( 171*8e88a29bSAndreas Gohr #[SensitiveParameter] 172850e6620SAndreas Gohr string $str 173850e6620SAndreas Gohr ): string { 174927933f5SAndreas Gohr return Base32::decodeUpper($str, true); 175927933f5SAndreas Gohr } 176927933f5SAndreas Gohr 177927933f5SAndreas Gohr /** 178927933f5SAndreas Gohr * RFC 4648 Base16 decoding 179927933f5SAndreas Gohr * 180927933f5SAndreas Gohr * "foo" -> "666F6F" 181927933f5SAndreas Gohr * 182927933f5SAndreas Gohr * @param string $str 183927933f5SAndreas Gohr * @return string 184927933f5SAndreas Gohr * 185927933f5SAndreas Gohr * @throws TypeError 186927933f5SAndreas Gohr */ 187850e6620SAndreas Gohr public static function base16Encode( 188*8e88a29bSAndreas Gohr #[SensitiveParameter] 189850e6620SAndreas Gohr string $str 190850e6620SAndreas Gohr ): string { 191927933f5SAndreas Gohr return Hex::encodeUpper($str); 192927933f5SAndreas Gohr } 193927933f5SAndreas Gohr 194927933f5SAndreas Gohr /** 195927933f5SAndreas Gohr * RFC 4648 Base16 decoding 196927933f5SAndreas Gohr * 197927933f5SAndreas Gohr * "666F6F" -> "foo" 198927933f5SAndreas Gohr * 199927933f5SAndreas Gohr * @param string $str 200927933f5SAndreas Gohr * @return string 201927933f5SAndreas Gohr */ 202850e6620SAndreas Gohr public static function base16Decode( 203*8e88a29bSAndreas Gohr #[SensitiveParameter] 204850e6620SAndreas Gohr string $str 205850e6620SAndreas Gohr ): string { 206927933f5SAndreas Gohr return Hex::decode($str, true); 207927933f5SAndreas Gohr } 208927933f5SAndreas Gohr} 209