From 4eebef1961a1c01890fe11dc5f1b9f3a1ea705e0 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 6 Sep 2023 23:23:31 -0400 Subject: add examples/ --- examples/03-tuplehash128/main.c | 75 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 examples/03-tuplehash128/main.c (limited to 'examples/03-tuplehash128/main.c') diff --git a/examples/03-tuplehash128/main.c b/examples/03-tuplehash128/main.c new file mode 100644 index 0000000..c9e1952 --- /dev/null +++ b/examples/03-tuplehash128/main.c @@ -0,0 +1,75 @@ +// +// tuplehash128-example: Hash tuples with TupleHash128 and key +// and print first 20 bytes of result to standard output. +// +#include +#include +#include "hex.h" +#include "sha3.h" + +static const struct { + const char *name; + tuplehash_str_t strs[10]; + size_t num_strs; +} ROWS[] = {{ + .name = "TupleHash128: \"abc\"", + .strs = {{ + .ptr = (uint8_t*) "abc", + .len = 3, + }}, + .num_strs = 1, +}, { + .name = "TupleHash128: \"ab\", \"c\"", + .strs = {{ + .ptr = (uint8_t*) "ab", + .len = 2, + }, { + .ptr = (uint8_t*) "c", + .len = 1, + }}, + .num_strs = 2, +}, { + .name = "TupleHash128: \"a\", \"bc\"", + .strs = {{ + .ptr = (uint8_t*) "a", + .len = 1, + }, { + .ptr = (uint8_t*) "bc", + .len = 2, + }}, + .num_strs = 2, +}, { + .name = "TupleHash128: \"a\", \"b\", \"c\"", + .strs = {{ + .ptr = (uint8_t*) "a", + .len = 1, + }, { + .ptr = (uint8_t*) "b", + .len = 1, + }, { + .ptr = (uint8_t*) "c", + .len = 1, + }}, + .num_strs = 3, +}}; + +int main() { + for (size_t i = 0; i < sizeof(ROWS)/sizeof(ROWS[0]); i++) { + // build params + tuplehash_params_t params = { + .strs = ROWS[i].strs, + .num_strs = ROWS[i].num_strs, + }; + + // hash data + uint8_t buf[32]; + tuplehash128(params, buf, sizeof(buf)); + + // print to stdout + printf("%s:\n", ROWS[i].name); + hex_write(stdout, buf, sizeof(buf)); + printf("\n"); + } + + return 0; +} -- cgit v1.2.3