diff options
author | Paul Duncan <pabs@pablotron.org> | 2023-09-06 23:23:31 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2023-09-06 23:23:31 -0400 |
commit | 4eebef1961a1c01890fe11dc5f1b9f3a1ea705e0 (patch) | |
tree | c48e3f15f1f26ce4456c549c2211545e8448cfac /examples/04-turboshake128/main.c | |
parent | 7887a482dc7951a88fd0b2a4caa8988f0aef4340 (diff) | |
download | sha3-4eebef1961a1c01890fe11dc5f1b9f3a1ea705e0.tar.bz2 sha3-4eebef1961a1c01890fe11dc5f1b9f3a1ea705e0.zip |
add examples/
Diffstat (limited to 'examples/04-turboshake128/main.c')
-rw-r--r-- | examples/04-turboshake128/main.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/examples/04-turboshake128/main.c b/examples/04-turboshake128/main.c new file mode 100644 index 0000000..a20a323 --- /dev/null +++ b/examples/04-turboshake128/main.c @@ -0,0 +1,56 @@ +// +// turboshake128-example: Absorb 4096 bytes of data from /dev/urandom in +// 1024 byte chunks, hash it with TurboShake128, then print 128 bytes of +// result in 32 byte chunks (hex-encoded) to standard output. +// output. +// +#include <stdint.h> +#include <stdio.h> +#include <err.h> +#include "hex.h" +#include "sha3.h" + +int main() { + // init turboshake + turboshake_t ts; + turboshake128_init(&ts); + + // open source + FILE *fh = fopen("/dev/urandom", "rb"); + if (!fh) { + err(-1, "fopen()"); + } + + { + // read and absorb + uint8_t buf[1024] = { 0 }; + for (size_t i = 0; i < 4096/sizeof(buf); i++) { + // read data + if (!fread(buf, sizeof(buf), 1, fh)) { + err(-1, "fread()"); + } + + // absorb + turboshake128_absorb(&ts, buf, sizeof(buf)); + } + } + + // close source + if (!fclose(fh)) { + warn("fclose()"); + } + + + printf("TurboShake128 output:\n"); + { + // squeeze in 32 byte chunks, write to stdout + uint8_t buf[32] = { 0 }; + for (size_t ofs = 0; ofs < 128; ofs += sizeof(buf)) { + turboshake128_squeeze(&ts, buf, sizeof(buf)); + hex_write(stdout, buf, sizeof(buf)); + } + } + printf("\n"); + + return 0; +} |