Base32Hex.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace ParagonIE\ConstantTime;
  3. /**
  4. * Copyright (c) 2016 - 2017 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 Base32Hex
  27. * [0-9][A-V]
  28. *
  29. * @package ParagonIE\ConstantTime
  30. */
  31. abstract class Base32Hex extends Base32
  32. {
  33. /**
  34. * Uses bitwise operators instead of table-lookups to turn 5-bit integers
  35. * into 8-bit integers.
  36. *
  37. * @param int $src
  38. * @return int
  39. */
  40. protected static function decode5Bits($src)
  41. {
  42. $ret = -1;
  43. // if ($src > 0x30 && $src < 0x3a) ret += $src - 0x2e + 1; // -47
  44. $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src - 47);
  45. // if ($src > 0x60 && $src < 0x77) ret += $src - 0x61 + 10 + 1; // -86
  46. $ret += (((0x60 - $src) & ($src - 0x77)) >> 8) & ($src - 86);
  47. return $ret;
  48. }
  49. /**
  50. * Uses bitwise operators instead of table-lookups to turn 5-bit integers
  51. * into 8-bit integers.
  52. *
  53. * @param int $src
  54. * @return int
  55. */
  56. protected static function decode5BitsUpper($src)
  57. {
  58. $ret = -1;
  59. // if ($src > 0x30 && $src < 0x3a) ret += $src - 0x2e + 1; // -47
  60. $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src - 47);
  61. // if ($src > 0x40 && $src < 0x57) ret += $src - 0x41 + 10 + 1; // -54
  62. $ret += (((0x40 - $src) & ($src - 0x57)) >> 8) & ($src - 54);
  63. return $ret;
  64. }
  65. /**
  66. * Uses bitwise operators instead of table-lookups to turn 8-bit integers
  67. * into 5-bit integers.
  68. *
  69. * @param int $src
  70. * @return string
  71. */
  72. protected static function encode5Bits($src)
  73. {
  74. $src += 0x30;
  75. // if ($src > 0x39) $src += 0x61 - 0x3a; // 39
  76. $src += ((0x39 - $src) >> 8) & 39;
  77. return \pack('C', $src);
  78. }
  79. /**
  80. * Uses bitwise operators instead of table-lookups to turn 8-bit integers
  81. * into 5-bit integers.
  82. *
  83. * Uppercase variant.
  84. *
  85. * @param int $src
  86. * @return string
  87. */
  88. protected static function encode5BitsUpper($src)
  89. {
  90. $src += 0x30;
  91. // if ($src > 0x39) $src += 0x41 - 0x3a; // 7
  92. $src += ((0x39 - $src) >> 8) & 7;
  93. return \pack('C', $src);
  94. }
  95. }