Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Encode in Base32 based on RFC 4648.
  5. * Requires 20% more space than base64
  6. * Great for case-insensitive filesystems like Windows and URL's (except for = char which can be excluded using the pad option for urls)
  7. *
  8. * @author Bryan Ruiz
  9. **/
  10. class Base32
  11. {
  12.  
  13. protected static $map = array(
  14. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 7
  15. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15
  16. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 23
  17. 'Y', 'Z', '2', '3', '4', '5', '6', '7', // 31
  18. '=' // padding char
  19. );
  20.  
  21. protected static $flippedMap = array(
  22. 'A' => '0', 'B' => '1', 'C' => '2', 'D' => '3', 'E' => '4', 'F' => '5', 'G' => '6', 'H' => '7',
  23. 'I' => '8', 'J' => '9', 'K' => '10', 'L' => '11', 'M' => '12', 'N' => '13', 'O' => '14', 'P' => '15',
  24. 'Q' => '16', 'R' => '17', 'S' => '18', 'T' => '19', 'U' => '20', 'V' => '21', 'W' => '22', 'X' => '23',
  25. 'Y' => '24', 'Z' => '25', '2' => '26', '3' => '27', '4' => '28', '5' => '29', '6' => '30', '7' => '31'
  26. );
  27.  
  28. /**
  29. * Use padding false when encoding for urls
  30. *
  31. * @return base32 encoded string
  32. * @author Bryan Ruiz
  33. **/
  34. public static function encode($input, $padding = true)
  35. {
  36. if (empty($input)) {
  37. return "";
  38. }
  39. $input = str_split($input);
  40. $binaryString = "";
  41. for ($i = 0; $i < count($input); $i++) {
  42. $binaryString .= str_pad(base_convert(ord($input[$i]), 10, 2), 8, '0', STR_PAD_LEFT);
  43. }
  44. $fiveBitBinaryArray = str_split($binaryString, 5);
  45. $base32 = "";
  46. $i = 0;
  47. while ($i < count($fiveBitBinaryArray)) {
  48. $base32 .= self::$map[base_convert(str_pad($fiveBitBinaryArray[$i], 5, '0'), 2, 10)];
  49. $i++;
  50. }
  51. if ($padding && ($x = strlen($binaryString) % 40) != 0) {
  52. if ($x == 8) {
  53. $base32 .= str_repeat(self::$map[32], 6);
  54. } else {
  55. if ($x == 16) {
  56. $base32 .= str_repeat(self::$map[32], 4);
  57. } else {
  58. if ($x == 24) {
  59. $base32 .= str_repeat(self::$map[32], 3);
  60. } else {
  61. if ($x == 32) {
  62. $base32 .= self::$map[32];
  63. }
  64. }
  65. }
  66. }
  67. }
  68. return $base32;
  69. }
  70.  
  71. public static function decode($input)
  72. {
  73. if (empty($input)) {
  74. return;
  75. }
  76. $paddingCharCount = substr_count($input, self::$map[32]);
  77. $allowedValues = array(6, 4, 3, 1, 0);
  78. if (!in_array($paddingCharCount, $allowedValues)) {
  79. return false;
  80. }
  81. for ($i = 0; $i < 4; $i++) {
  82. if ($paddingCharCount == $allowedValues[$i] &&
  83. substr($input, -($allowedValues[$i])) != str_repeat(self::$map[32], $allowedValues[$i])
  84. ) {
  85. return false;
  86. }
  87. }
  88. $input = str_replace('=', '', $input);
  89. $input = str_split($input);
  90. $binaryString = "";
  91. for ($i = 0; $i < count($input); $i = $i + 8) {
  92. $x = "";
  93. if (!in_array($input[$i], self::$map)) {
  94. return false;
  95. }
  96. for ($j = 0; $j < 8; $j++) {
  97. $x .= str_pad(base_convert(@self::$flippedMap[@$input[$i + $j]], 10, 2), 5, '0', STR_PAD_LEFT);
  98. }
  99. $eightBits = str_split($x, 8);
  100. for ($z = 0; $z < count($eightBits); $z++) {
  101. $binaryString .= (($y = chr(base_convert($eightBits[$z], 2, 10))) || ord($y) == 48) ? $y : "";
  102. }
  103. }
  104. return $binaryString;
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement