Debian Codegraph @

lib/3rdParty/composer/christian-riesen/base32/src/Base32.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Base32;
6
7 /**
8 * Base32 encoder and decoder.
9 *
10 * RFC 4648 compliant
11 *
12 * @see http://www.ietf.org/rfc/rfc4648.txt
13 * Some groundwork based on this class
14 * https://github.com/NTICompass/PHP-Base32
15 *
16 * @author Christian Riesen <[email protected]>
17 * @author Sam Williams <[email protected]>
18 *
19 * @see http://christianriesen.com
20 *
21 * @license MIT License see LICENSE file
22 */
23 class Base32
24 {
25 /**
26 * Alphabet for encoding and decoding base32.
27 *
28 * @var string
29 */
30 protected const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=';
31
32 protected const BASE32HEX_PATTERN = '/[^A-Z2-7]/';
33
34 /**
35 * Maps the Base32 character to its corresponding bit value.
36 */
37 protected const MAPPING = [
38 '=' => 0b00000,
39 'A' => 0b00000,
40 'B' => 0b00001,
41 'C' => 0b00010,
42 'D' => 0b00011,
43 'E' => 0b00100,
44 'F' => 0b00101,
45 'G' => 0b00110,
46 'H' => 0b00111,
47 'I' => 0b01000,
48 'J' => 0b01001,
49 'K' => 0b01010,
50 'L' => 0b01011,
51 'M' => 0b01100,
52 'N' => 0b01101,
53 'O' => 0b01110,
54 'P' => 0b01111,
55 'Q' => 0b10000,
56 'R' => 0b10001,
57 'S' => 0b10010,
58 'T' => 0b10011,
59 'U' => 0b10100,
60 'V' => 0b10101,
61 'W' => 0b10110,
62 'X' => 0b10111,
63 'Y' => 0b11000,
64 'Z' => 0b11001,
65 '2' => 0b11010,
66 '3' => 0b11011,
67 '4' => 0b11100,
68 '5' => 0b11101,
69 '6' => 0b11110,
70 '7' => 0b11111,
71 ];
72
73 /**
74 * Encodes into base32.
75 *
76 * @param string $string Clear text string
77 *
78 * @return string Base32 encoded string
79 */
80 public static function encode(string $string): string
81 {
82 // Empty string results in empty string
83 if ('' === $string) {
84 return '';
85 }
86
87 $encoded = '';
88
89 //Set the initial values
90 $n = $bitLen = $val = 0;
91 $len = \strlen($string);
92
93 //Pad the end of the string - this ensures that there are enough zeros
94 $string .= \str_repeat(\chr(0), 4);
95
96 //Explode string into integers
97 $chars = (array) \unpack('C*', $string, 0);
98
99 while ($n < $len || 0 !== $bitLen) {
100 //If the bit length has fallen below 5, shift left 8 and add the next character.
101 if ($bitLen < 5) {
102 $val = $val << 8;
103 $bitLen += 8;
104 $n++;
105 $val += $chars[$n];
106 }
107 $shift = $bitLen - 5;
108 $encoded .= ($n - (int)($bitLen > 8) > $len && 0 == $val) ? '=' : static::ALPHABET[$val >> $shift];
109 $val = $val & ((1 << $shift) - 1);
110 $bitLen -= 5;
111 }
112
113 return $encoded;
114 }
115
116 /**
117 * Decodes base32.
118 *
119 * @param string $base32String Base32 encoded string
120 *
121 * @return string Clear text string
122 */
123 public static function decode(string $base32String): string
124 {
125 // Only work in upper cases
126 $base32String = \strtoupper($base32String);
127
128 // Remove anything that is not base32 alphabet
129 $base32String = \preg_replace(static::BASE32HEX_PATTERN, '', $base32String);
130
131 // Empty string results in empty string
132 if ('' === $base32String || null === $base32String) {
133 return '';
134 }
135
136 $decoded = '';
137
138 //Set the initial values
139 $len = \strlen($base32String);
140 $n = 0;
141 $bitLen = 5;
142 $val = static::MAPPING[$base32String[0]];
143
144 while ($n < $len) {
145 //If the bit length has fallen below 8, shift left 5 and add the next pentet.
146 if ($bitLen < 8) {
147 $val = $val << 5;
148 $bitLen += 5;
149 $n++;
150 $pentet = $base32String[$n] ?? '=';
151
152 //If the new pentet is padding, make this the last iteration.
153 if ('=' === $pentet) {
154 $n = $len;
155 }
156 $val += static::MAPPING[$pentet];
157 continue;
158 }
159 $shift = $bitLen - 8;
160
161 $decoded .= \chr($val >> $shift);
162 $val = $val & ((1 << $shift) - 1);
163 $bitLen -= 8;
164 }
165
166 return $decoded;
167 }
168 }