aboutsummaryrefslogtreecommitdiffstats
path: root/anysum.c
blob: de85e46931ebb6933e7d12c442d1ec0249fcb7c3 (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* See LICENSE file for copyright and license details. */
#include "common.h"


static const char *default_algostrs[] = {
#if defined(SUPPORT_MD2) && 0 /* skipping: compromised is theory */
	"MD2",
#endif
#if defined(SUPPORT_MD4) && 0 /* skipping: compromised */
	"MD4",
#endif
#if defined(SUPPORT_MD5) && 0 /* skipping: compromised */
	"MD5",
#endif
#if defined(SUPPORT_RIPEMD_128) && 1
	"RIPEMD-128",
#endif
#if defined(SUPPORT_RIPEMD_160) && 1
	"RIPEMD-160",
#endif
#if defined(SUPPORT_RIPEMD_256) && 1
	"RIPEMD-256",
#endif
#if defined(SUPPORT_RIPEMD_320) && 1
	"RIPEMD-320",
#endif
#if defined(SUPPORT_SHA0) && 0 /* skipping: compromised */
	"SHA0",
#endif
#if defined(SUPPORT_SHA1) && 0 /* skipping: compromised */
	"SHA1",
#endif
#if defined(SUPPORT_SHA2) && 1
	"SHA-224",
	"SHA-256",
	"SHA-384",
	"SHA-512",
	"SHA-512/224",
	"SHA-512/256",
#endif
#if defined(SUPPORT_SHA3) && 1
	"SHA3-224",
	"SHA3-256",
	"SHA3-384",
	"SHA3-512",
#endif
#if defined(SUPPORT_KECCAK) && 1
	"Keccak-224",
	"Keccak-256",
	"Keccak-384",
	"Keccak-512",
#endif
#if defined(SUPPORT_RAWSHAKE) && 1
	"RawSHAKE128",
	"RawSHAKE256",
	"RawSHAKE512",
#endif
#if defined(SUPPORT_SHAKE) && 1
	"SHAKE128",
	"SHAKE256",
	"SHAKE512",
#endif
#if defined(SUPPORT_BLAKE224) && 1
	"BLAKE224",
#endif
#if defined(SUPPORT_BLAKE256) && 1
	"BLAKE256",
#endif
#if defined(SUPPORT_BLAKE384) && 1
	"BLAKE384",
#endif
#if defined(SUPPORT_BLAKE512) && 1
	"BLAKE512",
#endif
	NULL
};


int
main(int argc, char **argv)
{
	struct algorithm default_algorithms[ELEMSOF(default_algostrs)];
	struct config config = {.format = LOWERCASE_HEX | WITH_FILENAME | WITH_LF};
	char stdin_str[] = "-";
	char *stdin_array[] = {stdin_str, NULL};
	size_t i;
	int ret;

	libsimple_default_failure_exit = 2;
	cmdline(&argc, &argv, &config);
	if (!*argv)
		argv = stdin_array;

	if (config.verify) {
		ret = verify_checksums(argv, config.algorithms, config.nalgorithms, config.format,
		                       config.threads, config.warn_improper_format, config.hexinput);
	} else {
		if (!config.nalgorithms) {
			memset(default_algorithms, 0, sizeof(default_algorithms));
			for (i = 0; default_algostrs[i]; i++) {
				default_algorithms[i].algostr = default_algostrs[i];
				default_algorithms[i].result = NULL;
			}
			config.nalgorithms = i;
			if (!config.nalgorithms) {
				eprintf("not compiled to support any default hash functions");
			}
		}
		ret = calculate_and_print_each(argv, config.algorithms, config.nalgorithms, config.threads,
		                               config.format, config.hexinput, config.recursive, config.xdev,
		                               config.xlink);
	}

	for (i = 0; i < config.nalgorithms; i++)
		free(config.algorithms[i].result);
	free(config.algorithms);
	return ret;
}