aboutsummaryrefslogtreecommitdiffstats
path: root/libhashsum_get_algorithm_from_string.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-08-30 17:20:30 +0200
committerMattias Andrée <maandree@kth.se>2024-08-30 17:20:30 +0200
commit87437d71b36518dac5086f8cc92480935e4cf60b (patch)
tree8cabd9b8871320216d371aa7c0e8774fd06d91f4 /libhashsum_get_algorithm_from_string.c
parentm + add support for z parameter for keccak (diff)
downloadlibhashsum-87437d71b36518dac5086f8cc92480935e4cf60b.tar.gz
libhashsum-87437d71b36518dac5086f8cc92480935e4cf60b.tar.bz2
libhashsum-87437d71b36518dac5086f8cc92480935e4cf60b.tar.xz
m + add man pages
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libhashsum_get_algorithm_from_string.c')
-rw-r--r--libhashsum_get_algorithm_from_string.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/libhashsum_get_algorithm_from_string.c b/libhashsum_get_algorithm_from_string.c
new file mode 100644
index 0000000..96892e1
--- /dev/null
+++ b/libhashsum_get_algorithm_from_string.c
@@ -0,0 +1,101 @@
+/* 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];
+ return !*a && (!*b || *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
+ return 0;
+ return 1;
+}