aboutsummaryrefslogtreecommitdiffstats
path: root/libhashsum_get_algorithm_from_string.c
blob: 07d97e5949754dadb6e93c68376bfa24832eea15 (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
/* See LICENSE file for copyright and license details. */
#include "common.h"


#if defined(__GNUC__)
__attribute__((__pure__))
#endif
static int
equiv(const char *a, const char *b)
{
	while (*a && *b && *b != '[') {
		if (tolower(*a) == tolower(*b) || (*b == '/' && (*a == '-' || *a == '_'))) {
			a++;
			b++;
		} else if (*b == '-' && !isdigit(b[-1])) {
			b++;
		} else {
			return 0;
		}
	}
	if (!strncasecmp(a, "sum", 3))
		a = &a[3];
	if (*b == '[')
		return *a == '[' || !*a;
	return !*a && !*b;
}


int
libhashsum_get_algorithm_from_string(enum libhashsum_algorithm *algorithm_out, const char *algorithm)
{
	if (equiv(algorithm, "MD2"))
		*algorithm_out = LIBHASHSUM_MD2;
	else if (equiv(algorithm, "MD4"))
		*algorithm_out = LIBHASHSUM_MD4;
	else if (equiv(algorithm, "MD5"))
		*algorithm_out = LIBHASHSUM_MD5;
	else if (equiv(algorithm, "RIPEMD-128") || equiv(algorithm, "RMD-128"))
		*algorithm_out = LIBHASHSUM_RIPEMD_128;
	else if (equiv(algorithm, "RIPEMD-160") || equiv(algorithm, "RMD-160"))
		*algorithm_out = LIBHASHSUM_RIPEMD_160;
	else if (equiv(algorithm, "RIPEMD-256") || equiv(algorithm, "RMD-256"))
		*algorithm_out = LIBHASHSUM_RIPEMD_256;
	else if (equiv(algorithm, "RIPEMD-320") || equiv(algorithm, "RMD-320"))
		*algorithm_out = LIBHASHSUM_RIPEMD_320;
	else if (equiv(algorithm, "SHA-0"))
		*algorithm_out = LIBHASHSUM_SHA0;
	else if (equiv(algorithm, "SHA-1"))
		*algorithm_out = LIBHASHSUM_SHA1;
	else if (equiv(algorithm, "SHA-224") || equiv(algorithm, "SHA-2-224"))
		*algorithm_out = LIBHASHSUM_SHA_224;
	else if (equiv(algorithm, "SHA-256") || equiv(algorithm, "SHA-2-256"))
		*algorithm_out = LIBHASHSUM_SHA_256;
	else if (equiv(algorithm, "SHA-384") || equiv(algorithm, "SHA-2-384"))
		*algorithm_out = LIBHASHSUM_SHA_384;
	else if (equiv(algorithm, "SHA-512") || equiv(algorithm, "SHA-2-512"))
		*algorithm_out = LIBHASHSUM_SHA_512;
	else if (equiv(algorithm, "SHA-512/224") || equiv(algorithm, "SHA-2-512/224"))
		*algorithm_out = LIBHASHSUM_SHA_512_224;
	else if (equiv(algorithm, "SHA-512/256") || equiv(algorithm, "SHA-2-512/256"))
		*algorithm_out = LIBHASHSUM_SHA_512_256;
	else if (equiv(algorithm, "Keccak-224"))
		*algorithm_out = LIBHASHSUM_KECCAK_224;
	else if (equiv(algorithm, "Keccak-256"))
		*algorithm_out = LIBHASHSUM_KECCAK_256;
	else if (equiv(algorithm, "Keccak-384"))
		*algorithm_out = LIBHASHSUM_KECCAK_384;
	else if (equiv(algorithm, "Keccak-512"))
		*algorithm_out = LIBHASHSUM_KECCAK_512;
	else if (equiv(algorithm, "SHA-3-224"))
		*algorithm_out = LIBHASHSUM_SHA3_224;
	else if (equiv(algorithm, "SHA-3-256"))
		*algorithm_out = LIBHASHSUM_SHA3_256;
	else if (equiv(algorithm, "SHA-3-384"))
		*algorithm_out = LIBHASHSUM_SHA3_384;
	else if (equiv(algorithm, "SHA-3-512"))
		*algorithm_out = LIBHASHSUM_SHA3_512;
	else if (equiv(algorithm, "SHAKE-128[]"))
		*algorithm_out = LIBHASHSUM_SHAKE128;
	else if (equiv(algorithm, "SHAKE-256[]"))
		*algorithm_out = LIBHASHSUM_SHAKE256;
	else if (equiv(algorithm, "SHAKE-512[]"))
		*algorithm_out = LIBHASHSUM_SHAKE512;
	else if (equiv(algorithm, "RawSHAKE-128[]"))
		*algorithm_out = LIBHASHSUM_RAWSHAKE128;
	else if (equiv(algorithm, "RawSHAKE-256[]"))
		*algorithm_out = LIBHASHSUM_RAWSHAKE256;
	else if (equiv(algorithm, "RawSHAKE-512[]"))
		*algorithm_out = LIBHASHSUM_RAWSHAKE512;
	else if (equiv(algorithm, "Keccak[]"))
		*algorithm_out = LIBHASHSUM_KECCAK;
	else if (equiv(algorithm, "BLAKE-224[]") || equiv(algorithm, "B224[]"))
		*algorithm_out = LIBHASHSUM_BLAKE224;
	else if (equiv(algorithm, "BLAKE-256[]") || equiv(algorithm, "B256[]"))
		*algorithm_out = LIBHASHSUM_BLAKE256;
	else if (equiv(algorithm, "BLAKE-384[]") || equiv(algorithm, "B384[]"))
		*algorithm_out = LIBHASHSUM_BLAKE384;
	else if (equiv(algorithm, "BLAKE-512[]") || equiv(algorithm, "B512[]"))
		*algorithm_out = LIBHASHSUM_BLAKE512;
	else if (equiv(algorithm, "BLAKE-2s[]") || equiv(algorithm, "B2s[]"))
		*algorithm_out = LIBHASHSUM_BLAKE2S;
	else if (equiv(algorithm, "BLAKE-2b[]") || equiv(algorithm, "B2b[]"))
		*algorithm_out = LIBHASHSUM_BLAKE2B;
	else
		return 0;
	return 1;
}