Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base32.php
1<?php
3
6
7class Base32
8{
9
15 private static $alphabet = array(
16 'A' => 0,
17 'B' => 1,
18 'C' => 2,
19 'D' => 3,
20 'E' => 4,
21 'F' => 5,
22 'G' => 6,
23 'H' => 7,
24 'I' => 8,
25 'J' => 9,
26 'K' => 10,
27 'L' => 11,
28 'M' => 12,
29 'N' => 13,
30 'O' => 14,
31 'P' => 15,
32 'Q' => 16,
33 'R' => 17,
34 'S' => 18,
35 'T' => 19,
36 'U' => 20,
37 'V' => 21,
38 'W' => 22,
39 'X' => 23,
40 'Y' => 24,
41 'Z' => 25,
42 2 => 26,
43 3 => 27,
44 4 => 28,
45 5 => 29,
46 6 => 30,
47 7 => 31,
48 '=' => 32,
49 );
50
51 private static $encodeAlphabet = null;
52
60 public static function encode($string)
61 {
62 if (!$string)
63 return '';
64
65 if (!is_string($string))
66 throw new ArgumentTypeException('string', 'string');
67
68 if (self::$encodeAlphabet === null)
69 self::$encodeAlphabet = array_flip(self::$alphabet);
70
71 // Convert string to binary
72 $binaryString = '';
73
74 foreach (str_split($string) as $s)
75 {
76 // Return each character as an 8-bit binary string
77 $s = decbin(ord($s));
78 $binaryString .= str_pad($s, 8, 0, STR_PAD_LEFT);
79 }
80
81 // Break into 5-bit chunks, then break that into an array
82 $binaryArray = self::chunk($binaryString, 5);
83
84 // Pad array to be divisible by 8
85 while (count($binaryArray) % 8 !== 0)
86 {
87 $binaryArray[] = null;
88 }
89
90 $base32String = '';
91
92 // Encode in base32
93 foreach ($binaryArray as $bin)
94 {
95 $char = 32;
96
97 if (!is_null($bin))
98 {
99 // Pad the binary strings
100 $bin = str_pad($bin, 5, 0, STR_PAD_RIGHT);
101 $char = bindec($bin);
102 }
103
104 // Base32 character
105 $base32String .= self::$encodeAlphabet[$char];
106 }
107
108 return $base32String;
109 }
110
119 public static function decode($base32String)
120 {
121 if (!$base32String)
122 return '';
123
124 if (!is_string($base32String))
125 throw new ArgumentTypeException('base32String', 'string');
126
127 $base32String = mb_strtoupper($base32String);
128 $base32Array = str_split($base32String);
129
130 $string = '';
131
132 foreach ($base32Array as $str)
133 {
134 // skip padding
135 if ($str === '=')
136 continue;
137
138 if (!isset(self::$alphabet[$str]))
139 throw new DecodingException(sprintf('Illegal character: %s', $str));
140
141 $char = self::$alphabet[$str];
142 $char = decbin($char);
143 $string .= str_pad($char, 5, 0, STR_PAD_LEFT);
144 }
145
146 while (strlen($string) % 8 !== 0)
147 {
148 $string = substr($string, 0, -1);
149 }
150
151 $binaryArray = self::chunk($string, 8);
152
153 $realString = '';
154
155 foreach ($binaryArray as $bin)
156 {
157 // Pad each value to 8 bits
158 $bin = str_pad($bin, 8, 0, STR_PAD_RIGHT);
159 // Convert binary strings to ASCII
160 $realString .= chr(bindec($bin));
161 }
162
163 return $realString;
164 }
165
173 private static function chunk($binaryString, $bits)
174 {
175 $binaryString = chunk_split($binaryString, $bits, ' ');
176
177 if (substr($binaryString, -1) == ' ')
178 {
179 $binaryString = substr($binaryString, 0, -1);
180 }
181
182 return explode(' ', $binaryString);
183 }
184}
static decode($base32String)
Definition base32.php:119
static encode($string)
Definition base32.php:60