Base32.php 4.0 KB

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