Created
March 6, 2021 11:18
-
-
Save tzkmx/028c95dc1986c79d6842bc4cd52a59f0 to your computer and use it in GitHub Desktop.
experimentos base58
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('ALPHABET', '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'); | |
function numT58($num) | |
{ | |
$acc = ''; | |
while ($num >= 58) { | |
$mod = $num % 58; | |
$num = intval(floor($num / 58)); | |
echo $mod . ' :'; | |
echo ALPHABET[$mod], "\t", $num, "\n"; | |
$acc = ALPHABET[$mod] . $acc; | |
} | |
$acc = ALPHABET[$num] . $acc; | |
echo $acc; | |
} | |
function b58dec(string $b58) | |
{ | |
echo $b58, "\n"; | |
$acc = 0; | |
$len = $i = strlen($b58); | |
while ($i-- > 0) { | |
$char = $b58[$i]; | |
$num = strpos(ALPHABET, $char); | |
$power = $len - $i - 1; | |
$up = $num * pow(58, $power); | |
$acc += $up; | |
echo json_encode( | |
compact('i', 'char', 'num', | |
'power', 'up', 'acc') | |
), "\n"; | |
} | |
echo $acc, "\n"; | |
echo dechex($acc); | |
} | |
$num1 = 2064940092; | |
$b58r = '49UMkP'; | |
sha256(hex): 4569e91f1a2f6e1c3cc085a143e404db91591c2b9c25d1b12c1d88f0bb4c2bad
base58: 5fxoLz932qGvY57xjSq9akM9mp5LaQzjggNxRnWC6dpY
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
while( true ){ if( strlen($num) < 2 ) { if( intval($num) <= 0 ) { break; } } $rem = bcmod($num, $base); $rep = $basestr[intval($rem)] . $rep; $num = bcdiv(bcsub($num, $rem), $base); } return $rep;