From d245b30a36301ec7344aa193f29d4244e5511452 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 16 Jul 2019 18:09:06 -0400 Subject: working --- main.c | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 5ef63fc..3540a32 100644 --- a/main.c +++ b/main.c @@ -1,20 +1,53 @@ #include // printf() #include // strlen() #include "sha256.h" +#include "tests.h" -static const char DEFAULT[] = "The quick brown fox jumps over the lazy dog"; +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]); + } +} + +static void print_row( + const char * const src, + const uint8_t * const hash +) { + printf("\"%s\",", src); + print_hash(hash); + printf("\n"); +} + +static void on_test_fail( + const char * const src, + const uint8_t * const got_hash, + const uint8_t * const expected_hash +) { + printf("\"%s\",", src); + print_hash(got_hash); + printf(","); + print_hash(expected_hash); + printf("\n"); +} int main(int argc, char *argv[]) { - const char *src = (argc > 1) ? argv[1] : DEFAULT; - uint8_t dst[SHA256_HASH_SIZE]; + if (argc > 1) { + // if command-line parameters are given, then hash and print them + // instead of running the test vectors - sha256((const uint8_t *) src, strlen(src), dst); + for (int i = 1; i < argc; i++) { + const char * const src = argv[i]; - printf("src = \"%s\"\nhash = \"", src); - for (size_t i = 0; i < 32; i++) { - printf("%02x", dst[i]); + sha256((const uint8_t *) src, strlen(src), dst); + print_row(src, dst); + } + } else { + // no command-line parameters given. run internal tests + printf("input,result,expected\n"); + run_tests(on_test_fail); } - printf("\"\n"); return 0; } -- cgit v1.2.3