-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPHPService.php
More file actions
190 lines (156 loc) · 5.15 KB
/
Copy pathPHPService.php
File metadata and controls
190 lines (156 loc) · 5.15 KB
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
<?php
namespace StephenHill;
use InvalidArgumentException;
class PHPService implements ServiceInterface
{
/**
* @var string
* @since v1.1.0
*/
protected $alphabet;
/**
* @var int
* @since v1.1.0
*/
protected $base;
/**
* Constructor
*
* @param string $alphabet optional
* @since v1.1.0
*/
public function __construct($alphabet = null)
{
// Handle null alphabet
if ($alphabet === null) {
$alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
}
// Type validation
if (\is_string($alphabet) === false) {
throw new InvalidArgumentException('Argument $alphabet must be a string.');
}
// The alphabet must contain 58 characters
if (\strlen($alphabet) !== 58) {
throw new InvalidArgumentException('Argument $alphabet must contain 58 characters.');
}
$this->alphabet = $alphabet;
$this->base = \strlen($alphabet);
}
/**
* Encode a string into base58.
*
* @param string $string The string you wish to encode.
* @since Release v1.1.0
* @return string The Base58 encoded string.
*/
public function encode($string): string
{
// Type validation
if (\is_string($string) === false) {
throw new InvalidArgumentException('Argument $string must be a string.');
}
// If the string is empty, then the encoded string is obviously empty
if ($string === '') {
return '';
}
// Strings in PHP are essentially 8-bit byte arrays
// so lets convert the string into a PHP array
$bytes = array_values(unpack('C*', $string));
$leadingZerosNeeded = 0;
foreach ($bytes as $byte) {
if ($byte !== 0) {
break;
}
$leadingZerosNeeded++;
}
$source = \array_slice($bytes, $leadingZerosNeeded);
$result = $this->convertBase($source, 256, 58);
// Count existing leading zeros
$leadingZeroCount = 0;
foreach ($result as $digit) {
if ($digit !== 0) {
break;
}
$leadingZeroCount++;
}
// Now we need to add any missing leading zeros
for ($i = $leadingZeroCount; $i < $leadingZerosNeeded; $i++) {
array_unshift($result, 0);
}
// Encode to a string
return implode('', array_map(function ($ord) { return $this->alphabet[$ord]; }, $result));
}
/**
* Decode base58 into a PHP string.
*
* @param string $base58 The base58 encoded string.
* @since Release v1.1.0
* @return string Returns the decoded string.
*/
public function decode($base58): string
{
// Type Validation
if (\is_string($base58) === false) {
throw new InvalidArgumentException('Argument $base58 must be a string.');
}
// If the string is empty, then the decoded string is obviously empty
if ($base58 === '') {
return '';
}
$indexes = array_flip(str_split($this->alphabet));
$chars = str_split($base58);
$digits = [];
// Check for invalid characters in the supplied base58 string
foreach ($chars as $char) {
if (isset($indexes[$char]) === false) {
throw new InvalidArgumentException('Argument $base58 contains invalid characters. ($char: "'.$char.'" | $base58: "'.$base58.'") ');
}
$digits[] = $indexes[$char];
}
$leadingZerosNeeded = 0;
foreach ($digits as $digit) {
if ($digit !== 0) {
break;
}
$leadingZerosNeeded++;
}
$source = \array_slice($digits, $leadingZerosNeeded);
$result = $this->convertBase($source, 58, 256);
// Count existing leading zeros
$leadingZeroCount = 0;
foreach ($result as $digit) {
if ($digit !== 0) {
break;
}
$leadingZeroCount++;
}
// Now we need to add any missing leading zeros
for ($i = $leadingZeroCount; $i < $leadingZerosNeeded; $i++) {
array_unshift($result, 0);
}
// Encode to a string
return implode('', array_map('\chr', $result));
}
/**
* Basic manual base conversion algorithm,
* @see https://en.wikipedia.org/wiki/Positional_notation#Base_conversion
**/
private function convertBase(array $digits, int $base1, int $base2): array
{
$result = [];
do {
$digitCount = \count($digits);
$quotient = [];
$remainder = 0;
for ($i = 0; $i < $digitCount; $i++) {
$dividend = $remainder * $base1 + $digits[$i];
$quotient[] = intdiv($dividend, $base2);
$remainder = $dividend % $base2;
}
$result[] = $remainder;
$digits = $quotient;
} while (array_sum($quotient) > 0);
// Now we need to reverse the results
return array_reverse($result);
}
}