You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.1 KiB
52 lines
1.1 KiB
3 years ago
|
<?php
|
||
|
|
||
|
class Base58Check {
|
||
|
|
||
|
public static function encode(string $string, int $prefix = 128, bool $compressed = true) {
|
||
|
|
||
|
$string = hex2bin($string);
|
||
|
|
||
|
if ($prefix) {
|
||
|
$string = chr($prefix) . $string;
|
||
|
}
|
||
|
|
||
|
if ($compressed) {
|
||
|
$string .= chr(0x01);
|
||
|
}
|
||
|
|
||
|
$string = $string . substr(Hash::SHA256(Hash::SHA256($string)), 0, 4);
|
||
|
|
||
|
$base58 = Base58::encode(Crypto::bin2bc($string));
|
||
|
|
||
|
for ($i = 0; $i < strlen($string); $i++) {
|
||
|
|
||
|
if ($string[$i] != '\x00') {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
$base58 = '1' . $base58;
|
||
|
}
|
||
|
|
||
|
return $base58;
|
||
|
}
|
||
|
|
||
|
public static function decode(string $string, int $removeLeadingBytes = 1, int $removeTrailingBytes = 4, bool $removeCompression = true) {
|
||
|
|
||
|
$string = bin2hex(Crypto::bc2bin(Base58::decode($string)));
|
||
|
|
||
|
if ($removeLeadingBytes) {
|
||
|
$string = substr($string, $removeLeadingBytes * 2);
|
||
|
}
|
||
|
|
||
|
if ($removeTrailingBytes) {
|
||
|
$string = substr($string, 0, -($removeTrailingBytes * 2));
|
||
|
}
|
||
|
|
||
|
if ($removeCompression) {
|
||
|
$string = substr($string, 0, -2);
|
||
|
}
|
||
|
|
||
|
return $string;
|
||
|
}
|
||
|
}
|