diff --git a/README b/README index c6d4436..3543de0 100644 --- a/README +++ b/README @@ -40,3 +40,12 @@ itself. When b58enc returns, the variable will be modified to contain the actual number of bytes used (including the null terminator). If encoding fails for any reason, or if the string buffer is not large enough for the result, b58enc will return false. Otherwise, it returns true to indicate success. + + +Encoding Base58Check +-------------------- + +Targetting base58check is done similarly to raw base58 encoding, but you must +also provide a version byte: + bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, + const void *data, size_t datasz) diff --git a/base58.c b/base58.c index 7e78e11..06550e7 100644 --- a/base58.c +++ b/base58.c @@ -158,3 +158,19 @@ bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) return true; } + +bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz) +{ + uint8_t buf[1 + datasz + 0x20]; + uint8_t *hash = &buf[1 + datasz]; + + buf[0] = ver; + memcpy(&buf[1], data, datasz); + if (!my_dblsha256(hash, buf, datasz + 1)) + { + *b58c_sz = 0; + return false; + } + + return b58enc(b58c, b58c_sz, buf, 1 + datasz + 4); +} diff --git a/base58.h b/base58.h index d2e1468..d2baa54 100644 --- a/base58.h +++ b/base58.h @@ -10,5 +10,6 @@ extern bool b58tobin(void *bin, size_t binsz, const char *b58, size_t b58sz); extern int b58check(const void *bin, size_t binsz, const char *b58, size_t b58sz); extern bool b58enc(char *b58, size_t *b58sz, const void *bin, size_t binsz); +extern bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz); #endif