Hex.php 5.1 KB

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