1927933f5SAndreas Gohr<?php 2927933f5SAndreas Gohrdeclare(strict_types=1); 3927933f5SAndreas Gohrnamespace ParagonIE\ConstantTime; 4927933f5SAndreas Gohr 5*8e88a29bSAndreas Gohruse Override; 6*8e88a29bSAndreas 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 Base64UrlSafe 32927933f5SAndreas Gohr * [A-Z][a-z][0-9]\-_ 33927933f5SAndreas Gohr * 34927933f5SAndreas Gohr * @package ParagonIE\ConstantTime 35927933f5SAndreas Gohr */ 36927933f5SAndreas Gohrabstract class Base64UrlSafe extends Base64 37927933f5SAndreas Gohr{ 38927933f5SAndreas Gohr 39927933f5SAndreas Gohr /** 40927933f5SAndreas Gohr * Uses bitwise operators instead of table-lookups to turn 6-bit integers 41927933f5SAndreas Gohr * into 8-bit integers. 42927933f5SAndreas Gohr * 43927933f5SAndreas Gohr * Base64 character set: 44927933f5SAndreas Gohr * [A-Z] [a-z] [0-9] - _ 45927933f5SAndreas Gohr * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2d, 0x5f 46927933f5SAndreas Gohr * 47927933f5SAndreas Gohr * @param int $src 48927933f5SAndreas Gohr * @return int 49927933f5SAndreas Gohr */ 50*8e88a29bSAndreas Gohr #[Override] 51927933f5SAndreas Gohr protected static function decode6Bits(int $src): int 52927933f5SAndreas Gohr { 53927933f5SAndreas Gohr $ret = -1; 54927933f5SAndreas Gohr 55927933f5SAndreas Gohr // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 56927933f5SAndreas Gohr $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); 57927933f5SAndreas Gohr 58927933f5SAndreas Gohr // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70 59927933f5SAndreas Gohr $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70); 60927933f5SAndreas Gohr 61927933f5SAndreas Gohr // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5 62927933f5SAndreas Gohr $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5); 63927933f5SAndreas Gohr 64927933f5SAndreas Gohr // if ($src == 0x2c) $ret += 62 + 1; 65927933f5SAndreas Gohr $ret += (((0x2c - $src) & ($src - 0x2e)) >> 8) & 63; 66927933f5SAndreas Gohr 67927933f5SAndreas Gohr // if ($src == 0x5f) ret += 63 + 1; 68927933f5SAndreas Gohr $ret += (((0x5e - $src) & ($src - 0x60)) >> 8) & 64; 69927933f5SAndreas Gohr 70927933f5SAndreas Gohr return $ret; 71927933f5SAndreas Gohr } 72927933f5SAndreas Gohr 73927933f5SAndreas Gohr /** 74927933f5SAndreas Gohr * Uses bitwise operators instead of table-lookups to turn 8-bit integers 75927933f5SAndreas Gohr * into 6-bit integers. 76927933f5SAndreas Gohr * 77927933f5SAndreas Gohr * @param int $src 78927933f5SAndreas Gohr * @return string 79927933f5SAndreas Gohr */ 80*8e88a29bSAndreas Gohr #[Override] 81927933f5SAndreas Gohr protected static function encode6Bits(int $src): string 82927933f5SAndreas Gohr { 83927933f5SAndreas Gohr $diff = 0x41; 84927933f5SAndreas Gohr 85927933f5SAndreas Gohr // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 86927933f5SAndreas Gohr $diff += ((25 - $src) >> 8) & 6; 87927933f5SAndreas Gohr 88927933f5SAndreas Gohr // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75 89927933f5SAndreas Gohr $diff -= ((51 - $src) >> 8) & 75; 90927933f5SAndreas Gohr 91927933f5SAndreas Gohr // if ($src > 61) $diff += 0x2d - 0x30 - 10; // -13 92927933f5SAndreas Gohr $diff -= ((61 - $src) >> 8) & 13; 93927933f5SAndreas Gohr 94927933f5SAndreas Gohr // if ($src > 62) $diff += 0x5f - 0x2b - 1; // 3 95927933f5SAndreas Gohr $diff += ((62 - $src) >> 8) & 49; 96927933f5SAndreas Gohr 97927933f5SAndreas Gohr return \pack('C', $src + $diff); 98927933f5SAndreas Gohr } 99927933f5SAndreas Gohr} 100