1 | <?php |
||
9 | class Bech32 |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | protected static $charset = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected static $charsetKey = [ |
||
20 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||
21 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||
22 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||
23 | 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, |
||
24 | -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, |
||
25 | 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1, |
||
26 | -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, |
||
27 | 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1 |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected static $generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; |
||
34 | |||
35 | /** |
||
36 | * @param int[] $values |
||
37 | * @param int $numValues |
||
38 | * @return int |
||
39 | */ |
||
40 | public static function polyMod(array $values, int $numValues): int |
||
55 | |||
56 | /** |
||
57 | * Expands the human readable part into a character array for checksumming. |
||
58 | * @param string $hrp |
||
59 | * @param int $hrpLen |
||
60 | * @return int[] |
||
61 | */ |
||
62 | public static function hrpExpand(string $hrp, int $hrpLen): array |
||
74 | |||
75 | /** |
||
76 | * Converts words of $fromBits bits to $toBits bits in size. |
||
77 | * |
||
78 | * @param int[] $data - character array of data to convert |
||
79 | * @param int $inLen - number of elements in array |
||
80 | * @param int $fromBits - word (bit count) size of provided data |
||
81 | * @param int $toBits - requested word size (bit count) |
||
82 | * @param bool $pad - whether to pad (only when encoding) |
||
83 | * @return int[] |
||
84 | * @throws Bech32Exception |
||
85 | */ |
||
86 | 1 | public static function convertBits(array $data, int $inLen, int $fromBits, int $toBits, bool $pad = true): array |
|
119 | |||
120 | /** |
||
121 | * @param string $hrp |
||
122 | * @param int[] $convertedDataChars |
||
123 | * @return int[] |
||
124 | */ |
||
125 | public static function createChecksum(string $hrp, array $convertedDataChars): array |
||
136 | |||
137 | /** |
||
138 | * Verifies the checksum given $hrp and $convertedDataChars. |
||
139 | * |
||
140 | * @param string $hrp |
||
141 | * @param int[] $convertedDataChars |
||
142 | * @return bool |
||
143 | */ |
||
144 | public static function verifyChecksum(string $hrp, array $convertedDataChars): bool |
||
151 | |||
152 | /** |
||
153 | * @param string $hrp |
||
154 | * @param array $combinedDataChars |
||
155 | * @return string |
||
156 | */ |
||
157 | public static function encode(string $hrp, array $combinedDataChars): string |
||
169 | |||
170 | /** |
||
171 | * Validates a bech32 string and returns [$hrp, $dataChars] if |
||
172 | * the conversion was successful. An exception is thrown on invalid |
||
173 | * data. |
||
174 | * |
||
175 | * @param string $sBech - the bech32 encoded string |
||
176 | * @return array - returns [$hrp, $dataChars] |
||
177 | * @throws Bech32Exception |
||
178 | */ |
||
179 | 9 | public static function decode(string $sBech): array |
|
183 | } |
||
184 |