aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-07-16 20:24:47 -0400
committerPaul Duncan <pabs@pablotron.org>2019-07-16 20:24:47 -0400
commitae8484372cbbf0bad041f3bd9b11c18d9cf37be7 (patch)
treebd73b1e0ee6fb9e3b98c59cdd9de1689ffad93b8 /main.c
parentd245b30a36301ec7344aa193f29d4244e5511452 (diff)
downloadsha2-ae8484372cbbf0bad041f3bd9b11c18d9cf37be7.tar.bz2
sha2-ae8484372cbbf0bad041f3bd9b11c18d9cf37be7.zip
make faster
Diffstat (limited to 'main.c')
-rw-r--r--main.c30
1 files changed, 23 insertions, 7 deletions
diff --git a/main.c b/main.c
index 3540a32..7c795ef 100644
--- a/main.c
+++ b/main.c
@@ -3,8 +3,6 @@
#include "sha256.h"
#include "tests.h"
-static uint8_t dst[SHA256_HASH_SIZE];
-
static void print_hash(const uint8_t * const hash) {
for (size_t i = 0; i < SHA256_HASH_SIZE; i++) {
printf("%02x", hash[i]);
@@ -32,16 +30,34 @@ static void on_test_fail(
printf("\n");
}
+static uint8_t dst[SHA256_HASH_SIZE];
+static uint8_t buf[1 << 20];
+
int main(int argc, char *argv[]) {
if (argc > 1) {
- // if command-line parameters are given, then hash and print them
- // instead of running the test vectors
+ // 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++) {
- const char * const src = argv[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((const uint8_t *) src, strlen(src), dst);
- print_row(src, dst);
+ sha256_fini(&ctx, dst);
+ print_row(argv[i], dst);
}
} else {
// no command-line parameters given. run internal tests