60 public static function encode($string)
65 if (!is_string($string))
68 if (self::$encodeAlphabet ===
null)
69 self::$encodeAlphabet = array_flip(self::$alphabet);
74 foreach (str_split($string) as $s)
78 $binaryString .= str_pad($s, 8, 0, STR_PAD_LEFT);
82 $binaryArray = self::chunk($binaryString, 5);
85 while (count($binaryArray) % 8 !== 0)
87 $binaryArray[] =
null;
93 foreach ($binaryArray as $bin)
100 $bin = str_pad($bin, 5, 0, STR_PAD_RIGHT);
101 $char = bindec($bin);
105 $base32String .= self::$encodeAlphabet[$char];
108 return $base32String;
119 public static function decode($base32String)
124 if (!is_string($base32String))
127 $base32String = mb_strtoupper($base32String);
128 $base32Array = str_split($base32String);
132 foreach ($base32Array as $str)
138 if (!isset(self::$alphabet[$str]))
141 $char = self::$alphabet[$str];
142 $char = decbin($char);
143 $string .= str_pad($char, 5, 0, STR_PAD_LEFT);
146 while (strlen($string) % 8 !== 0)
148 $string = substr($string, 0, -1);
151 $binaryArray = self::chunk($string, 8);
155 foreach ($binaryArray as $bin)
158 $bin = str_pad($bin, 8, 0, STR_PAD_RIGHT);
160 $realString .= chr(bindec($bin));