summaryrefslogtreecommitdiff
path: root/examples/04-turboshake128
diff options
context:
space:
mode:
Diffstat (limited to 'examples/04-turboshake128')
-rw-r--r--examples/04-turboshake128/Makefile16
l---------examples/04-turboshake128/hex.h1
-rw-r--r--examples/04-turboshake128/main.c56
l---------examples/04-turboshake128/sha3.c1
l---------examples/04-turboshake128/sha3.h1
5 files changed, 75 insertions, 0 deletions
diff --git a/examples/04-turboshake128/Makefile b/examples/04-turboshake128/Makefile
new file mode 100644
index 0000000..020ca7c
--- /dev/null
+++ b/examples/04-turboshake128/Makefile
@@ -0,0 +1,16 @@
+CFLAGS=-W -Wall -Wextra -Werror -pedantic -std=c11 -O3 -march=native -mtune=native
+APP=./turbohash128-example
+OBJS=sha3.o main.o
+
+.PHONY=all
+
+all: $(APP)
+
+$(APP): $(OBJS)
+ $(CC) -o $(APP) $(CFLAGS) $(OBJS)
+
+%.o: %.c
+ $(CC) -c $(CFLAGS) $<
+
+clean:
+ $(RM) -f $(APP) $(OBJS)
diff --git a/examples/04-turboshake128/hex.h b/examples/04-turboshake128/hex.h
new file mode 120000
index 0000000..2adfa3e
--- /dev/null
+++ b/examples/04-turboshake128/hex.h
@@ -0,0 +1 @@
+../../hex.h \ No newline at end of file
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;
+}
diff --git a/examples/04-turboshake128/sha3.c b/examples/04-turboshake128/sha3.c
new file mode 120000
index 0000000..4748193
--- /dev/null
+++ b/examples/04-turboshake128/sha3.c
@@ -0,0 +1 @@
+../../sha3.c \ No newline at end of file
diff --git a/examples/04-turboshake128/sha3.h b/examples/04-turboshake128/sha3.h
new file mode 120000
index 0000000..b7c53d4
--- /dev/null
+++ b/examples/04-turboshake128/sha3.h
@@ -0,0 +1 @@
+../../sha3.h \ No newline at end of file