aboutsummaryrefslogtreecommitdiffstats
path: root/libhashsum_init_hasher_from_string.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-08-24 11:52:30 +0200
committerMattias Andrée <maandree@kth.se>2024-08-24 11:52:30 +0200
commitba0bbce290c3742843fb2e8a693d9060e71eeefb (patch)
tree83950d23e99a4ecaac2801ffedf87e52e3d77007 /libhashsum_init_hasher_from_string.c
parentAdd libhashsum_hasher.algorithm_string (diff)
downloadlibhashsum-ba0bbce290c3742843fb2e8a693d9060e71eeefb.tar.gz
libhashsum-ba0bbce290c3742843fb2e8a693d9060e71eeefb.tar.bz2
libhashsum-ba0bbce290c3742843fb2e8a693d9060e71eeefb.tar.xz
Add libhashsum_init_hasher_from_string + m
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libhashsum_init_hasher_from_string.c')
-rw-r--r--libhashsum_init_hasher_from_string.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/libhashsum_init_hasher_from_string.c b/libhashsum_init_hasher_from_string.c
new file mode 100644
index 0000000..e41c8b1
--- /dev/null
+++ b/libhashsum_init_hasher_from_string.c
@@ -0,0 +1,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;
+}