aboutsummaryrefslogblamecommitdiffstats
path: root/libhashsum_init_hasher_from_string.c
blob: e41c8b11614e491827dd4afbb065252034691be9 (plain) (tree)




























































                                                                                         
/* 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;
}