Skip to content

Instantly share code, notes, and snippets.

@tuupola
Last active December 23, 2025 08:26
Show Gist options
  • Select an option

  • Save tuupola/b2f90580d0d80b62c1d350c7d9d9854e to your computer and use it in GitHub Desktop.

Select an option

Save tuupola/b2f90580d0d80b62c1d350c7d9d9854e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <openssl/sha.h>
#include "libbase58.h"
/* SHA256 callback for libbase58 */
static bool sha256_impl(void *digest, const void *data, size_t datasz)
{
SHA256(data, datasz, digest);
return true;
}
int main() {
char b58[64];
size_t b58sz;
unsigned char bin[64];
size_t binsz;
/* Register SHA256 implementation for Base58Check */
b58_sha256_impl = sha256_impl;
/* Test 1: Encode empty string */
printf("=== Encoding empty string ===\n");
b58sz = sizeof(b58);
if (b58enc(b58, &b58sz, "", 0)) {
printf("Encoded: '%s' (length: %zu)\n", b58, b58sz - 1);
} else {
printf("Encoding failed\n");
}
/* Test 2: Decode empty string */
printf("\n=== Decoding empty string ===\n");
binsz = sizeof(bin);
if (b58tobin(bin, &binsz, "", 0)) {
printf("Decoded length: %zu\n", binsz);
} else {
printf("Decoding failed\n");
}
/* Test 3: Encode single null byte */
printf("\n=== Encoding single null byte (\\x00) ===\n");
b58sz = sizeof(b58);
if (b58enc(b58, &b58sz, "\x00", 1)) {
printf("Encoded: '%s' (length: %zu)\n", b58, b58sz - 1);
} else {
printf("Encoding failed\n");
}
/* Test 4: Base58Check encode empty string (version 0x00) */
printf("\n=== Base58Check encode empty string (version 0x00) ===\n");
b58sz = sizeof(b58);
if (b58check_enc(b58, &b58sz, 0x00, "", 0)) {
printf("Encoded: '%s' (length: %zu)\n", b58, b58sz - 1);
} else {
printf("Encoding failed\n");
}
/* Test 5: Decode "1Wh4bh" (Base58Check encoded empty string) */
printf("\n=== Decoding '1Wh4bh' ===\n");
binsz = sizeof(bin);
memset(bin, 0xff, sizeof(bin));
if (b58tobin(bin, &binsz, "1Wh4bh", 6)) {
printf("Decoded length: %zu\n", binsz);
printf("Decoded bytes: ");
for (size_t i = 0; i < binsz; i++) {
printf("%02x ", bin[i]);
}
printf("\n");
printf("(version: %02x, payload: empty, checksum: %02x%02x%02x%02x)\n",
bin[0], bin[1], bin[2], bin[3], bin[4]);
} else {
printf("Decoding failed\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment