Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I base58 encode a string?

Tags:

c

base

char (* text)[1][45+1];
text = calloc(5000,(130+1));
strcpy(0[*text],"sometext)");

Now I want to encode "sometext" to base58, however, I do not know how, and oddly enough, there isn't one example of BASE58 in C.

The base58 encoding I'm interested in uses these symbols:

123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ

It's been optimized to lessen the risk of mis-reading, so 0 and 'O' are both gone, for instance.

P.S Don't mind the weird allocation and declaration of the variables, I was experimenting.

like image 742
farmdve Avatar asked Jan 23 '12 11:01

farmdve


3 Answers

You're not supposed to encode strings, you're supposed to encode integers.

If starting with a string, you must first decide how to interpret it as an integer (might be base128, or something), then re-encode in base58.

like image 51
unwind Avatar answered Sep 28 '22 06:09

unwind


Satoshi has the reference implementation (https://github.com/bitcoin/bitcoin/blob/master/src/base58.h)

However, he uses some utility bignum class to do it, and it's in C++. If you have access to a bignum library, you just keep dividing by 58 until the number is broken up. If you don't have a bignum library, AFAIK you're outta luck.

like image 44
user2387532 Avatar answered Sep 28 '22 07:09

user2387532


Here's an implementation in PHP for large numbers I've created for Amithings, beyond the integers (Integer -> http://php.net/manual/en/language.types.integer.php).

For example, try the example below (Don't forget to pass your ID to the function in string format. Use the PHP function strval()):

$number = '123456789009876543211234567890';
$result = base58_encode($number);
echo('Encoded: ' . $result . '<br>');
echo('Decoded: ' . base58_decode($result) . '<br>');

Important: You may consider to change this routine by including some sort of key/password/encryption to ensure that others can not decode your database IDs.

function base58_encode($input)
{
    $alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
    $base_count = strval(strlen($alphabet));
    $encoded = '';
    while (floatval($input) >= floatval($base_count))
    {
        $div = bcdiv($input, $base_count);
        $mod = bcmod($input, $base_count);
        $encoded = substr($alphabet, intval($mod), 1) . $encoded;
        $input = $div;
    }
    if (floatval($input) > 0)
    {
        $encoded = substr($alphabet, intval($input), 1) . $encoded;
    }
    return($encoded);
}

function base58_decode($input)
{
    $alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
    $base_count = strval(strlen($alphabet));
    $decoded = strval(0);
    $multi = strval(1);
    while (strlen($input) > 0)
    {
        $digit = substr($input, strlen($input) - 1);
        $decoded = bcadd($decoded, bcmul($multi, strval(strpos($alphabet, $digit))));
        $multi = bcmul($multi, $base_count);
        $input = substr($input, 0, strlen($input) - 1);
    }
    return($decoded);
}
like image 35
Amithings Avatar answered Sep 28 '22 06:09

Amithings