aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2023-09-06 23:23:08 -0400
committerPaul Duncan <pabs@pablotron.org>2023-09-06 23:23:08 -0400
commit7887a482dc7951a88fd0b2a4caa8988f0aef4340 (patch)
tree6f4b6a823da4447b8a6fa74343d4f31da720ea7b
parent088e6c53453d672cc9110f8d0ec5355a1af6920f (diff)
downloadsha3-7887a482dc7951a88fd0b2a4caa8988f0aef4340.tar.bz2
sha3-7887a482dc7951a88fd0b2a4caa8988f0aef4340.zip
add hex.h, refactor main.c
-rw-r--r--hex.h15
-rw-r--r--main.c20
2 files changed, 22 insertions, 13 deletions
diff --git a/hex.h b/hex.h
new file mode 100644
index 0000000..246aeed
--- /dev/null
+++ b/hex.h
@@ -0,0 +1,15 @@
+#ifndef HEX_H
+#define HEX_H
+
+#include <stdio.h> // fprintf()
+#include <stdint.h> // uint8_t
+
+// print hex-encoded buffer to given file handle.
+// (used by top-level main.c)
+static void hex_write(FILE *fh, const uint8_t * const buf, const size_t len) {
+ for (size_t i = 0; i < len; i++) {
+ fprintf(fh, "%02x", buf[i]);
+ }
+}
+
+#endif /* HEX_H */
diff --git a/main.c b/main.c
index a616ebd..d7918f4 100644
--- a/main.c
+++ b/main.c
@@ -3,15 +3,9 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include "hex.h" // hex_write()
#include "sha3.h"
-// print hex-encoded buffer to stdout.
-static void print_hex(const uint8_t * const buf, const size_t len) {
- for (size_t i = 0; i < len; i++) {
- printf("%02x", buf[i]);
- }
-}
-
// shake128-xof handler
static void do_shake128_xof(const uint8_t *msg, const size_t msg_len, const size_t out_len) {
// init xof
@@ -29,7 +23,7 @@ static void do_shake128_xof(const uint8_t *msg, const size_t msg_len, const size
// squeeze and print
const size_t len = (out_len - i < sizeof(buf)) ? out_len - i : sizeof(buf);
shake128_xof_squeeze(&xof, buf, len);
- print_hex(buf, len);
+ hex_write(stdout, buf, len);
}
fputs("\n", stdout);
@@ -52,7 +46,7 @@ static void do_shake256_xof(const uint8_t * const msg, const size_t msg_len, con
// squeeze and print
const size_t len = (out_len - i < sizeof(buf)) ? out_len - i : sizeof(buf);
shake256_xof_squeeze(&xof, buf, len);
- print_hex(buf, len);
+ hex_write(stdout, buf, len);
}
fputs("\n", stdout);
@@ -75,7 +69,7 @@ static void do_turboshake128(const uint8_t * const msg, const size_t msg_len, co
// squeeze and print
const size_t len = (out_len - i < sizeof(buf)) ? out_len - i : sizeof(buf);
turboshake128_squeeze(&ts, buf, len);
- print_hex(buf, len);
+ hex_write(stdout, buf, len);
}
fputs("\n", stdout);
@@ -98,7 +92,7 @@ static void do_turboshake256(const uint8_t * const msg, const size_t msg_len, co
// squeeze and print
const size_t len = (out_len - i < sizeof(buf)) ? out_len - i : sizeof(buf);
turboshake256_squeeze(&ts, buf, len);
- print_hex(buf, len);
+ hex_write(stdout, buf, len);
}
fputs("\n", stdout);
@@ -115,7 +109,7 @@ static void do_k12(const uint8_t * const msg, const size_t msg_len, const size_t
// squeeze and print
const size_t len = (out_len - i < sizeof(buf)) ? out_len - i : sizeof(buf);
k12_squeeze(&k12, buf, len);
- print_hex(buf, len);
+ hex_write(stdout, buf, len);
}
fputs("\n", stdout);
@@ -237,7 +231,7 @@ int main(int argc, char *argv[]) {
// hash into buffer, print buffer, print newline
uint8_t buf[64];
fns[ofs].hash_func(msg, len, buf);
- print_hex(buf, fns[ofs].size);
+ hex_write(stdout, buf, fns[ofs].size);
fputs("\n", stdout);
}