Command line tool "base58"
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -19,3 +19,4 @@ Makefile.in
|
||||
.libs
|
||||
ii
|
||||
*.tar*
|
||||
base58
|
||||
|
||||
@@ -15,3 +15,8 @@ pkgconfig_DATA = libbase58.pc
|
||||
|
||||
dist_noinst_SCRIPTS = autogen.sh
|
||||
dist_doc_DATA = README
|
||||
|
||||
bin_PROGRAMS = base58
|
||||
base58_SOURCES = clitool.c
|
||||
base58_CFLAGS = $(LIBGCRYPT_CFLAGS)
|
||||
base58_LDADD = libbase58.la $(LIBGCRYPT_LIBS)
|
||||
|
||||
98
clitool.c
Normal file
98
clitool.c
Normal file
@@ -0,0 +1,98 @@
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <gcrypt.h>
|
||||
|
||||
#include "base58.h"
|
||||
|
||||
static
|
||||
bool my_sha256(void *digest, const void *data, size_t datasz)
|
||||
{
|
||||
gcry_md_hash_buffer(GCRY_MD_SHA256, digest, data, datasz);
|
||||
return true;
|
||||
}
|
||||
|
||||
static
|
||||
void usage(const char *prog)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s [-c] [-d] [data]\n", prog);
|
||||
fprintf(stderr, "\t-c Use base58check (default: raw base58)\n");
|
||||
fprintf(stderr, "\t-d <size> Decode <size> bytes\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
bool b58c = false;
|
||||
int decode = 0;
|
||||
int opt;
|
||||
while ( (opt = getopt(argc, argv, "cd:h")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 'c':
|
||||
b58c = true;
|
||||
b58_sha256_impl = my_sha256;
|
||||
break;
|
||||
case 'd':
|
||||
decode = atoi(optarg);
|
||||
break;
|
||||
default:
|
||||
usage(argv[0]);
|
||||
}
|
||||
}
|
||||
|
||||
size_t rt;
|
||||
uint8_t *r;
|
||||
if (optind >= argc)
|
||||
{
|
||||
rt = 0;
|
||||
r = NULL;
|
||||
while (!feof(stdin))
|
||||
{
|
||||
r = realloc(r, rt + 0x100);
|
||||
rt += fread(&r[rt], 1, 0x100, stdin);
|
||||
}
|
||||
if (decode)
|
||||
while (isspace(r[rt-1]))
|
||||
--rt;
|
||||
}
|
||||
else
|
||||
{
|
||||
r = argv[optind];
|
||||
rt = strlen(r);
|
||||
}
|
||||
|
||||
if (decode)
|
||||
{
|
||||
uint8_t bin[decode];
|
||||
if (!b58tobin(bin, decode, r, rt))
|
||||
return 2;
|
||||
if (b58c)
|
||||
{
|
||||
int chk = b58check(bin, decode, r, rt);
|
||||
if (chk < 0)
|
||||
return chk;
|
||||
}
|
||||
if (fwrite(bin, decode, 1, stdout) != 1)
|
||||
return 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t ssz = rt * 2;
|
||||
char s[ssz];
|
||||
bool rv;
|
||||
if (b58c)
|
||||
rv = rt && b58check_enc(s, &ssz, r[0], &r[1], rt-1);
|
||||
else
|
||||
rv = b58enc(s, &ssz, r, rt);
|
||||
if (!rv)
|
||||
return 2;
|
||||
puts(s);
|
||||
}
|
||||
}
|
||||
16
configure.ac
16
configure.ac
@@ -25,4 +25,20 @@ AC_CONFIG_FILES([Makefile
|
||||
|
||||
AC_CHECK_LIB([ws2_32], [strchr])
|
||||
|
||||
AC_ARG_ENABLE([tool],
|
||||
[AC_HELP_STRING([--disable-tool],[Compile command line base58 tool (default enabled)])],
|
||||
[use_tool=$enableval],
|
||||
[use_tool=auto])
|
||||
if test x$use_tool != xno; then
|
||||
AM_PATH_LIBGCRYPT([],[
|
||||
use_tool=yes
|
||||
],[
|
||||
if test x$use_tool = xyes; then
|
||||
AC_MSG_ERROR([libgcrypt not found; use --disable-tool])
|
||||
fi
|
||||
use_tool=no
|
||||
])
|
||||
fi
|
||||
AM_CONDITIONAL([USE_TOOL], [test x$use_tool = xyes])
|
||||
|
||||
AC_OUTPUT
|
||||
|
||||
Reference in New Issue
Block a user