1927933f5SAndreas Gohr<?php 2927933f5SAndreas Gohrdeclare(strict_types=1); 3927933f5SAndreas Gohrnamespace ParagonIE\ConstantTime; 4927933f5SAndreas Gohr 5*8e88a29bSAndreas Gohruse Override; 6*8e88a29bSAndreas Gohruse function pack; 7*8e88a29bSAndreas 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 Base32Hex 33927933f5SAndreas Gohr * [0-9][A-V] 34927933f5SAndreas Gohr * 35927933f5SAndreas Gohr * @package ParagonIE\ConstantTime 36927933f5SAndreas Gohr */ 37927933f5SAndreas Gohrabstract class Base32Hex extends Base32 38927933f5SAndreas Gohr{ 39927933f5SAndreas Gohr /** 40927933f5SAndreas Gohr * Uses bitwise operators instead of table-lookups to turn 5-bit integers 41927933f5SAndreas Gohr * into 8-bit integers. 42927933f5SAndreas Gohr * 43927933f5SAndreas Gohr * @param int $src 44927933f5SAndreas Gohr * @return int 45927933f5SAndreas Gohr */ 46*8e88a29bSAndreas Gohr #[Override] 47927933f5SAndreas Gohr protected static function decode5Bits(int $src): int 48927933f5SAndreas Gohr { 49927933f5SAndreas Gohr $ret = -1; 50927933f5SAndreas Gohr 51927933f5SAndreas Gohr // if ($src > 0x30 && $src < 0x3a) ret += $src - 0x2e + 1; // -47 52927933f5SAndreas Gohr $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src - 47); 53927933f5SAndreas Gohr 54927933f5SAndreas Gohr // if ($src > 0x60 && $src < 0x77) ret += $src - 0x61 + 10 + 1; // -86 55927933f5SAndreas Gohr $ret += (((0x60 - $src) & ($src - 0x77)) >> 8) & ($src - 86); 56927933f5SAndreas Gohr 57927933f5SAndreas Gohr return $ret; 58927933f5SAndreas Gohr } 59927933f5SAndreas Gohr 60927933f5SAndreas Gohr /** 61927933f5SAndreas Gohr * Uses bitwise operators instead of table-lookups to turn 5-bit integers 62927933f5SAndreas Gohr * into 8-bit integers. 63927933f5SAndreas Gohr * 64927933f5SAndreas Gohr * @param int $src 65927933f5SAndreas Gohr * @return int 66927933f5SAndreas Gohr */ 67*8e88a29bSAndreas Gohr #[Override] 68927933f5SAndreas Gohr protected static function decode5BitsUpper(int $src): int 69927933f5SAndreas Gohr { 70927933f5SAndreas Gohr $ret = -1; 71927933f5SAndreas Gohr 72927933f5SAndreas Gohr // if ($src > 0x30 && $src < 0x3a) ret += $src - 0x2e + 1; // -47 73927933f5SAndreas Gohr $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src - 47); 74927933f5SAndreas Gohr 75927933f5SAndreas Gohr // if ($src > 0x40 && $src < 0x57) ret += $src - 0x41 + 10 + 1; // -54 76927933f5SAndreas Gohr $ret += (((0x40 - $src) & ($src - 0x57)) >> 8) & ($src - 54); 77927933f5SAndreas Gohr 78927933f5SAndreas Gohr return $ret; 79927933f5SAndreas Gohr } 80927933f5SAndreas Gohr 81927933f5SAndreas Gohr /** 82927933f5SAndreas Gohr * Uses bitwise operators instead of table-lookups to turn 8-bit integers 83927933f5SAndreas Gohr * into 5-bit integers. 84927933f5SAndreas Gohr * 85927933f5SAndreas Gohr * @param int $src 86927933f5SAndreas Gohr * @return string 87927933f5SAndreas Gohr */ 88*8e88a29bSAndreas Gohr #[Override] 89927933f5SAndreas Gohr protected static function encode5Bits(int $src): string 90927933f5SAndreas Gohr { 91927933f5SAndreas Gohr $src += 0x30; 92927933f5SAndreas Gohr 93927933f5SAndreas Gohr // if ($src > 0x39) $src += 0x61 - 0x3a; // 39 94927933f5SAndreas Gohr $src += ((0x39 - $src) >> 8) & 39; 95927933f5SAndreas Gohr 96*8e88a29bSAndreas Gohr return pack('C', $src); 97927933f5SAndreas Gohr } 98927933f5SAndreas Gohr 99927933f5SAndreas Gohr /** 100927933f5SAndreas Gohr * Uses bitwise operators instead of table-lookups to turn 8-bit integers 101927933f5SAndreas Gohr * into 5-bit integers. 102927933f5SAndreas Gohr * 103927933f5SAndreas Gohr * Uppercase variant. 104927933f5SAndreas Gohr * 105927933f5SAndreas Gohr * @param int $src 106927933f5SAndreas Gohr * @return string 107927933f5SAndreas Gohr */ 108*8e88a29bSAndreas Gohr #[Override] 109927933f5SAndreas Gohr protected static function encode5BitsUpper(int $src): string 110927933f5SAndreas Gohr { 111927933f5SAndreas Gohr $src += 0x30; 112927933f5SAndreas Gohr 113927933f5SAndreas Gohr // if ($src > 0x39) $src += 0x41 - 0x3a; // 7 114927933f5SAndreas Gohr $src += ((0x39 - $src) >> 8) & 7; 115927933f5SAndreas Gohr 116*8e88a29bSAndreas Gohr return pack('C', $src); 117927933f5SAndreas Gohr } 118927933f5SAndreas Gohr}