How to base58 encode a string?

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

Now I want to encode "sometext" to base58, however I don’t know how, and, oddly enough, there is not a single BASE58 example in C.

The base58 encoding that interests me uses these characters:

123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ

It has been optimized to reduce the risk of misreading, so 0 and "O", for example, are gone.

PS Do not pay attention to the strange distribution and declaration of variables, I experimented.

+7
source share
5 answers

You do not have to encode strings; you must encode integers.

If you start with a string, you must first decide how to interpret it as a whole (maybe base128 or something else), and then transcode to base58.

+5
source

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

However, it uses some bignum utility class for this, as well as in C ++. If you have access to the bignum library, you just keep dividing by 58 until the number is broken. If you don't have a bignum library, AFAIK is out of luck.

+6
source

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

For example, try the example below (do not forget to pass your function identifier to string format. Use the PHP strval () function):

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

Important: you can change this procedure to include some kind of key / password / encryption so that others cannot decode your database identifiers.

 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); } 
+2
source

Here is an implementation that seems clean c .

 https://github.com/trezor/trezor-crypto/blob/master/base58.c 
+1
source

My simple code with the Crypto ++ library:

 string base58_encode(Integer num, string vers) { string alphabet[58] = {"1","2","3","4","5","6","7","8","9","A","B","C","D","E","F", "G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c", "d","e","f","g","h","i","j","k","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; int base_count = 58; string encoded; Integer div; Integer mod; while (num >= base_count) { div = num / base_count; mod = (num - (base_count * div)); encoded = alphabet[ mod.ConvertToLong() ] + encoded; num = div; } encoded = vers + alphabet[ num.ConvertToLong() ] + encoded; return encoded; } 
It just for cryptocurrency wallets. string can be changed for other tasks.
+1
source

All Articles