-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base32.php
213 lines (180 loc) · 6.33 KB
/
Base32.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
declare(strict_types=1);
namespace Bakame\Aide\Base32;
use RuntimeException;
use ValueError;
use function array_key_exists;
use function chr;
use function rtrim;
use function str_replace;
use function str_split;
use function strcspn;
use function strlen;
use function strpos;
use function strtoupper;
use function substr;
use function unpack;
final class Base32
{
private const ALPHABET_SIZE = 32;
private const RESERVED_CHARACTERS = "\r\t\n ";
/** @var non-empty-string */
private readonly string $alphabet;
/** @var non-empty-string */
private readonly string $padding;
/**
* @param non-empty-string $alphabet
* @param non-empty-string $padding
*/
private function __construct(string $alphabet, string $padding)
{
if (1 !== strlen($padding) || false !== strpos(self::RESERVED_CHARACTERS, $padding)) {
throw new ValueError('The padding character must be a non-reserved single byte character.');
}
if (self::ALPHABET_SIZE !== strlen($alphabet)) {
throw new ValueError('The alphabet must be a '.self::ALPHABET_SIZE.' bytes long string.');
}
$upperAlphabet = strtoupper($alphabet);
$upperPadding = strtoupper($padding);
if (self::ALPHABET_SIZE !== strcspn($upperAlphabet, self::RESERVED_CHARACTERS.$upperPadding)) {
throw new ValueError('The alphabet can not contain a reserved character.');
}
$uniqueChars = [];
for ($index = 0; $index < self::ALPHABET_SIZE; $index++) {
$char = $upperAlphabet[$index];
if (array_key_exists($char, $uniqueChars)) {
throw new ValueError('The alphabet must only contain unique characters.');
}
$uniqueChars[$char] = 1;
}
$this->alphabet = $alphabet;
$this->padding = $padding;
}
/**
* @param non-empty-string $alphabet
* @param non-empty-string $padding
*/
public static function new(string $alphabet, string $padding): self
{
return new self($alphabet, $padding);
}
public function encode(string $decoded): string
{
if ('' === $decoded) {
return '';
}
$offset = 0;
$bitLen = 0;
$val = 0;
$length = strlen($decoded);
$decoded .= chr(0).chr(0).chr(0).chr(0);
$chars = (array) unpack('C*', $decoded);
$encoded = '';
while ($offset < $length || 0 !== $bitLen) {
if ($bitLen < 5) {
$bitLen += 8;
$offset++;
$val = ($val << 8) + $chars[$offset];
}
$shift = $bitLen - 5;
$encoded .= ($offset - ($bitLen > 8 ? 1 : 0) > $length && 0 === $val) ? $this->padding : $this->alphabet[$val >> $shift];
$val &= ((1 << $shift) - 1);
$bitLen -= 5;
}
return $encoded;
}
public function decode(string $encoded, bool $strict = false): string
{
[$encoded, $alphabet, $padding] = $this->prepareDecoding($encoded, $strict);
if ('' === $encoded) {
return '';
}
$chars = [];
for ($index = 0; $index < self::ALPHABET_SIZE; $index++) {
$chars[$alphabet[$index]] = $index;
}
$chars[$padding] = 0;
$offset = 0;
$bitLen = 5;
$length = strlen($encoded);
$decoded = '';
do {
if (!isset($val)) {
$index = $encoded[$offset];
$val = array_key_exists($index, $chars) ? $chars[$index] : -1;
}
if (-1 === $val) {
if ($strict) {
throw new RuntimeException('The encoded data contains characters unknown to the alphabet.');
}
$offset++;
if ($offset < $length) {
$val = null;
continue;
}
break;
}
if ($bitLen < 8) {
$bitLen += 5;
$offset++;
$pentet = $encoded[$offset] ?? $padding;
if ($padding === $pentet) {
$offset = $length;
}
if ($strict && !array_key_exists($pentet, $chars)) {
throw new RuntimeException('The encoded data contains characters unknown to the alphabet.');
}
$val = ($val << 5) + ($chars[$pentet] ?? 0);
continue;
}
$shift = $bitLen - 8;
$decoded .= chr($val >> $shift);
$val &= ((1 << $shift) - 1);
$bitLen -= 8;
} while ($offset < $length);
return $decoded;
}
/**
* @return array<string>
*/
private function prepareDecoding(string $encoded, bool $strict): array
{
if ('' === $encoded) {
return ['', $this->alphabet, $this->padding];
}
$alphabet = $this->alphabet;
$padding = $this->padding;
$encoded = str_replace(str_split(self::RESERVED_CHARACTERS), [''], $encoded);
if (!$strict) {
$alphabet = strtoupper($alphabet);
$padding = strtoupper($padding);
$encoded = strtoupper($encoded);
}
$inside = rtrim($encoded, $padding);
$end = substr($encoded, strlen($inside));
if ($strict) {
$endLength = strlen($end);
if (0 !== $endLength && 1 !== $endLength && 3 !== $endLength && 4 !== $endLength && 6 !== $endLength) {
throw new RuntimeException('The encoded data ends with an invalid padding sequence length.');
}
}
if (false !== strpos($inside, $padding)) {
if ($strict) {
throw new RuntimeException('The padding character is used in the encoded data in an invalid place.');
}
$encoded = str_replace($padding, '', $inside).$end;
}
$remainder = strlen($encoded) % 8;
if (0 !== $remainder) {
if ($strict) {
throw new RuntimeException('The encoded data length is invalid.');
}
$remainderStr = '';
for ($index = 0; $index < $remainder; $index++) {
$remainderStr .= $padding;
}
$encoded .= $remainderStr;
}
return [$encoded, $alphabet, $padding];
}
}