aboutsummaryrefslogblamecommitdiffstats
path: root/libhashsum_get_algorithm_from_string.c
blob: 07d97e5949754dadb6e93c68376bfa24832eea15 (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 && *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;
}