Last active
September 14, 2018 12:36
-
-
Save WietseWind/42e5b8dce8dda95b47e263e5084bcfb8 to your computer and use it in GitHub Desktop.
Ripple Validator HEX to public key
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 | |
function decodeHex($hex) { | |
$chars = '0123456789ABCDEF'; | |
$return = '0'; | |
for ($i = 0; $i < strlen($hex); $i++) { | |
$current = (string) strpos($chars, $hex[$i]); | |
$return = (string) bcmul($return, '16', 0); | |
$return = (string) bcadd($return, $current, 0); | |
} | |
return $return; | |
} | |
function encodeBase58($hex) { | |
$orighex = $hex; | |
$chars = 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz'; // Ripple alphabet | |
$hex = decodeHex($hex); | |
$return = ''; | |
while (bccomp($hex, 0) == 1) { | |
$dv = (string) bcdiv($hex, '58', 0); | |
$rem = (integer) bcmod($hex, '58'); | |
$hex = $dv; | |
$return = $return.$chars[$rem]; | |
} | |
$return = strrev($return); | |
for ($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == '00'; $i += 2) { $return = '1' . $return; } | |
return $return; | |
} | |
/// Here we go. | |
$hex = 'ED47A79FDB712056366C08617357AF413708CA11499FD42634FDEE42A721360355'; | |
$extendedKey = '1C' . $hex; | |
$buffer = $extendedKey; // We'll modify this one. | |
// sha256.x2 | |
$buffer = strtoupper(hash('sha256', hex2bin($buffer))); | |
$buffer = strtoupper(hash('sha256', hex2bin($buffer))); | |
// (0, 8) since 4 chars are 8 bytes | |
$checksum = substr($buffer, 0, 8); | |
// Append the checksum | |
$buffer = $extendedKey . $checksum; | |
// Base58 encode | |
$buffer = encodeBase58($buffer); | |
echo $buffer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment