base32.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Encode in Base32 based on RFC 4648.
  4. * Requires 20% more space than base64
  5. * Great for case-insensitive filesystems like Windows and URL's (except for = char which can be excluded using the pad option for urls)
  6. *
  7. * @package default
  8. * @author Bryan Ruiz
  9. **/
  10. class Base32 {
  11. private static $map = array(
  12. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 7
  13. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15
  14. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 23
  15. 'Y', 'Z', '2', '3', '4', '5', '6', '7', // 31
  16. '=' // padding char
  17. );
  18. private static $flippedMap = array(
  19. 'A'=>'0', 'B'=>'1', 'C'=>'2', 'D'=>'3', 'E'=>'4', 'F'=>'5', 'G'=>'6', 'H'=>'7',
  20. 'I'=>'8', 'J'=>'9', 'K'=>'10', 'L'=>'11', 'M'=>'12', 'N'=>'13', 'O'=>'14', 'P'=>'15',
  21. 'Q'=>'16', 'R'=>'17', 'S'=>'18', 'T'=>'19', 'U'=>'20', 'V'=>'21', 'W'=>'22', 'X'=>'23',
  22. 'Y'=>'24', 'Z'=>'25', '2'=>'26', '3'=>'27', '4'=>'28', '5'=>'29', '6'=>'30', '7'=>'31'
  23. );
  24. /**
  25. * Use padding false when encoding for urls
  26. *
  27. * @return base32 encoded string
  28. * @author Bryan Ruiz
  29. **/
  30. public static function encode($input, $padding = true) {
  31. if(empty($input)) return "";
  32. $input = str_split($input);
  33. $binaryString = "";
  34. for($i = 0; $i < count($input); $i++) {
  35. $binaryString .= str_pad(base_convert(ord($input[$i]), 10, 2), 8, '0', STR_PAD_LEFT);
  36. }
  37. $fiveBitBinaryArray = str_split($binaryString, 5);
  38. $base32 = "";
  39. $i=0;
  40. while($i < count($fiveBitBinaryArray)) {
  41. $base32 .= self::$map[base_convert(str_pad($fiveBitBinaryArray[$i], 5,'0'), 2, 10)];
  42. $i++;
  43. }
  44. if($padding && ($x = strlen($binaryString) % 40) != 0) {
  45. if($x == 8) $base32 .= str_repeat(self::$map[32], 6);
  46. else if($x == 16) $base32 .= str_repeat(self::$map[32], 4);
  47. else if($x == 24) $base32 .= str_repeat(self::$map[32], 3);
  48. else if($x == 32) $base32 .= self::$map[32];
  49. }
  50. return $base32;
  51. }
  52. public static function decode($input) {
  53. if(empty($input)) return;
  54. $paddingCharCount = substr_count($input, self::$map[32]);
  55. $allowedValues = array(6,4,3,1,0);
  56. if(!in_array($paddingCharCount, $allowedValues)) return false;
  57. for($i=0; $i<4; $i++){
  58. if($paddingCharCount == $allowedValues[$i] &&
  59. substr($input, -($allowedValues[$i])) != str_repeat(self::$map[32], $allowedValues[$i])) return false;
  60. }
  61. $input = str_replace('=','', $input);
  62. $input = str_split($input);
  63. $binaryString = "";
  64. for($i=0; $i < count($input); $i = $i+8) {
  65. $x = "";
  66. if(!in_array($input[$i], self::$map)) return false;
  67. for($j=0; $j < 8; $j++) {
  68. $x .= str_pad(base_convert(@self::$flippedMap[@$input[$i + $j]], 10, 2), 5, '0', STR_PAD_LEFT);
  69. }
  70. $eightBits = str_split($x, 8);
  71. for($z = 0; $z < count($eightBits); $z++) {
  72. $binaryString .= ( ($y = chr(base_convert($eightBits[$z], 2, 10))) || ord($y) == 48 ) ? $y:"";
  73. }
  74. }
  75. return $binaryString;
  76. }
  77. }