48 public static function decode($b32) {
50 $b32 = strtoupper($b32);
52 if (!preg_match(
'/^[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]+$/', $b32, $match))
53 throw new Exception(
'Invalid characters in the base32 string.');
60 for ($i = 0; $i < $l; $i++) {
64 $n = $n + self::$lut[$b32[$i]];
70 $binary .= chr(($n & (0xFF << $j)) >> $j);
80 public static function encode($string) {
83 throw new Exception(
'Empty string.');
88 $bytes = str_split($string);
89 $length =
count( $bytes );
90 for ($i = 0; $i < $length; $i++) {
91 $bits = base_convert(ord($bytes[$i]), 10, 2);
92 $binary .= str_pad($bits, 8,
'0', STR_PAD_LEFT);
95 $map = array_keys(self::$lut);
96 $fivebits = str_split($binary, 5);
97 $length =
count( $fivebits );
98 for ($i = 0; $i < $length; $i++) {
99 $dec = base_convert(str_pad($fivebits[$i], 5,
'0'), 2, 10);