aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-07-16 18:09:06 -0400
committerPaul Duncan <pabs@pablotron.org>2019-07-16 18:09:06 -0400
commitd245b30a36301ec7344aa193f29d4244e5511452 (patch)
tree83a1f6fa2acf227d658c5f2de96d1e41eb7ad00e /main.c
parentb1089d91ec8a7acef32ab588917680e77862617e (diff)
downloadsha2-d245b30a36301ec7344aa193f29d4244e5511452.tar.bz2
sha2-d245b30a36301ec7344aa193f29d4244e5511452.zip
working
Diffstat (limited to 'main.c')
-rw-r--r--main.c49
1 files changed, 41 insertions, 8 deletions
diff --git a/main.c b/main.c
index 5ef63fc..3540a32 100644
--- a/main.c
+++ b/main.c
@@ -1,20 +1,53 @@
#include <stdio.h> // printf()
#include <string.h> // 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;
}