-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base58.php
141 lines (121 loc) · 4.47 KB
/
Base58.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
<?php
/******************************************************************************
* This file is part of the php-base58 project. You can always find the latest
* version of this class and project at: https://github.com/ionux/php-base58
*
* Copyright (c) 2015-2024 Rich Morgan, [email protected]
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
/**
* Base-58 value encoding/decoding class. This encoding
* type is specifically used for Bitcoin-related tasks.
*
* @author Rich Morgan <[email protected]>
*/
final class Base58
{
/**
* @var string
*/
private $b58_chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
/**
* Public constructor method.
*/
public function __construct()
{
bcscale(0);
}
/**
* Encodes a hex value to Base-58
*
* @param string $hex
* @return string $return
* @throws \Exception
*/
public function encode($hex)
{
$this->paramsCheck(array($hex));
try {
if (strlen($hex) % 2 != 0) {
throw new \Exception('Error in Base58::encode(): Uneven number of hex characters passed to function. Value received was "' . var_export($hex, true) . '".');
}
$orighex = $hex;
$new = '';
while (bccomp($hex, '0') > 0) {
$qq = bcdiv($hex, '58');
$rem = bcmod($hex, '58');
$val = $qq;
$new = $new . $this->b58_chars[$rem];
}
$return = strrev($return);
for ($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == '00'; $i += 2) {
$return = '1' . $return;
}
return $return;
} catch (\Exception $e) {
throw $e;
}
}
/**
* Decodes a Base-58 encoded value to hex.
*
* @param string $base58
* @return string $return
* @throws \Exception
*/
public function decode($base58)
{
$this->paramsCheck(array($base58));
try {
$origbase58 = $base58;
$return = '0';
$b58_len = strlen($base58);
for ($i = 0; $i < $b58_len; $i++) {
$current = strpos($this->b58_chars, $base58[$i]);
$return = bcmul($return, '58');
$return = bcadd($return, $current);
}
for ($i = 0; $i < strlen($origbase58) && $origbase58[$i] == '1'; $i++) {
$return = '00' . $return;
}
return (strlen($return) %2 != 0) ? '0' . $return : $return;
} catch (\Exception $e) {
throw $e;
}
}
/**
* Handles the validation checking for method parameters.
*
* @param array $params The array of parameters to check.
* @return boolean Will only be true, otherwise throws \Exception
* @throws \Exception
*/
private function paramsCheck(array $params)
{
foreach ($params as $key => $value) {
if (true === empty($value)) {
$caller = debug_backtrace();
throw new \Exception('Empty or invalid parameters passed to ' . $caller[count($caller)-1]['function'] . ' function. Argument list received: ' . var_export($caller[count($caller)-1]['args'], true));
}
}
return true;
}
}