| 1 |
<?php |
| 2 |
//namespace Base32; |
| 3 |
|
| 4 |
/** |
| 5 |
* Base32 encoder and decoder |
| 6 |
* |
| 7 |
* Last update: 2012-06-20 |
| 8 |
* |
| 9 |
* RFC 4648 compliant |
| 10 |
* @link http://www.ietf.org/rfc/rfc4648.txt |
| 11 |
* |
| 12 |
* Some groundwork based on this class |
| 13 |
* https://github.com/NTICompass/PHP-Base32 |
| 14 |
* |
| 15 |
* @author Christian Riesen <chris.riesen@gmail.com> |
| 16 |
* @link http://christianriesen.com |
| 17 |
* @license MIT License see LICENSE file |
| 18 |
*/ |
| 19 |
class Base32 |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Table for encoding base32 |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
private static $encode = array( |
| 27 |
0 => 'A', |
| 28 |
1 => 'B', |
| 29 |
2 => 'C', |
| 30 |
3 => 'D', |
| 31 |
4 => 'E', |
| 32 |
5 => 'F', |
| 33 |
6 => 'G', |
| 34 |
7 => 'H', |
| 35 |
8 => 'I', |
| 36 |
9 => 'J', |
| 37 |
10 => 'K', |
| 38 |
11 => 'L', |
| 39 |
12 => 'M', |
| 40 |
13 => 'N', |
| 41 |
14 => 'O', |
| 42 |
15 => 'P', |
| 43 |
16 => 'Q', |
| 44 |
17 => 'R', |
| 45 |
18 => 'S', |
| 46 |
19 => 'T', |
| 47 |
20 => 'U', |
| 48 |
21 => 'V', |
| 49 |
22 => 'W', |
| 50 |
23 => 'X', |
| 51 |
24 => 'Y', |
| 52 |
25 => 'Z', |
| 53 |
26 => 2, |
| 54 |
27 => 3, |
| 55 |
28 => 4, |
| 56 |
29 => 5, |
| 57 |
30 => 6, |
| 58 |
31 => 7, |
| 59 |
32 => '=', |
| 60 |
); |
| 61 |
|
| 62 |
/** |
| 63 |
* Table for decoding base32 |
| 64 |
* |
| 65 |
* @var array |
| 66 |
*/ |
| 67 |
private static $decode = array( |
| 68 |
'A' => 0, |
| 69 |
'B' => 1, |
| 70 |
'C' => 2, |
| 71 |
'D' => 3, |
| 72 |
'E' => 4, |
| 73 |
'F' => 5, |
| 74 |
'G' => 6, |
| 75 |
'H' => 7, |
| 76 |
'I' => 8, |
| 77 |
'J' => 9, |
| 78 |
'K' => 10, |
| 79 |
'L' => 11, |
| 80 |
'M' => 12, |
| 81 |
'N' => 13, |
| 82 |
'O' => 14, |
| 83 |
'P' => 15, |
| 84 |
'Q' => 16, |
| 85 |
'R' => 17, |
| 86 |
'S' => 18, |
| 87 |
'T' => 19, |
| 88 |
'U' => 20, |
| 89 |
'V' => 21, |
| 90 |
'W' => 22, |
| 91 |
'X' => 23, |
| 92 |
'Y' => 24, |
| 93 |
'Z' => 25, |
| 94 |
2 => 26, |
| 95 |
3 => 27, |
| 96 |
4 => 28, |
| 97 |
5 => 29, |
| 98 |
6 => 30, |
| 99 |
7 => 31, |
| 100 |
'=' => 32, |
| 101 |
); |
| 102 |
|
| 103 |
/** |
| 104 |
* Creates an array from a binary string into a given chunk size |
| 105 |
* |
| 106 |
* @param string $binaryString String to chunk |
| 107 |
* @param integer $bits Number of bits per chunk |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
private static function chunk($binaryString, $bits) |
| 111 |
{ |
| 112 |
$binaryString = chunk_split($binaryString, $bits, ' '); |
| 113 |
|
| 114 |
if (substr($binaryString, (strlen($binaryString)) - 1) == ' ') { |
| 115 |
$binaryString = substr($binaryString, 0, strlen($binaryString)-1); |
| 116 |
} |
| 117 |
|
| 118 |
return explode(' ', $binaryString); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Encodes into base32 |
| 123 |
* |
| 124 |
* @param string $string Clear text string |
| 125 |
* @return string Base32 encoded string |
| 126 |
*/ |
| 127 |
public static function encode($string) |
| 128 |
{ |
| 129 |
if (strlen($string) == 0) { |
| 130 |
// Gives an empty string |
| 131 |
return ''; |
| 132 |
} |
| 133 |
|
| 134 |
// Convert string to binary |
| 135 |
$binaryString = ''; |
| 136 |
|
| 137 |
foreach (str_split($string) as $s) { |
| 138 |
// Return each character as an 8-bit binary string |
| 139 |
$s = decbin(ord($s)); |
| 140 |
$binaryString .= str_pad($s, 8, 0, STR_PAD_LEFT); |
| 141 |
} |
| 142 |
|
| 143 |
// Break into 5-bit chunks, then break that into an array |
| 144 |
$binaryArray = self::chunk($binaryString, 5); |
| 145 |
|
| 146 |
// Pad array to be divisible by 8 |
| 147 |
while (count($binaryArray) % 8 !== 0) { |
| 148 |
$binaryArray[] = null; |
| 149 |
} |
| 150 |
|
| 151 |
$base32String = ''; |
| 152 |
|
| 153 |
// Encode in base32 |
| 154 |
foreach ($binaryArray as $bin) { |
| 155 |
$char = 32; |
| 156 |
|
| 157 |
if (!is_null($bin)) { |
| 158 |
// Pad the binary strings |
| 159 |
$bin = str_pad($bin, 5, 0, STR_PAD_RIGHT); |
| 160 |
$char = bindec($bin); |
| 161 |
} |
| 162 |
|
| 163 |
// Base32 character |
| 164 |
$base32String .= self::$encode[$char]; |
| 165 |
} |
| 166 |
|
| 167 |
return $base32String; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Decodes base32 |
| 172 |
* |
| 173 |
* @param string $base32String Base32 encoded string |
| 174 |
* @return string Clear text string |
| 175 |
*/ |
| 176 |
public static function decode($base32String) |
| 177 |
{ |
| 178 |
if (strlen($base32String) == 0) { |
| 179 |
// Gives an empty string |
| 180 |
return ''; |
| 181 |
} |
| 182 |
|
| 183 |
// Only work in upper cases |
| 184 |
$base32String = strtoupper($base32String); |
| 185 |
|
| 186 |
// Remove anything that is not base32 alphabet |
| 187 |
$pattern = '/[^A-Z2-7]/'; |
| 188 |
$replacement = ''; |
| 189 |
|
| 190 |
$base32String = preg_replace($pattern, '', $base32String); |
| 191 |
|
| 192 |
$base32Array = str_split($base32String); |
| 193 |
|
| 194 |
$binaryArray = array(); |
| 195 |
$string = ''; |
| 196 |
|
| 197 |
foreach ($base32Array as $str) { |
| 198 |
$char = self::$decode[$str]; |
| 199 |
|
| 200 |
// Ignore the padding character |
| 201 |
if ($char !== 32) { |
| 202 |
$char = decbin($char); |
| 203 |
$string .= str_pad($char, 5, 0, STR_PAD_LEFT); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
while (strlen($string) %8 !== 0) { |
| 208 |
$string = substr($string, 0, strlen($string)-1); |
| 209 |
} |
| 210 |
|
| 211 |
$binaryArray = self::chunk($string, 8); |
| 212 |
|
| 213 |
$realString = ''; |
| 214 |
|
| 215 |
foreach ($binaryArray as $bin) { |
| 216 |
// Pad each value to 8 bits |
| 217 |
$bin = str_pad($bin, 8, 0, STR_PAD_RIGHT); |
| 218 |
// Convert binary strings to ascii |
| 219 |
$realString .= chr(bindec($bin)); |
| 220 |
} |
| 221 |
|
| 222 |
return $realString; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|