4 * NTICompass' crappy base32 library for PHP
6 * http://labs.nticompassinc.com
10 var $encode, $decode, $type;
12 // Supports RFC 4648 (default) or Crockford (http://www.crockford.com/wrmg/base32.html)
13 function __construct($alphabet = 'rfc4648')
15 $alphabet = strtolower($alphabet);
16 $this->type = $alphabet;
17 // Crockford's alphabet removes I,L,O, and U
18 $crockfordABC = range('A', 'Z');
19 unset($crockfordABC[8], $crockfordABC[11], $crockfordABC[14], $crockfordABC[20]);
20 $crockfordABC = array_values($crockfordABC);
23 'rfc4648' => array_merge(range('A', 'Z'), range(2, 7), array('=')),
24 'crockford' => array_merge(range(0, 9), $crockfordABC, array('='))
26 $this->encode = $alphabets[$alphabet];
27 $this->decode = array_flip($this->encode);
28 // Add extra letters for Crockford's alphabet
29 if ($alphabet === 'crockford') {
30 $this->decode['O'] = 0;
31 $this->decode['I'] = 1;
32 $this->decode['L'] = 1;
36 private function bin_chunk($binaryString, $bits)
38 $binaryString = chunk_split($binaryString, $bits, ' ');
39 if ($this->endsWith($binaryString, ' ')) {
40 $binaryString = substr($binaryString, 0, strlen($binaryString) - 1);
42 return explode(' ', $binaryString);
45 // String <-> Binary conversion
46 // Based off: http://psoug.org/snippet/PHP-Binary-to-Text-Text-to-Binary_380.htm
48 private function bin2str($binaryString)
50 // Make sure binary string is in 8-bit chunks
51 $binaryArray = $this->bin_chunk($binaryString, 8);
53 foreach ($binaryArray as $bin) {
54 // Pad each value to 8 bits
55 $bin = str_pad($bin, 8, 0, STR_PAD_RIGHT);
56 // Convert binary strings to ascii
57 $string .= chr(bindec($bin));
62 private function str2bin($input)
65 foreach (str_split($input) as $s) {
66 // Return each character as an 8-bit binary string
68 $bin .= str_pad($s, 8, 0, STR_PAD_LEFT);
73 // starts/endsWith from:
74 // http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions/834355#834355
76 private function startsWith($haystack, $needle)
78 $length = strlen($needle);
79 return substr($haystack, 0, $length) === $needle;
82 private function endsWith($haystack, $needle)
84 $length = strlen($needle);
85 return substr($haystack, -$length) === $needle;
88 // base32 info from: http://www.garykessler.net/library/base64.html
90 public function base32_encode($string)
92 // Convert string to binary
93 $binaryString = $this->str2bin($string);
95 // Break into 5-bit chunks, then break that into an array
96 $binaryArray = $this->bin_chunk($binaryString, 5);
98 // Pad array to be divisible by 8
99 while (count($binaryArray) % 8 !== 0) {
100 $binaryArray[] = null;
106 foreach ($binaryArray as $bin) {
108 if (!is_null($bin)) {
109 // Pad the binary strings
110 $bin = str_pad($bin, 5, 0, STR_PAD_RIGHT);
111 $char = bindec($bin);
114 $base32String .= $this->encode[$char];
117 return $base32String;
121 public function base32_decode($base32String)
123 $base32Array = str_split(str_replace('-', '', strtoupper($base32String)));
124 $binaryArray = array();
126 foreach ($base32Array as $str) {
127 $char = $this->decode[$str];
129 $char = decbin($char);
130 $string .= str_pad($char, 5, 0, STR_PAD_LEFT);
133 while (strlen($string) % 8 !== 0) {
134 $string = substr($string, 0, strlen($string) - 1);
136 return $this->bin2str($string);