diff --git a/ChangeLog.adoc b/ChangeLog.adoc index fc8e0b61a..3fd6cee3a 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -22,6 +22,7 @@ New features:: * core: add variable "old_full_name" in buffer, set during buffer renaming (issue #1428) * core: add debug option "-d" in command /eval (issue #1434) + * api: add functions string_hash_binary and string_hash * api: add info "weechat_headless" (issue #1433) * buflist: add pointer "window" in bar item evaluation * relay: reject client with weechat protocol if password or totp is received in init command but not set in WeeChat (issue #1435) diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc index 429032e62..622cef607 100644 --- a/doc/en/weechat_plugin_api.en.adoc +++ b/doc/en/weechat_plugin_api.en.adoc @@ -2061,6 +2061,116 @@ char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL); [NOTE] This function is not available in scripting API. +==== string_hash_binary + +_WeeChat ≥ 2.8._ + +Compute hash of data. + +Prototype: + +[source,C] +---- +void string_hash_binary (const char *data, int length_data, const char *hash_algo, + char **hash, int *length_hash); +---- + +Arguments: + +* _data_: the data to hash +* _length_data_: number of bytes to hash in _data_ +* _hash_algo_: the hash algorithm, see table below +* _hash_: pointer to the hash variable, which is allocated by the function and + used to store the resulting hash (NULL if error) +* _length_hash_: pointer to a variable used to store the length of the hash + computed (in bytes) (0 if error) + +Supported hash algorithms: + +[width="100%",cols="4,4,4,5,12",options="header"] +|=== +| Value | Algorithm | Hash size | Output (binary) | Notes +| `+md5+` | MD5 | 128 bits | 16 bytes | *Weak*, not recommended for cryptography usage. +| `+sha1+` | SHA-1 | 160 bits | 20 bytes | *Weak*, not recommended for cryptography usage. +| `+sha224+` | SHA-224 | 224 bits | 28 bytes | +| `+sha256+` | SHA-256 | 256 bits | 32 bytes | +| `+sha384+` | SHA-384 | 384 bits | 48 bytes | +| `+sha512+` | SHA-512 | 512 bits | 64 bytes | +| `+sha3-224+` | SHA3-224 | 224 bits | 28 bytes | +| `+sha3-256+` | SHA3-256 | 256 bits | 32 bytes | +| `+sha3-384+` | SHA3-384 | 384 bits | 48 bytes | +| `+sha3-512+` | SHA3-512 | 512 bits | 64 bytes | +|=== + +C example: + +[source,C] +---- +const char *data = "abcdefghijklmnopqrstuvwxyz"; +char *hash; +int length_hash; +weechat_string_hash_binary (data, strlen (data), "sha256", &hash, &length_hash); +/* hash is a binary buffer with: + 71 c4 80 df 93 d6 ae 2f 1e fa d1 44 7c 66 c9 52 5e 31 62 18 cf 51 fc 8d 9e d8 32 f2 da f1 8b 73 */ +---- + +[NOTE] +This function is not available in scripting API. + +==== string_hash + +_WeeChat ≥ 2.8._ + +Compute hash of data, as hexadecimal string. + +Prototype: + +[source,C] +---- +char *string_hash (const char *data, int length_data, const char *hash_algo); +---- + +Arguments: + +* _data_: the data to hash +* _length_data_: number of bytes to hash in _data_ +* _hash_algo_: the hash algorithm, see table below + +Supported hash algorithms: + +[width="100%",cols="4,4,4,5,12",options="header"] +|=== +| Value | Algorithm | Hash size | Output (string) | Notes +| `+md5+` | MD5 | 128 bits | 32 hex chars | *Weak*, not recommended for cryptography usage. +| `+sha1+` | SHA-1 | 160 bits | 40 hex chars | *Weak*, not recommended for cryptography usage. +| `+sha224+` | SHA-224 | 224 bits | 56 hex chars | +| `+sha256+` | SHA-256 | 256 bits | 64 hex chars | +| `+sha384+` | SHA-384 | 384 bits | 96 hex chars | +| `+sha512+` | SHA-512 | 512 bits | 128 hex chars | +| `+sha3-224+` | SHA3-224 | 224 bits | 56 hex chars | +| `+sha3-256+` | SHA3-256 | 256 bits | 64 hex chars | +| `+sha3-384+` | SHA3-384 | 384 bits | 96 hex chars | +| `+sha3-512+` | SHA3-512 | 512 bits | 128 hex chars | +|=== + +Return value: + +* string with hash of data as hexadecimal (must be freed by calling "free" + after use), NULL if error + +C example: + +[source,C] +---- +const char *data = "abcdefghijklmnopqrstuvwxyz"; +char *hash; +hash = weechat_string_hash (data, strlen (data), "sha256"); +/* hash == "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73" +---- + +[NOTE] +This function is not available in scripting API. + ==== string_is_command_char _WeeChat ≥ 0.3.2._ diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc index d27ee1a87..41189dfda 100644 --- a/doc/fr/weechat_plugin_api.fr.adoc +++ b/doc/fr/weechat_plugin_api.fr.adoc @@ -2099,6 +2099,116 @@ char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL); [NOTE] Cette fonction n'est pas disponible dans l'API script. +==== string_hash_binary + +_WeeChat ≥ 2.8._ + +Calculer le hachage des données. + +Prototype : + +[source,C] +---- +void string_hash_binary (const char *data, int length_data, const char *hash_algo, + char **hash, int *length_hash); +---- + +Paramètres : + +* _data_ : les données à hacher +* _length_data_ : nombre d'octets à hacher dans _data_ +* _hash_algo_ : l'algorithme de hachage, voir le tableau ci-dessous +* _hash_ : pointeur vers la variable de hachage, qui est allouée par la fonction + et utilisée pour stocker le résultat du hachage (NULL si erreur) +* _length_hash_ : pointeur vers une variable utiliser pour stocker la longueur + du résultat du hachage (en octets) (0 si erreur) + +Algorithmes de hachage supportés : + +[width="100%",cols="4,4,4,5,12",options="header"] +|=== +| Valeur | Algorithme | Taille du haché | Sortie (binaire) | Notes +| `+md5+` | MD5 | 128 bits | 16 octets | *Faible*, non recommandé pour un usage cryptographique. +| `+sha1+` | SHA-1 | 160 bits | 20 octets | *Faible*, non recommandé pour un usage cryptographique. +| `+sha224+` | SHA-224 | 224 bits | 28 octets | +| `+sha256+` | SHA-256 | 256 bits | 32 octets | +| `+sha384+` | SHA-384 | 384 bits | 48 octets | +| `+sha512+` | SHA-512 | 512 bits | 64 octets | +| `+sha3-224+` | SHA3-224 | 224 bits | 28 octets | +| `+sha3-256+` | SHA3-256 | 256 bits | 32 octets | +| `+sha3-384+` | SHA3-384 | 384 bits | 48 octets | +| `+sha3-512+` | SHA3-512 | 512 bits | 64 octets | +|=== + +Exemple en C : + +[source,C] +---- +const char *data = "abcdefghijklmnopqrstuvwxyz"; +char *hash; +int length_hash; +weechat_string_hash_binary (data, strlen (data), "sha256", &hash, &length_hash); +/* hash is a binary buffer with: + 71 c4 80 df 93 d6 ae 2f 1e fa d1 44 7c 66 c9 52 5e 31 62 18 cf 51 fc 8d 9e d8 32 f2 da f1 8b 73 */ +---- + +[NOTE] +Cette fonction n'est pas disponible dans l'API script. + +==== string_hash + +_WeeChat ≥ 2.8._ + +Calculer le hachage des données, sous forme de chaîne hexadécimale. + +Prototype : + +[source,C] +---- +char *string_hash (const char *data, int length_data, const char *hash_algo); +---- + +Paramètres : + +* _data_ : les données à hacher +* _length_data_ : nombre d'octets à hacher dans _data_ +* _hash_algo_ : l'algorithme de hachage, voir le tableau ci-dessous + +Algorithmes de hachage supportés : + +[width="100%",cols="4,4,4,5,12",options="header"] +|=== +| Valeur | Algorithme | Taille du haché | Sortie (chaîne) | Notes +| `+md5+` | MD5 | 128 bits | 32 caractères hexa | *Faible*, non recommandé pour un usage cryptographique. +| `+sha1+` | SHA-1 | 160 bits | 40 caractères hexa | *Faible*, non recommandé pour un usage cryptographique. +| `+sha224+` | SHA-224 | 224 bits | 56 caractères hexa | +| `+sha256+` | SHA-256 | 256 bits | 64 caractères hexa | +| `+sha384+` | SHA-384 | 384 bits | 96 caractères hexa | +| `+sha512+` | SHA-512 | 512 bits | 128 caractères hexa | +| `+sha3-224+` | SHA3-224 | 224 bits | 56 caractères hexa | +| `+sha3-256+` | SHA3-256 | 256 bits | 64 caractères hexa | +| `+sha3-384+` | SHA3-384 | 384 bits | 96 caractères hexa | +| `+sha3-512+` | SHA3-512 | 512 bits | 128 caractères hexa | +|=== + +Valeur de retour : + +* chaîne avec le résultat du hachage en hexadécimal (doit être supprimée par un + appel à "free" après utilisation), NULL si erreur + +Exemple en C : + +[source,C] +---- +const char *data = "abcdefghijklmnopqrstuvwxyz"; +char *hash; +hash = weechat_string_hash (data, strlen (data), "sha256"); +/* hash == "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73" +---- + +[NOTE] +Cette fonction n'est pas disponible dans l'API script. + ==== string_is_command_char _WeeChat ≥ 0.3.2._ diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc index 29cec3970..45325214f 100644 --- a/doc/it/weechat_plugin_api.it.adoc +++ b/doc/it/weechat_plugin_api.it.adoc @@ -2152,6 +2152,118 @@ char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL); [NOTE] Questa funzione non è disponibile nelle API per lo scripting. +// TRANSLATION MISSING +==== string_hash_binary + +_WeeChat ≥ 2.8._ + +Compute hash of data. + +Prototipo: + +[source,C] +---- +void string_hash_binary (const char *data, int length_data, const char *hash_algo, + char **hash, int *length_hash); +---- + +Argomenti: + +* _data_: the data to hash +* _length_data_: number of bytes to hash in _data_ +* _hash_algo_: the hash algorithm, see table below +* _hash_: pointer to the hash variable, which is allocated by the function and + used to store the resulting hash (NULL if error) +* _length_hash_: pointer to a variable used to store the length of the hash + computed (in bytes) (0 if error) + +Supported hash algorithms: + +[width="100%",cols="4,4,4,5,12",options="header"] +|=== +| Value | Algorithm | Hash size | Output (binary) | Notes +| `+md5+` | MD5 | 128 bits | 16 bytes | *Weak*, not recommended for cryptography usage. +| `+sha1+` | SHA-1 | 160 bits | 20 bytes | *Weak*, not recommended for cryptography usage. +| `+sha224+` | SHA-224 | 224 bits | 28 bytes | +| `+sha256+` | SHA-256 | 256 bits | 32 bytes | +| `+sha384+` | SHA-384 | 384 bits | 48 bytes | +| `+sha512+` | SHA-512 | 512 bits | 64 bytes | +| `+sha3-224+` | SHA3-224 | 224 bits | 28 bytes | +| `+sha3-256+` | SHA3-256 | 256 bits | 32 bytes | +| `+sha3-384+` | SHA3-384 | 384 bits | 48 bytes | +| `+sha3-512+` | SHA3-512 | 512 bits | 64 bytes | +|=== + +Esempio in C: + +[source,C] +---- +const char *data = "abcdefghijklmnopqrstuvwxyz"; +char *hash; +int length_hash; +weechat_string_hash_binary (data, strlen (data), "sha256", &hash, &length_hash); +/* hash is a binary buffer with: + 71 c4 80 df 93 d6 ae 2f 1e fa d1 44 7c 66 c9 52 5e 31 62 18 cf 51 fc 8d 9e d8 32 f2 da f1 8b 73 */ +---- + +[NOTE] +Questa funzione non è disponibile nelle API per lo scripting. + +// TRANSLATION MISSING +==== string_hash + +_WeeChat ≥ 2.8._ + +Compute hash of data, as hexadecimal string. + +Prototipo: + +[source,C] +---- +char *string_hash (const char *data, int length_data, const char *hash_algo); +---- + +Argomenti: + +* _data_: the data to hash +* _length_data_: number of bytes to hash in _data_ +* _hash_algo_: the hash algorithm, see table below + +Supported hash algorithms: + +[width="100%",cols="4,4,4,5,12",options="header"] +|=== +| Value | Algorithm | Hash size | Output (string) | Notes +| `+md5+` | MD5 | 128 bits | 32 hex chars | *Weak*, not recommended for cryptography usage. +| `+sha1+` | SHA-1 | 160 bits | 40 hex chars | *Weak*, not recommended for cryptography usage. +| `+sha224+` | SHA-224 | 224 bits | 56 hex chars | +| `+sha256+` | SHA-256 | 256 bits | 64 hex chars | +| `+sha384+` | SHA-384 | 384 bits | 96 hex chars | +| `+sha512+` | SHA-512 | 512 bits | 128 hex chars | +| `+sha3-224+` | SHA3-224 | 224 bits | 56 hex chars | +| `+sha3-256+` | SHA3-256 | 256 bits | 64 hex chars | +| `+sha3-384+` | SHA3-384 | 384 bits | 96 hex chars | +| `+sha3-512+` | SHA3-512 | 512 bits | 128 hex chars | +|=== + +Valore restituito: + +* string with hash of data as hexadecimal (must be freed by calling "free" + after use), NULL if error + +Esempio in C: + +[source,C] +---- +const char *data = "abcdefghijklmnopqrstuvwxyz"; +char *hash; +hash = weechat_string_hash (data, strlen (data), "sha256"); +/* hash == "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73" +---- + +[NOTE] +Questa funzione non è disponibile nelle API per lo scripting. + ==== string_is_command_char _WeeChat ≥ 0.3.2._ diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc index cd2c53b42..c8e69621c 100644 --- a/doc/ja/weechat_plugin_api.ja.adoc +++ b/doc/ja/weechat_plugin_api.ja.adoc @@ -2074,6 +2074,118 @@ char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL); [NOTE] スクリプト API ではこの関数を利用できません。 +// TRANSLATION MISSING +==== string_hash_binary + +_WeeChat バージョン 2.8 以上で利用可。_ + +Compute hash of data. + +プロトタイプ: + +[source,C] +---- +void string_hash_binary (const char *data, int length_data, const char *hash_algo, + char **hash, int *length_hash); +---- + +引数: + +* _data_: the data to hash +* _length_data_: number of bytes to hash in _data_ +* _hash_algo_: the hash algorithm, see table below +* _hash_: pointer to the hash variable, which is allocated by the function and + used to store the resulting hash (NULL if error) +* _length_hash_: pointer to a variable used to store the length of the hash + computed (in bytes) (0 if error) + +Supported hash algorithms: + +[width="100%",cols="4,4,4,5,12",options="header"] +|=== +| Value | Algorithm | Hash size | Output (binary) | Notes +| `+md5+` | MD5 | 128 bits | 16 bytes | *Weak*, not recommended for cryptography usage. +| `+sha1+` | SHA-1 | 160 bits | 20 bytes | *Weak*, not recommended for cryptography usage. +| `+sha224+` | SHA-224 | 224 bits | 28 bytes | +| `+sha256+` | SHA-256 | 256 bits | 32 bytes | +| `+sha384+` | SHA-384 | 384 bits | 48 bytes | +| `+sha512+` | SHA-512 | 512 bits | 64 bytes | +| `+sha3-224+` | SHA3-224 | 224 bits | 28 bytes | +| `+sha3-256+` | SHA3-256 | 256 bits | 32 bytes | +| `+sha3-384+` | SHA3-384 | 384 bits | 48 bytes | +| `+sha3-512+` | SHA3-512 | 512 bits | 64 bytes | +|=== + +C 言語での使用例: + +[source,C] +---- +const char *data = "abcdefghijklmnopqrstuvwxyz"; +char *hash; +int length_hash; +weechat_string_hash_binary (data, strlen (data), "sha256", &hash, &length_hash); +/* hash is a binary buffer with: + 71 c4 80 df 93 d6 ae 2f 1e fa d1 44 7c 66 c9 52 5e 31 62 18 cf 51 fc 8d 9e d8 32 f2 da f1 8b 73 */ +---- + +[NOTE] +スクリプト API ではこの関数を利用できません。 + +// TRANSLATION MISSING +==== string_hash + +_WeeChat バージョン 2.8 以上で利用可。_ + +Compute hash of data, as hexadecimal string. + +プロトタイプ: + +[source,C] +---- +char *string_hash (const char *data, int length_data, const char *hash_algo); +---- + +引数: + +* _data_: the data to hash +* _length_data_: number of bytes to hash in _data_ +* _hash_algo_: the hash algorithm, see table below + +Supported hash algorithms: + +[width="100%",cols="4,4,4,5,12",options="header"] +|=== +| Value | Algorithm | Hash size | Output (string) | Notes +| `+md5+` | MD5 | 128 bits | 32 hex chars | *Weak*, not recommended for cryptography usage. +| `+sha1+` | SHA-1 | 160 bits | 40 hex chars | *Weak*, not recommended for cryptography usage. +| `+sha224+` | SHA-224 | 224 bits | 56 hex chars | +| `+sha256+` | SHA-256 | 256 bits | 64 hex chars | +| `+sha384+` | SHA-384 | 384 bits | 96 hex chars | +| `+sha512+` | SHA-512 | 512 bits | 128 hex chars | +| `+sha3-224+` | SHA3-224 | 224 bits | 56 hex chars | +| `+sha3-256+` | SHA3-256 | 256 bits | 64 hex chars | +| `+sha3-384+` | SHA3-384 | 384 bits | 96 hex chars | +| `+sha3-512+` | SHA3-512 | 512 bits | 128 hex chars | +|=== + +戻り値: + +* string with hash of data as hexadecimal (must be freed by calling "free" + after use), NULL if error + +C 言語での使用例: + +[source,C] +---- +const char *data = "abcdefghijklmnopqrstuvwxyz"; +char *hash; +hash = weechat_string_hash (data, strlen (data), "sha256"); +/* hash == "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73" +---- + +[NOTE] +スクリプト API ではこの関数を利用できません。 + ==== string_is_command_char _WeeChat バージョン 0.3.2 以上で利用可。_ 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 #include #include +#include #ifdef HAVE_ICONV #include @@ -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. * 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, diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 5a7e2bd39..ff1c4d766 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -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; diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index fb8870e89..1b97783c2 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -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) \ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2af1450dd..0eec7c41e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -38,6 +38,7 @@ set(LIB_WEECHAT_UNIT_TESTS_CORE_SRC unit/core/test-core-url.cpp unit/core/test-core-utf8.cpp unit/core/test-core-util.cpp + unit/core/test-core.h unit/gui/test-gui-color.cpp unit/gui/test-gui-line.cpp unit/gui/test-gui-nick.cpp diff --git a/tests/Makefile.am b/tests/Makefile.am index 68e7c6765..fa5e596f3 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -35,6 +35,7 @@ lib_weechat_unit_tests_core_a_SOURCES = unit/test-plugins.cpp \ unit/core/test-core-url.cpp \ unit/core/test-core-utf8.cpp \ unit/core/test-core-util.cpp \ + unit/core/test-core.h \ unit/gui/test-gui-color.cpp \ unit/gui/test-gui-line.cpp \ unit/gui/test-gui-nick.cpp \ diff --git a/tests/unit/core/test-core-secure.cpp b/tests/unit/core/test-core-secure.cpp index cd8891515..2fe178b44 100644 --- a/tests/unit/core/test-core-secure.cpp +++ b/tests/unit/core/test-core-secure.cpp @@ -25,30 +25,10 @@ extern "C" { #include #include +#include "tests/unit/core/test-core.h" #include "src/core/wee-secure.h" #include "src/core/wee-string.h" -#define DATA_HASH "this is a test of hash function" -#define DATA_HASH_MD5 "1197d121af621ac6a63cb8ef6b5dfa30" -#define DATA_HASH_SHA1 "799d818061175b400dc5aaeb14b8d32cdef32ff0" -#define DATA_HASH_SHA224 "637d21f3ba3f4e9fa9fb889dc990b31a658cb37b4aefb5144" \ - "70b016d" -#define DATA_HASH_SHA256 "b9a4c3393dfac4330736684510378851e581c68add8eca841" \ - "10c31a33e694676" -#define DATA_HASH_SHA384 "42853280be9b8409eed265f272bd580e2fbd448b7c7e236c7" \ - "f37dafec7906d51d982dc84ec70a4733eca49d86ac19455" -#define DATA_HASH_SHA512 "4469190d4e0d1fdc0afb6f408d9873c89b8ce89cc4db79fe0" \ - "58255c55ad6821fa5e9bb068f9e578c8ae7cc825d85ff99c439d59e439bc589d95620a" \ - "1e6b8ae6e" -#define DATA_HASH_SHA3_224 "26432a3a4ea998790be43386b1de417f88be43146a4af98" \ - "2a9627d10" -#define DATA_HASH_SHA3_256 "226e3830306711cf653c1661765c304b37038e7457c35dd" \ - "14fca0f6a8ba1d2e3" -#define DATA_HASH_SHA3_384 "77bc16f89c102efc783ddeccc71862fe919b66e1aaa88bd" \ - "2ba5f0bbe604fcb86c68f0e401d5d553597366cdd400595ba" -#define DATA_HASH_SHA3_512 "31dfb5fc8f30ac7007acddc4fce562d408706833d0d2af2" \ - "e5f61a179099592927ff7d100e278406c7f98d42575001e26e153b135c21f7ef5b00c8" \ - "cef93ca048d" #define SECURE_PASSPHRASE "this_is_a_secret_passphrase" #define SECURE_PASSWORD "this_is_a_secret_password" #define TOTP_SECRET "secretpasswordbase32" diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp index 24801e29c..0c39e5204 100644 --- a/tests/unit/core/test-core-string.cpp +++ b/tests/unit/core/test-core-string.cpp @@ -32,6 +32,7 @@ extern "C" #include #include #include "tests/tests.h" +#include "tests/unit/core/test-core.h" #include "src/core/weechat.h" #include "src/core/wee-string.h" #include "src/core/wee-hashtable.h" @@ -107,6 +108,47 @@ extern "C" STRCMP_EQUAL(__result, str); \ free (str); +#define WEE_CHECK_HASH_BIN(__result, __buffer, __length, __hash_algo) \ + if (__result) \ + { \ + result_bin = (char *)malloc (4096); \ + length_bin = string_base16_decode (__result, \ + (char *)result_bin); \ + } \ + else \ + { \ + result_bin = NULL; \ + length_bin = 0; \ + } \ + string_hash_binary (__buffer, __length, __hash_algo, \ + &hash_bin, &length_hash_bin); \ + if (__result == NULL) \ + { \ + POINTERS_EQUAL(NULL, hash_bin); \ + } \ + else \ + { \ + MEMCMP_EQUAL(result_bin, hash_bin, length_hash_bin); \ + } \ + LONGS_EQUAL(length_bin, length_hash_bin); \ + if (result_bin) \ + free (result_bin); \ + if (hash_bin) \ + free (hash_bin); + +#define WEE_CHECK_HASH_HEX(__result, __buffer, __length, __hash_algo) \ + hash = string_hash (__buffer, __length, __hash_algo); \ + if (__result == NULL) \ + { \ + POINTERS_EQUAL(NULL, hash); \ + } \ + else \ + { \ + STRCMP_EQUAL(__result, hash); \ + } \ + if (hash) \ + free (hash); + extern struct t_hashtable *string_hashtable_shared; TEST_GROUP(CoreString) @@ -1910,6 +1952,63 @@ TEST(CoreString, Hex_dump) str); } +/* + * Tests functions: + * string_hash_binary + * string_hash + */ + +TEST(CoreString, Hash) +{ + const char *data = DATA_HASH; + char *result_bin, *hash_bin, *hash; + int length, length_bin, length_hash_bin; + + length = strlen (data); + + WEE_CHECK_HASH_BIN(NULL, NULL, 0, NULL); + WEE_CHECK_HASH_HEX(NULL, NULL, 0, NULL); + + WEE_CHECK_HASH_BIN(NULL, DATA_HASH, 0, NULL); + WEE_CHECK_HASH_HEX(NULL, DATA_HASH, 0, NULL); + + WEE_CHECK_HASH_BIN(NULL, DATA_HASH, length, NULL); + WEE_CHECK_HASH_HEX(NULL, DATA_HASH, length, NULL); + + WEE_CHECK_HASH_BIN(NULL, DATA_HASH, length, "not_an_algo"); + WEE_CHECK_HASH_HEX(NULL, DATA_HASH, length, "not_an_algo"); + + WEE_CHECK_HASH_BIN(DATA_HASH_MD5, data, length, "md5"); + WEE_CHECK_HASH_HEX(DATA_HASH_MD5, data, length, "md5"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA1, data, length, "sha1"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA1, data, length, "sha1"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA224, data, length, "sha224"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA224, data, length, "sha224"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA256, data, length, "sha256"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA256, data, length, "sha256"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA384, data, length, "sha384"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA384, data, length, "sha384"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA512, data, length, "sha512"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA512, data, length, "sha512"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_224, data, length, "sha3-224"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_224, data, length, "sha3-224"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_256, data, length, "sha3-256"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_256, data, length, "sha3-256"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_384, data, length, "sha3-384"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_384, data, length, "sha3-384"); + + WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_512, data, length, "sha3-512"); + WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_512, data, length, "sha3-512"); +} + /* * Tests functions: * string_is_command_char diff --git a/tests/unit/core/test-core.h b/tests/unit/core/test-core.h new file mode 100644 index 000000000..ace1e55aa --- /dev/null +++ b/tests/unit/core/test-core.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 Sébastien Helleu + * + * This file is part of WeeChat, the extensible chat client. + * + * WeeChat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WeeChat. If not, see . + */ + +#ifndef WEECHAT_TEST_UNIT_CORE_H +#define WEECHAT_TEST_UNIT_CORE_H + +#define DATA_HASH "this is a test of hash function" +#define DATA_HASH_MD5 "1197d121af621ac6a63cb8ef6b5dfa30" +#define DATA_HASH_SHA1 "799d818061175b400dc5aaeb14b8d32cdef32ff0" +#define DATA_HASH_SHA224 "637d21f3ba3f4e9fa9fb889dc990b31a658cb37b4aefb5144" \ + "70b016d" +#define DATA_HASH_SHA256 "b9a4c3393dfac4330736684510378851e581c68add8eca841" \ + "10c31a33e694676" +#define DATA_HASH_SHA384 "42853280be9b8409eed265f272bd580e2fbd448b7c7e236c7" \ + "f37dafec7906d51d982dc84ec70a4733eca49d86ac19455" +#define DATA_HASH_SHA512 "4469190d4e0d1fdc0afb6f408d9873c89b8ce89cc4db79fe0" \ + "58255c55ad6821fa5e9bb068f9e578c8ae7cc825d85ff99c439d59e439bc589d95620a" \ + "1e6b8ae6e" +#define DATA_HASH_SHA3_224 "26432a3a4ea998790be43386b1de417f88be43146a4af98" \ + "2a9627d10" +#define DATA_HASH_SHA3_256 "226e3830306711cf653c1661765c304b37038e7457c35dd" \ + "14fca0f6a8ba1d2e3" +#define DATA_HASH_SHA3_384 "77bc16f89c102efc783ddeccc71862fe919b66e1aaa88bd" \ + "2ba5f0bbe604fcb86c68f0e401d5d553597366cdd400595ba" +#define DATA_HASH_SHA3_512 "31dfb5fc8f30ac7007acddc4fce562d408706833d0d2af2" \ + "e5f61a179099592927ff7d100e278406c7f98d42575001e26e153b135c21f7ef5b00c8" \ + "cef93ca048d" + +#endif /* WEECHAT_TEST_UNIT_CORE_H */