summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2020-02-29 21:02:42 +0100
committerSébastien Helleu <flashcode@flashtux.org>2020-02-29 21:02:42 +0100
commit410a5b341f4b6246fcebdeab18251f85f3f1bda7 (patch)
treef8ca616cab500daad8efd149aa87d0f513b39b25 /src/core
parent7e808e2ef7e3e6eee0027502d8670157bd4983f9 (diff)
downloadweechat-410a5b341f4b6246fcebdeab18251f85f3f1bda7.zip
api: add functions string_hash_binary and string_hash
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-string.c88
-rw-r--r--src/core/wee-string.h5
2 files changed, 93 insertions, 0 deletions
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index 0d48d9b90..71154f631 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -34,6 +34,7 @@
#include <regex.h>
#include <wchar.h>
#include <stdint.h>
+#include <gcrypt.h>
#ifdef HAVE_ICONV
#include <iconv.h>
@@ -52,6 +53,7 @@
#include "wee-config.h"
#include "wee-eval.h"
#include "wee-hashtable.h"
+#include "wee-secure.h"
#include "wee-utf8.h"
#include "../gui/gui-chat.h"
#include "../gui/gui-color.h"
@@ -63,6 +65,20 @@
((c >= 'A') && (c <= 'F')) ? c - 'A' + 10 : \
c - '0')
+char *string_hash_algo_string[] = {
+ "md5",
+ "sha1",
+ "sha224", "sha256", "sha384", "sha512",
+ "sha3-224", "sha3-256", "sha3-384", "sha3-512",
+ NULL,
+};
+int string_hash_algo[] = {
+ GCRY_MD_MD5,
+ GCRY_MD_SHA1,
+ GCRY_MD_SHA224, GCRY_MD_SHA256, GCRY_MD_SHA384, GCRY_MD_SHA512,
+ GCRY_MD_SHA3_224, GCRY_MD_SHA3_256, GCRY_MD_SHA3_384, GCRY_MD_SHA3_512,
+};
+
struct t_hashtable *string_hashtable_shared = NULL;
@@ -3405,6 +3421,78 @@ end:
}
/*
+ * Returns the hash algorithm with the name, or GCRY_MD_NONE if not found.
+ */
+
+int
+string_get_hash_algo (const char *hash_algo)
+{
+ int i;
+
+ if (!hash_algo)
+ return GCRY_MD_NONE;
+
+ for (i = 0; string_hash_algo_string[i]; i++)
+ {
+ if (strcmp (string_hash_algo_string[i], hash_algo) == 0)
+ return string_hash_algo[i];
+ }
+
+ return GCRY_MD_NONE;
+}
+
+/*
+ * Computes hash data, as binary buffer.
+ *
+ * Note: "*hash" must be freed after use.
+ */
+
+void
+string_hash_binary (const char *data, int length_data, const char *hash_algo,
+ char **hash, int *length_hash)
+{
+ int algo;
+
+ if (!hash || !length_hash)
+ return;
+
+ *hash = NULL;
+ *length_hash = 0;
+
+ if (!data || (length_data < 1) || !hash_algo)
+ return;
+
+ algo = string_get_hash_algo (hash_algo);
+ if (algo == GCRY_MD_NONE)
+ return;
+
+ secure_hash_binary (data, length_data, algo, hash, length_hash);
+}
+
+/*
+ * Computes hash of a buffer, as text (string with hexadecimal).
+ *
+ * Returns a string with the hash as hexadecimal, NULL if error.
+ *
+ * Note: result must be freed after use.
+ */
+
+char *
+string_hash (const char *data, int length_data, const char *hash_algo)
+{
+ int algo;
+
+ if (!data || (length_data < 1) || !hash_algo)
+ return NULL;
+
+ algo = string_get_hash_algo (hash_algo);
+ if (algo == GCRY_MD_NONE)
+ return NULL;
+
+ return secure_hash (data, length_data, algo);
+}
+
+/*
* Checks if a string is a command.
*
* Returns:
diff --git a/src/core/wee-string.h b/src/core/wee-string.h
index 585cb399d..84d78b4a9 100644
--- a/src/core/wee-string.h
+++ b/src/core/wee-string.h
@@ -118,6 +118,11 @@ extern int string_base64_decode (const char *from, char *to);
extern char *string_hex_dump (const char *data, int data_size,
int bytes_per_line,
const char *prefix, const char *suffix);
+extern void string_hash_binary (const char *data, int length_data,
+ const char *hash_algo,
+ char **hash, int *length_hash);
+extern char *string_hash (const char *data, int length_data,
+ const char *hash_algo);
extern int string_is_command_char (const char *string);
extern const char *string_input_for_buffer (const char *string);
extern char *string_replace_with_callback (const char *string,