Base32.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @package Grav\Common\Helpers
  4. *
  5. * @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Helpers;
  9. use function chr;
  10. use function count;
  11. use function ord;
  12. use function strlen;
  13. /**
  14. * Class Base32
  15. * @package Grav\Common\Helpers
  16. */
  17. class Base32
  18. {
  19. /** @var string */
  20. protected static $base32Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
  21. /** @var array */
  22. protected static $base32Lookup = [
  23. 0xFF,0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, // '0', '1', '2', '3', '4', '5', '6', '7'
  24. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // '8', '9', ':', ';', '<', '=', '>', '?'
  25. 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, // '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G'
  26. 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, // 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'
  27. 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, // 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W'
  28. 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF, // 'X', 'Y', 'Z', '[', '\', ']', '^', '_'
  29. 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, // '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g'
  30. 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, // 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'
  31. 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, // 'p', 'q', 'r', 's', 't', 'u', 'v', 'w'
  32. 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF // 'x', 'y', 'z', '{', '|', '}', '~', 'DEL'
  33. ];
  34. /**
  35. * Encode in Base32
  36. *
  37. * @param string $bytes
  38. * @return string
  39. */
  40. public static function encode($bytes)
  41. {
  42. $i = 0;
  43. $index = 0;
  44. $base32 = '';
  45. $bytesLen = strlen($bytes);
  46. while ($i < $bytesLen) {
  47. $currByte = ord($bytes[$i]);
  48. /* Is the current digit going to span a byte boundary? */
  49. if ($index > 3) {
  50. if (($i + 1) < $bytesLen) {
  51. $nextByte = ord($bytes[$i+1]);
  52. } else {
  53. $nextByte = 0;
  54. }
  55. $digit = $currByte & (0xFF >> $index);
  56. $index = ($index + 5) % 8;
  57. $digit <<= $index;
  58. $digit |= $nextByte >> (8 - $index);
  59. $i++;
  60. } else {
  61. $digit = ($currByte >> (8 - ($index + 5))) & 0x1F;
  62. $index = ($index + 5) % 8;
  63. if ($index === 0) {
  64. $i++;
  65. }
  66. }
  67. $base32 .= self::$base32Chars[$digit];
  68. }
  69. return $base32;
  70. }
  71. /**
  72. * Decode in Base32
  73. *
  74. * @param string $base32
  75. * @return string
  76. */
  77. public static function decode($base32)
  78. {
  79. $bytes = [];
  80. $base32Len = strlen($base32);
  81. $base32LookupLen = count(self::$base32Lookup);
  82. for ($i = $base32Len * 5 / 8 - 1; $i >= 0; --$i) {
  83. $bytes[] = 0;
  84. }
  85. for ($i = 0, $index = 0, $offset = 0; $i < $base32Len; $i++) {
  86. $lookup = ord($base32[$i]) - ord('0');
  87. /* Skip chars outside the lookup table */
  88. if ($lookup < 0 || $lookup >= $base32LookupLen) {
  89. continue;
  90. }
  91. $digit = self::$base32Lookup[$lookup];
  92. /* If this digit is not in the table, ignore it */
  93. if ($digit === 0xFF) {
  94. continue;
  95. }
  96. if ($index <= 3) {
  97. $index = ($index + 5) % 8;
  98. if ($index === 0) {
  99. $bytes[$offset] |= $digit;
  100. $offset++;
  101. if ($offset >= count($bytes)) {
  102. break;
  103. }
  104. } else {
  105. $bytes[$offset] |= $digit << (8 - $index);
  106. }
  107. } else {
  108. $index = ($index + 5) % 8;
  109. $bytes[$offset] |= ($digit >> $index);
  110. $offset++;
  111. if ($offset >= count($bytes)) {
  112. break;
  113. }
  114. $bytes[$offset] |= $digit << (8 - $index);
  115. }
  116. }
  117. $bites = '';
  118. foreach ($bytes as $byte) {
  119. $bites .= chr($byte);
  120. }
  121. return $bites;
  122. }
  123. }