-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha3-test.c
86 lines (72 loc) · 2.21 KB
/
sha3-test.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "sha3.h"
#include "util.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define OPTIONS "hvl:i:o:"
void print_usage(char **argv) {
fprintf(stderr,
"SYNOPSIS\n"
" Hashes inputs using the SHA-3 256 algorithm.\n"
"\n"
"USAGE\n"
" %s [-hv] -l length [-i input] [-o output]\n"
"\n"
"OPTIONS\n"
" -h Display program help and usage\n"
" -v Display verbose program output\n"
" -l length Length of input in bytes\n"
" -i input Specify input to hash (stdin by default)\n"
" -o output Specify output of hashed input (stdout by default)\n",
argv[0]);
}
void hash_file(int64_t length, FILE *infile, FILE *outfile) {
uint8_t *msg = (uint8_t *)calloc(length ? length : 1, sizeof(uint8_t));
check(msg, "Failed to allocate array for message.\n");
output("Len = %" PRId64 "\n", length);
fread(msg, sizeof(uint8_t), length, infile);
output("Msg = ");
hexprint(length, msg);
uint8_t md[SHA3_256_MD_LEN] = { 0 };
sha3_256_digest(msg, length, md);
output("MD = ");
hexprint(SHA3_256_MD_LEN, md);
fwrite(md, sizeof(uint8_t), SHA3_256_MD_LEN, outfile);
free(msg);
}
int main(int argc, char **argv) {
int opt = 0;
int64_t length = -1;
FILE *infile = stdin;
FILE *outfile = stdout;
while ((opt = getopt(argc, argv, OPTIONS)) != -1) {
switch (opt) {
case 'h':
print_usage(argv);
return 0;
case 'v':
verbose = true;
break;
case 'l':
length = (int64_t)strtoll(optarg, NULL, 10);
break;
case 'i':
infile = fopen(optarg, "rb");
check(infile, "Failed to open %s.\n", optarg);
break;
case 'o':
outfile = fopen(optarg, "wb");
check(outfile, "Failed to open %s.\n", optarg);
break;
default:
print_usage(argv);
return 1;
}
}
check(length >= 0, "Valid message length must be supplied.\n");
hash_file(length, infile, outfile);
fclose(infile);
fclose(outfile);
return 0;
}