aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: a5c14dae4ca77ebb47c31113d6b635d72712f6df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h> // printf()
#include <string.h> // strlen()
#include "sha2.h"
#include "tests.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 void on_test_fail(
  const int algo,
  const char * const src,
  const uint8_t * const got_hash,
  const uint8_t * const expected_hash
) {
  printf("sha%d,\"%s\",", algo, src);
  print_hash(got_hash);
  printf(",");
  print_hash(expected_hash);
  printf("\n");
}

static uint8_t dst[SHA256_HASH_SIZE];
static uint8_t buf[1 << 21];

int main(int argc, char *argv[]) {
  if (argc > 1) {
    // 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);
    }
  } else {
    // no command-line parameters given.  run internal tests
    printf("algo,input,result,expected\n");
    run_tests(on_test_fail);
  }

  return 0;
}