FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bitcoin-php/src/Base58.php at main · ProtonMail/bitcoin-php · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
ProtonMail
/
bitcoin-php
Public
forked from
Bit-Wasp/bitcoin-php
Notifications
You must be signed in to change notification settings
Fork
23
Star
32
Code
Issues
3
Pull requests
1
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
bitcoin-php
/
src
/
Base58.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
133 lines (114 loc) · 3.71 KB
Breadcrumbs
bitcoin-php
/
src
/
Base58.php
Copy path
File metadata and controls
133 lines (114 loc) · 3.71 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
declare
(strict_types=
1
);
namespace
BitWasp
\
Bitcoin
;
use
BitWasp
\
Bitcoin
\
Crypto
\
Hash
;
use
BitWasp
\
Bitcoin
\
Exceptions
\
Base58ChecksumFailure
;
use
BitWasp
\
Bitcoin
\
Exceptions
\
Base58InvalidCharacter
;
use
BitWasp
\
Buffertools
\
Buffer
;
use
BitWasp
\
Buffertools
\
BufferInterface
;
use
BitWasp
\
Buffertools
\
Buffertools
;
class
Base58
{
/**
* @var string
*/
private
static
$
base58chars
=
'
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
'
;
/**
* Encode a given hex string in base58
*
* @param BufferInterface $buffer
* @return string
*/
public
static
function
encode
(
BufferInterface
$
buffer
):
string
{
$
size
=
$
buffer
->
getSize
();
if
(
$
size
===
0
) {
return
''
;
}
$
orig
=
$
buffer
->
getBinary
();
$
decimal
=
$
buffer
->
getGmp
();
$
return
=
''
;
$
zero
=
gmp_init
(
0
);
$
_58
=
gmp_init
(
58
);
while
(
gmp_cmp
(
$
decimal
,
$
zero
) >
0
) {
$
div
=
gmp_div
(
$
decimal
,
$
_58
);
$
rem
=
gmp_sub
(
$
decimal
,
gmp_mul
(
$
div
,
$
_58
));
$
return
.=
self
::
$
base58chars
[(
int
)
gmp_strval
(
$
rem
,
10
)];
$
decimal
=
$
div
;
}
$
return
=
strrev
(
$
return
);
// Leading zeros
for
(
$
i
=
0
;
$
i
<
$
size
&&
$
orig
[
$
i
] ===
"\x00"
;
$
i
++) {
$
return
=
'
1
'
.
$
return
;
}
return
$
return
;
}
/**
* Decode a base58 string
* @param string $base58
* @return BufferInterface
* @throws Base58InvalidCharacter
*/
public
static
function
decode
(
string
$
base58
):
BufferInterface
{
if
(
$
base58
===
''
) {
return
new
Buffer
(
''
,
0
);
}
$
original
=
$
base58
;
$
length
=
strlen
(
$
base58
);
$
return
=
gmp_init
(
0
);
$
_58
=
gmp_init
(
58
);
for
(
$
i
=
0
;
$
i
<
$
length
;
$
i
++) {
$
loc
=
strpos
(
self
::
$
base58chars
,
$
base58
[
$
i
]);
if
(
$
loc
===
false
) {
throw
new
Base58InvalidCharacter
(
'
Found character that is not allowed in base58:
'
.
$
base58
[
$
i
]);
}
$
return
=
gmp_add
(
gmp_mul
(
$
return
,
$
_58
),
gmp_init
(
$
loc
,
10
));
}
$
binary
=
gmp_cmp
(
$
return
,
gmp_init
(
0
)) ===
0
?
''
: Buffer::
int
(
gmp_strval
(
$
return
,
10
))->
getBinary
();
for
(
$
i
=
0
;
$
i
<
$
length
&&
$
original
[
$
i
] ===
'
1
'
;
$
i
++) {
$
binary
=
"\x00"
.
$
binary
;
}
return
new
Buffer
(
$
binary
);
}
/**
* @param BufferInterface $data
* @return BufferInterface
*/
public
static
function
checksum
(
BufferInterface
$
data
):
BufferInterface
{
return
Hash::
sha256d
(
$
data
)->
slice
(
0
,
4
);
}
/**
* Decode a base58 checksum string and validate checksum
* @param string $base58
* @return BufferInterface
* @throws Base58ChecksumFailure
* @throws Base58InvalidCharacter
* @throws \Exception
*/
public
static
function
decodeCheck
(
string
$
base58
):
BufferInterface
{
$
decoded
=
self
::
decode
(
$
base58
);
$
checksumLength
=
4
;
if
(
$
decoded
->
getSize
() <
$
checksumLength
) {
throw
new
Base58ChecksumFailure
(
"
Missing base58 checksum
"
);
}
$
data
=
$
decoded
->
slice
(
0
, -
$
checksumLength
);
$
csVerify
=
$
decoded
->
slice
(-
$
checksumLength
);
if
(!
hash_equals
(
self
::
checksum
(
$
data
)->
getBinary
(),
$
csVerify
->
getBinary
())) {
throw
new
Base58ChecksumFailure
(
'
Failed to verify checksum
'
);
}
return
$
data
;
}
/**
* Encode the given data in base58, with a checksum to check integrity.
*
* @param BufferInterface $data
* @return string
*/
public
static
function
encodeCheck
(
BufferInterface
$
data
):
string
{
return
self
::
encode
(Buffertools::
concat
(
$
data
,
self
::
checksum
(
$
data
)));
}
}
Back
|
FazBrowse Home
|
New Git URL