aboutsummaryrefslogtreecommitdiff
path: root/hash-main.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-07-17 17:03:37 -0400
committerPaul Duncan <pabs@pablotron.org>2019-07-17 17:03:37 -0400
commit73100bf2aaa8a1bfc4b92f42a7461b99c587ef3d (patch)
tree4d1d988f6714e22e2b991e2f074a15e03667ac7f /hash-main.c
parent2c9390c10b2f31c87830e0816ca207974ebfc590 (diff)
downloadsha2-73100bf2aaa8a1bfc4b92f42a7461b99c587ef3d.tar.bz2
sha2-73100bf2aaa8a1bfc4b92f42a7461b99c587ef3d.zip
add hmac-sha2 and update files
Diffstat (limited to 'hash-main.c')
-rw-r--r--hash-main.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/hash-main.c b/hash-main.c
new file mode 100644
index 0000000..61a6940
--- /dev/null
+++ b/hash-main.c
@@ -0,0 +1,50 @@
+#include <stdio.h> // printf()
+#include <string.h> // strlen()
+#include "sha2.h"
+
+static void print_hash(const uint8_t * const hash) {
+ for (size_t i = 0; i < SHA256_HASH_SIZE; i++) {
+ printf("%02x", hash[i]);
+ }
+}
+
+static void print_row(
+ const char * const src,
+ const uint8_t * const hash
+) {
+ printf("\"%s\",", src);
+ print_hash(hash);
+ printf("\n");
+}
+
+static uint8_t dst[SHA256_HASH_SIZE];
+static uint8_t buf[1 << 21];
+
+int main(int argc, char *argv[]) {
+ // if command-line parameters are given, then treat them as a
+ // list of files: open each file, hash it, and and print the
+ // result instead of running the test vectors
+
+ for (int i = 1; i < argc; i++) {
+ sha256_t ctx;
+ sha256_init(&ctx);
+
+ FILE *fh = fopen(argv[i], "rb");
+ if (!fh) {
+ fprintf(stderr, "fopen(\"%s\") failed", argv[i]);
+ return 1;
+ }
+
+ size_t len = 0;
+ while ((len = fread(buf, 1, sizeof(buf), fh)) > 0) {
+ sha256_push(&ctx, buf, len);
+ }
+
+ fclose(fh);
+
+ sha256_fini(&ctx, dst);
+ print_row(argv[i], dst);
+ }
+
+ return 0;
+}