aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--Makefile1
-rw-r--r--common.h1
-rw-r--r--libhashsum.h18
-rw-r--r--libhashsum_init_hasher.c32
-rw-r--r--libhashsum_init_hasher_from_string.c61
5 files changed, 97 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index eb9004c..039c15b 100644
--- a/Makefile
+++ b/Makefile
@@ -63,6 +63,7 @@ LDFLAGS_FULL =\
OBJ =\
libhashsum_init_hasher.o\
+ libhashsum_init_hasher_from_string.o\
libhashsum_init_md2_hasher.o\
libhashsum_init_md4_hasher.o\
libhashsum_init_md5_hasher.o\
diff --git a/common.h b/common.h
index 5bfac1e..2df927b 100644
--- a/common.h
+++ b/common.h
@@ -6,6 +6,7 @@
# pragma clang diagnostic ignored "-Wcovered-switch-default"
#endif
#include "libhashsum.h"
+#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
diff --git a/libhashsum.h b/libhashsum.h
index 203ec10..0e49083 100644
--- a/libhashsum.h
+++ b/libhashsum.h
@@ -12,9 +12,11 @@
#if defined(__GNUC__)
# define LIBHASHSUM_USERET_ __attribute__((__warn_unused_result__))
# define LIBHASHSUM_1_NONNULL_ __attribute__((__nonnull__(1)))
+# define LIBHASHSUM_NONNULL_ __attribute__((__nonnull__))
#else
# define LIBHASHSUM_USERET_
# define LIBHASHSUM_1_NONNULL_
+# define LIBHASHSUM_NONNULL_
#endif
@@ -402,6 +404,22 @@ LIBHASHSUM_1_NONNULL_
int libhashsum_init_hasher(struct libhashsum_hasher *this, enum libhashsum_algorithm algorithm);
/**
+ * Create an initialised state for a hash algorithm
+ * and return hash functions and details
+ *
+ * @param this The output parameter for the functions, details, and state
+ * @param algorithm The hashing algorithm and parameters
+ * @return 0 on success, -1 on failure
+ *
+ * @throws EINVAL `algorithm` is not recognised or contains an invalid value
+ * @throws ENOSYS Support for `algorithm` was excluded at compile time
+ *
+ * @since 1.0
+ */
+LIBHASHSUM_NONNULL_
+int libhashsum_init_hasher_from_string(struct libhashsum_hasher *this, const char *algorithm);
+
+/**
* Create an initialised state for MD2
* hashing and return hash functions and details
*
diff --git a/libhashsum_init_hasher.c b/libhashsum_init_hasher.c
index d747e52..3f99e6c 100644
--- a/libhashsum_init_hasher.c
+++ b/libhashsum_init_hasher.c
@@ -3,39 +3,39 @@
int
-libhashsum_init_hasher(struct libhashsum_hasher *hasher, enum libhashsum_algorithm algorithm)
+libhashsum_init_hasher(struct libhashsum_hasher *this, enum libhashsum_algorithm algorithm)
{
switch (algorithm) {
case LIBHASHSUM_MD2:
- return libhashsum_init_md2_hasher(hasher);
+ return libhashsum_init_md2_hasher(this);
case LIBHASHSUM_MD4:
- return libhashsum_init_md4_hasher(hasher);
+ return libhashsum_init_md4_hasher(this);
case LIBHASHSUM_MD5:
- return libhashsum_init_md5_hasher(hasher);
+ return libhashsum_init_md5_hasher(this);
case LIBHASHSUM_RIPEMD_128:
- return libhashsum_init_ripemd_128_hasher(hasher);
+ return libhashsum_init_ripemd_128_hasher(this);
case LIBHASHSUM_RIPEMD_160:
- return libhashsum_init_ripemd_160_hasher(hasher);
+ return libhashsum_init_ripemd_160_hasher(this);
case LIBHASHSUM_RIPEMD_256:
- return libhashsum_init_ripemd_256_hasher(hasher);
+ return libhashsum_init_ripemd_256_hasher(this);
case LIBHASHSUM_RIPEMD_320:
- return libhashsum_init_ripemd_320_hasher(hasher);
+ return libhashsum_init_ripemd_320_hasher(this);
case LIBHASHSUM_SHA0:
- return libhashsum_init_sha0_hasher(hasher);
+ return libhashsum_init_sha0_hasher(this);
case LIBHASHSUM_SHA1:
- return libhashsum_init_sha1_hasher(hasher);
+ return libhashsum_init_sha1_hasher(this);
case LIBHASHSUM_SHA_224:
- return libhashsum_init_sha_224_hasher(hasher);
+ return libhashsum_init_sha_224_hasher(this);
case LIBHASHSUM_SHA_256:
- return libhashsum_init_sha_256_hasher(hasher);
+ return libhashsum_init_sha_256_hasher(this);
case LIBHASHSUM_SHA_384:
- return libhashsum_init_sha_384_hasher(hasher);
+ return libhashsum_init_sha_384_hasher(this);
case LIBHASHSUM_SHA_512:
- return libhashsum_init_sha_512_hasher(hasher);
+ return libhashsum_init_sha_512_hasher(this);
case LIBHASHSUM_SHA_512_224:
- return libhashsum_init_sha_512_224_hasher(hasher);
+ return libhashsum_init_sha_512_224_hasher(this);
case LIBHASHSUM_SHA_512_256:
- return libhashsum_init_sha_512_256_hasher(hasher);
+ return libhashsum_init_sha_512_256_hasher(this);
default:
errno = EINVAL;
return -1;
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;
+}