Base32.php
7 months ago
Base32Hex.php
7 months ago
Base64.php
7 months ago
Base64DotSlash.php
7 months ago
Base64DotSlashOrdered.php
7 months ago
Base64UrlSafe.php
7 months ago
Binary.php
7 months ago
EncoderInterface.php
7 months ago
Encoding.php
7 months ago
Hex.php
7 months ago
RFC4648.php
7 months ago
Hex.php
155 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace AmeliaVendor\ParagonIE\ConstantTime; |
| 5 | |
| 6 | use RangeException; |
| 7 | use SensitiveParameter; |
| 8 | use SodiumException; |
| 9 | use TypeError; |
| 10 | use function extension_loaded; |
| 11 | use function pack; |
| 12 | use function sodium_bin2hex; |
| 13 | use function sodium_hex2bin; |
| 14 | use function unpack; |
| 15 | /** |
| 16 | * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises. |
| 17 | * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) |
| 18 | * |
| 19 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 20 | * of this software and associated documentation files (the "Software"), to deal |
| 21 | * in the Software without restriction, including without limitation the rights |
| 22 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 23 | * copies of the Software, and to permit persons to whom the Software is |
| 24 | * furnished to do so, subject to the following conditions: |
| 25 | * |
| 26 | * The above copyright notice and this permission notice shall be included in all |
| 27 | * copies or substantial portions of the Software. |
| 28 | * |
| 29 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 30 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 31 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 32 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 33 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 34 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 35 | * SOFTWARE. |
| 36 | */ |
| 37 | /** |
| 38 | * Class Hex |
| 39 | * @package \ParagonIE\ConstantTime |
| 40 | */ |
| 41 | abstract class Hex implements EncoderInterface |
| 42 | { |
| 43 | /** |
| 44 | * Convert a binary string into a hexadecimal string without cache-timing |
| 45 | * leaks |
| 46 | * |
| 47 | * @param string $binString (raw binary) |
| 48 | * @return string |
| 49 | * @throws TypeError |
| 50 | */ |
| 51 | public static function encode( |
| 52 | #[SensitiveParameter] |
| 53 | string $binString |
| 54 | ): string |
| 55 | { |
| 56 | if (extension_loaded('sodium')) { |
| 57 | try { |
| 58 | return sodium_bin2hex($binString); |
| 59 | } catch (SodiumException $ex) { |
| 60 | throw new RangeException($ex->getMessage(), $ex->getCode(), $ex); |
| 61 | } |
| 62 | } |
| 63 | $hex = ''; |
| 64 | $len = Binary::safeStrlen($binString); |
| 65 | for ($i = 0; $i < $len; ++$i) { |
| 66 | /** @var array<int, int> $chunk */ |
| 67 | $chunk = unpack('C', $binString[$i]); |
| 68 | $c = $chunk[1] & 0xf; |
| 69 | $b = $chunk[1] >> 4; |
| 70 | $hex .= pack('CC', 87 + $b + ($b - 10 >> 8 & ~38), 87 + $c + ($c - 10 >> 8 & ~38)); |
| 71 | } |
| 72 | return $hex; |
| 73 | } |
| 74 | /** |
| 75 | * Convert a binary string into a hexadecimal string without cache-timing |
| 76 | * leaks, returning uppercase letters (as per RFC 4648) |
| 77 | * |
| 78 | * @param string $binString (raw binary) |
| 79 | * @return string |
| 80 | * @throws TypeError |
| 81 | */ |
| 82 | public static function encodeUpper( |
| 83 | #[SensitiveParameter] |
| 84 | string $binString |
| 85 | ): string |
| 86 | { |
| 87 | $hex = ''; |
| 88 | $len = Binary::safeStrlen($binString); |
| 89 | for ($i = 0; $i < $len; ++$i) { |
| 90 | /** @var array<int, int> $chunk */ |
| 91 | $chunk = unpack('C', $binString[$i]); |
| 92 | $c = $chunk[1] & 0xf; |
| 93 | $b = $chunk[1] >> 4; |
| 94 | $hex .= pack('CC', 55 + $b + ($b - 10 >> 8 & ~6), 55 + $c + ($c - 10 >> 8 & ~6)); |
| 95 | } |
| 96 | return $hex; |
| 97 | } |
| 98 | /** |
| 99 | * Convert a hexadecimal string into a binary string without cache-timing |
| 100 | * leaks |
| 101 | * |
| 102 | * @param string $encodedString |
| 103 | * @param bool $strictPadding |
| 104 | * @return string (raw binary) |
| 105 | * @throws RangeException |
| 106 | */ |
| 107 | public static function decode( |
| 108 | #[SensitiveParameter] |
| 109 | string $encodedString, |
| 110 | bool $strictPadding = false |
| 111 | ): string |
| 112 | { |
| 113 | if (extension_loaded('sodium') && $strictPadding) { |
| 114 | try { |
| 115 | return sodium_hex2bin($encodedString); |
| 116 | } catch (SodiumException $ex) { |
| 117 | throw new RangeException($ex->getMessage(), $ex->getCode(), $ex); |
| 118 | } |
| 119 | } |
| 120 | $hex_pos = 0; |
| 121 | $bin = ''; |
| 122 | $c_acc = 0; |
| 123 | $hex_len = Binary::safeStrlen($encodedString); |
| 124 | $state = 0; |
| 125 | if (($hex_len & 1) !== 0) { |
| 126 | if ($strictPadding) { |
| 127 | throw new RangeException('Expected an even number of hexadecimal characters'); |
| 128 | } else { |
| 129 | $encodedString = '0' . $encodedString; |
| 130 | ++$hex_len; |
| 131 | } |
| 132 | } |
| 133 | /** @var array<int, int> $chunk */ |
| 134 | $chunk = unpack('C*', $encodedString); |
| 135 | while ($hex_pos < $hex_len) { |
| 136 | ++$hex_pos; |
| 137 | $c = $chunk[$hex_pos]; |
| 138 | $c_num = $c ^ 48; |
| 139 | $c_num0 = $c_num - 10 >> 8; |
| 140 | $c_alpha = ($c & ~32) - 55; |
| 141 | $c_alpha0 = ($c_alpha - 10 ^ $c_alpha - 16) >> 8; |
| 142 | if (($c_num0 | $c_alpha0) === 0) { |
| 143 | throw new RangeException('Expected hexadecimal character'); |
| 144 | } |
| 145 | $c_val = $c_num0 & $c_num | $c_alpha & $c_alpha0; |
| 146 | if ($state === 0) { |
| 147 | $c_acc = $c_val * 16; |
| 148 | } else { |
| 149 | $bin .= pack('C', $c_acc | $c_val); |
| 150 | } |
| 151 | $state ^= 1; |
| 152 | } |
| 153 | return $bin; |
| 154 | } |
| 155 | } |