summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2023-09-01 16:45:37 -0400
committerPaul Duncan <pabs@pablotron.org>2023-09-01 16:45:37 -0400
commitc7f630ed23144939a540219787c7be3ffe14b24c (patch)
treed8952a4b64c61e92fe552c54ea26e0d5b6d99a34 /main.c
downloadsha3-c7f630ed23144939a540219787c7be3ffe14b24c.tar.bz2
sha3-c7f630ed23144939a540219787c7be3ffe14b24c.zip
initial commit
Diffstat (limited to 'main.c')
-rw-r--r--main.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..62fc21b
--- /dev/null
+++ b/main.c
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <string.h>
+#include "sha3.h"
+
+// available functions
+static const struct {
+ const char *name;
+ const size_t size;
+ void (*func)(const uint8_t *, size_t, uint8_t *);
+} fns[] = {{
+ .name = "sha3-224",
+ .size = 28,
+ .func = sha3_224,
+}, {
+ .name = "sha3-256",
+ .size = 32,
+ .func = sha3_256,
+}, {
+ .name = "sha3-384",
+ .size = 48,
+ .func = sha3_384,
+}, {
+ .name = "sha3-512",
+ .size = 64,
+ .func = sha3_512,
+}, {
+ .name = "shake128",
+ .size = 16,
+ .func = shake128,
+}, {
+ .name = "shake256",
+ .size = 32,
+ .func = shake256,
+}};
+
+#define USAGE "Usage: %s <algo> <data>\n" \
+ "\n" \
+ "Algorithms:\n" \
+ "- sha3-224\n" \
+ "- sha3-256\n" \
+ "- sha3-384\n" \
+ "- sha3-512\n" \
+ "- shake128\n" \
+ "- shake256\n" \
+ "\n" \
+ "Example:\n" \
+ " %s sha3-256 \"asdf\"\n" \
+ " dd2781f4c51bccdbe23e4d398b8a82261f585c278dbb4b84989fea70e76723a9\n"
+
+int main(int argc, char *argv[]) {
+ if (argc < 3) {
+ const char *app = (argc > 0) ? argv[0] : "sha3";
+ fprintf(stderr, USAGE, app, app);
+ return -1;
+ }
+
+ for (size_t i = 0; i < sizeof(fns)/sizeof(fns[0]); i++) {
+ if (strncmp(argv[1], fns[i].name, strlen(fns[i].name) + 1)) {
+ continue;
+ }
+
+ // hash into buf
+ uint8_t buf[64];
+ fns[i].func((uint8_t*) argv[2], strlen(argv[2]), buf);
+
+ // print result
+ for (size_t j = 0; j < fns[i].size; j++) {
+ printf("%02x", buf[j]);
+ }
+ fputs("\n", stdout);
+
+ // exit with success
+ return 0;
+ }
+
+ fprintf(stderr, "Unknown algorithm: %s", argv[1]);
+ return -1;
+}