1 #include <ccan/crypto/shachain/shachain.h>
2 #include <ccan/str/hex/hex.h>
3 #include <ccan/str/str.h>
4 #include <ccan/err/err.h>
5 #include <ccan/rbuf/rbuf.h>
10 int main(int argc, char *argv[])
12 if (argc == 2 && streq(argv[1], "--store")) {
15 size_t size = rbuf_good_size(STDIN_FILENO);
19 rbuf_init(&rbuf, STDIN_FILENO, malloc(size), size);
21 while ((p = rbuf_read_str(&rbuf, '\n', realloc)) != NULL) {
23 unsigned long long idx;
25 if (strstarts(p, "0x"))
27 if (!hex_decode(p, 64, &hash, sizeof(hash)))
28 errx(2, "%.*s is not 64 chars of hex", 64, p);
30 p += strspn(p, " \t");
31 idx = strtoull(p, NULL, 0);
32 if (shachain_add_hash(&s, idx, &hash))
37 } else if (argc == 3) {
38 struct sha256 seed, hash;
40 unsigned long long idx;
43 if (strstarts(argv[1], "0x"))
47 idx = strtoull(argv[2], NULL, 0);
49 if (!hex_decode(p, 64, &seed, sizeof(seed)))
50 errx(2, "%s is not 64 chars of hex", p);
52 shachain_from_seed(&seed, idx, &hash);
53 hex_encode(&hash, sizeof(hash), hex, sizeof(hex));
54 printf("0x%s\n", hex);
56 errx(1, "Usage: shachain --store OR shachain <seed> <index>");