1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-01 07:16:37 +02:00

api: add functions string_hash_binary and string_hash

This commit is contained in:
Sébastien Helleu
2020-02-29 21:02:42 +01:00
parent 7e808e2ef7
commit 410a5b341f
14 changed files with 700 additions and 22 deletions
+88
View File
@@ -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;
@@ -3404,6 +3420,78 @@ end:
return buf;
}
/*
* 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.
*
+5
View File
@@ -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,
+2
View File
@@ -623,6 +623,8 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv)
new_plugin->string_base_encode = &plugin_api_string_base_encode;
new_plugin->string_base_decode = &plugin_api_string_base_decode;
new_plugin->string_hex_dump = &string_hex_dump;
new_plugin->string_hash_binary = &string_hash_binary;
new_plugin->string_hash = &string_hash;
new_plugin->string_is_command_char = &string_is_command_char;
new_plugin->string_input_for_buffer = &string_input_for_buffer;
new_plugin->string_eval_expression = &eval_expression;
+13 -1
View File
@@ -67,7 +67,7 @@ struct timeval;
* please change the date with current one; for a second change at same
* date, increment the 01, otherwise please keep 01.
*/
#define WEECHAT_PLUGIN_API_VERSION "20190810-01"
#define WEECHAT_PLUGIN_API_VERSION "20200229-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@@ -341,6 +341,11 @@ struct t_weechat_plugin
char *(*string_hex_dump) (const char *data, int data_size,
int bytes_per_line, const char *prefix,
const char *suffix);
void (*string_hash_binary) (const char *data, int length_data,
const char *hash_algo,
char **hash, int *length_hash);
char *(*string_hash) (const char *data, int length_data,
const char *hash_algo);
int (*string_is_command_char) (const char *string);
const char *(*string_input_for_buffer) (const char *string);
char *(*string_eval_expression )(const char *expr,
@@ -1254,6 +1259,13 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
(weechat_plugin->string_hex_dump)(__data, __data_size, \
__bytes_per_line, __prefix, \
__suffix)
#define weechat_string_hash_binary(__data, __length_data, __hash_algo, \
__hash, __length_hash) \
(weechat_plugin->string_hash_binary)(__data, __length_data, \
__hash_algo, \
__hash, __length_hash)
#define weechat_string_hash(__data, __length_data, __hash_algo) \
(weechat_plugin->string_hash)(__data, __length_data, __hash_algo)
#define weechat_string_is_command_char(__string) \
(weechat_plugin->string_is_command_char)(__string)
#define weechat_string_input_for_buffer(__string) \