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
|
#include <stdio.h>
#include <string.h>
#include "sha3.h"
// available functions
static const struct {
const char *name;
const size_t size;
void (*func)(const uint8_t *, size_t, uint8_t *);
} fns[] = {{
.name = "sha3-224",
.size = 28,
.func = sha3_224,
}, {
.name = "sha3-256",
.size = 32,
.func = sha3_256,
}, {
.name = "sha3-384",
.size = 48,
.func = sha3_384,
}, {
.name = "sha3-512",
.size = 64,
.func = sha3_512,
}, {
.name = "shake128",
.size = 16,
.func = shake128,
}, {
.name = "shake256",
.size = 32,
.func = shake256,
}};
#define USAGE "Usage: %s <algo> <data>\n" \
"\n" \
"Algorithms:\n" \
"- sha3-224\n" \
"- sha3-256\n" \
"- sha3-384\n" \
"- sha3-512\n" \
"- shake128\n" \
"- shake256\n" \
"\n" \
"Example:\n" \
" %s sha3-256 \"asdf\"\n" \
" dd2781f4c51bccdbe23e4d398b8a82261f585c278dbb4b84989fea70e76723a9\n"
int main(int argc, char *argv[]) {
if (argc < 3) {
const char *app = (argc > 0) ? argv[0] : "sha3";
fprintf(stderr, USAGE, app, app);
return -1;
}
for (size_t i = 0; i < sizeof(fns)/sizeof(fns[0]); i++) {
if (strncmp(argv[1], fns[i].name, strlen(fns[i].name) + 1)) {
continue;
}
// hash into buf
uint8_t buf[64];
fns[i].func((uint8_t*) argv[2], strlen(argv[2]), buf);
// print result
for (size_t j = 0; j < fns[i].size; j++) {
printf("%02x", buf[j]);
}
fputs("\n", stdout);
// exit with success
return 0;
}
fprintf(stderr, "Unknown algorithm: %s", argv[1]);
return -1;
}
|