Hex.php 4.1 KB

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