aboutsummaryrefslogtreecommitdiffstats
path: root/libhashsum_init_hasher_from_string.c
blob: e41c8b11614e491827dd4afbb065252034691be9 (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
/* 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) {
		if (tolower(*a) == tolower(*b)) {
			a++;
			b++;
		} else if (*b == '-') {
			b++;
		} else {
			return 0;
		}
	}
	return !*a && !*b;
}


int
libhashsum_init_hasher_from_string(struct libhashsum_hasher *this, const char *algorithm)
{
	if (!strcasecmp(algorithm, "MD2"))
		return libhashsum_init_md2_hasher(this);
	if (!strcasecmp(algorithm, "MD4"))
		return libhashsum_init_md4_hasher(this);
	if (!strcasecmp(algorithm, "MD5"))
		return libhashsum_init_md5_hasher(this);
	if (equiv(algorithm, "RIPEMD-128"))
		return libhashsum_init_ripemd_128_hasher(this);
	if (equiv(algorithm, "RIPEMD-160"))
		return libhashsum_init_ripemd_160_hasher(this);
	if (equiv(algorithm, "RIPEMD-256"))
		return libhashsum_init_ripemd_256_hasher(this);
	if (equiv(algorithm, "RIPEMD-320"))
		return libhashsum_init_ripemd_320_hasher(this);
	if (equiv(algorithm, "SHA-0"))
		return libhashsum_init_sha0_hasher(this);
	if (equiv(algorithm, "SHA-1"))
		return libhashsum_init_sha1_hasher(this);
	if (equiv(algorithm, "SHA-224") || !strcasecmp(algorithm, "SHA2-224"))
		return libhashsum_init_sha_224_hasher(this);
	if (equiv(algorithm, "SHA-256") || !strcasecmp(algorithm, "SHA2-256"))
		return libhashsum_init_sha_256_hasher(this);
	if (equiv(algorithm, "SHA-384") || !strcasecmp(algorithm, "SHA2-384"))
		return libhashsum_init_sha_384_hasher(this);
	if (equiv(algorithm, "SHA-512") || !strcasecmp(algorithm, "SHA2-512"))
		return libhashsum_init_sha_512_hasher(this);
	if (equiv(algorithm, "SHA-512/224") || !strcasecmp(algorithm, "SHA2-512/224"))
		return libhashsum_init_sha_512_224_hasher(this);
	if (equiv(algorithm, "SHA-512/256") || !strcasecmp(algorithm, "SHA2-512/256"))
		return libhashsum_init_sha_512_256_hasher(this);

	errno = EINVAL;
	return -1;
}