X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fcrypto%2Fshachain%2Ftools%2Fshachain48.c;fp=ccan%2Fcrypto%2Fshachain%2Ftools%2Fshachain48.c;h=5cc910909ba1d33a9f151701a76134d9d328a0a2;hb=4bb69fe6d568f84bd682cf6f86bae168313deb91;hp=0000000000000000000000000000000000000000;hpb=d07f742c5925b97ed558eb07aae285616f5df823;p=ccan diff --git a/ccan/crypto/shachain/tools/shachain48.c b/ccan/crypto/shachain/tools/shachain48.c new file mode 100644 index 00000000..5cc91090 --- /dev/null +++ b/ccan/crypto/shachain/tools/shachain48.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc == 2 && streq(argv[1], "--store")) { + struct shachain s; + struct rbuf rbuf; + size_t size = rbuf_good_size(STDIN_FILENO); + char *p; + + shachain_init(&s); + rbuf_init(&rbuf, STDIN_FILENO, malloc(size), size); + + while ((p = rbuf_read_str(&rbuf, '\n', realloc)) != NULL) { + struct sha256 hash; + unsigned long long idx; + + if (strstarts(p, "0x")) + p += 2; + if (!hex_decode(p, 64, &hash, sizeof(hash))) + errx(2, "%.*s is not 64 chars of hex", 64, p); + p += 64; + p += strspn(p, " \t"); + idx = strtoull(p, NULL, 0); + if (shachain_add_hash(&s, idx, &hash)) + printf("OK\n"); + else + printf("ERROR\n"); + } + } else if (argc == 3) { + struct sha256 seed, hash; + const char *p; + unsigned long long idx; + char hex[65]; + + if (strstarts(argv[1], "0x")) + p = argv[1] + 2; + else + p = argv[1]; + idx = strtoull(argv[2], NULL, 0); + + if (!hex_decode(p, 64, &seed, sizeof(seed))) + errx(2, "%s is not 64 chars of hex", p); + + shachain_from_seed(&seed, idx, &hash); + hex_encode(&hash, sizeof(hash), hex, sizeof(hex)); + printf("0x%s\n", hex); + } else + errx(1, "Usage: shachain --store OR shachain "); + return 0; +}