Bitrix-D7  20.0.0
base32.php
См. документацию.
1 <?php
2 namespace Bitrix\Main\Text;
3 
6 
7 class Base32
8 {
9 
10  /**
11  * Table for encoding/decoding base32, RFC 4648/3548
12  *
13  * @var array $alphabet
14  */
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 
53  /**
54  * Encode data to Base32 string
55  *
56  * @param string $string Data to encode.
57  * @return string Base32 encoded data.
58  * @throws ArgumentTypeException
59  */
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 
111  /**
112  * Decode Base32 encoded string
113  *
114  * @param string $base32String Base32 encoded string.
115  * @throws ArgumentTypeException
116  * @throws DecodingException
117  * @return string Original data.
118  */
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 = 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 (\CUtil::binStrlen($string) % 8 !== 0)
147  {
148  $string = \CUtil::binSubstr($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 
166  /**
167  * Split binary string to chunks
168  *
169  * @param string $binaryString The string to be chunked.
170  * @param int $bits The chunk length.
171  * @return array
172  */
173  private static function chunk($binaryString, $bits)
174  {
175  $binaryString = chunk_split($binaryString, $bits, ' ');
176 
177  if (\CUtil::binSubstr($binaryString, -1) == ' ')
178  {
179  $binaryString = \CUtil::binSubstr($binaryString, 0, -1);
180  }
181 
182  return explode(' ', $binaryString);
183  }
184 }
Bitrix\Main\DecodingException
Definition: main/lib/exception.php:221
Bitrix\Main\Text\Base32\encode
static encode($string)
Encode data to Base32 string.
Definition: base32.php:60
Bitrix\Main\Text
Definition: base32.php:2
Bitrix\Main\ArgumentTypeException
Exception is thrown when the type of an argument is not accepted by function.
Definition: main/lib/exception.php:113
Bitrix\Main\Text\Base32\decode
static decode($base32String)
Decode Base32 encoded string.
Definition: base32.php:119
Bitrix\Main\Text\Base32
Definition: base32.php:7