core: merge functions string_hash_binary and string_hash into a single function string_hash

v2.8-utf8proc
Sébastien Helleu 2020-03-01 16:41:28 +01:00
parent 1ae2591458
commit c4ef3d6c2e
15 changed files with 305 additions and 675 deletions

View File

@ -22,7 +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 function 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)

View File

@ -2061,7 +2061,7 @@ char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL);
[NOTE]
This function is not available in scripting API.
==== string_hash_binary
==== string_hash
_WeeChat ≥ 2.8._
@ -2071,103 +2071,51 @@ Prototype:
[source,C]
----
void string_hash_binary (const char *data, int length_data, const char *hash_algo,
char **hash, int *length_hash);
int string_hash (const void *data, int data_size, const char *hash_algo, void *hash, int *hash_size);
----
Arguments:
* _data_: the data to hash
* _length_data_: number of bytes to hash in _data_
* _data_size_: 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)
* _hash_: pointer to the hash variable, which is used to store the resulting hash
(the buffer must be large enough, according to the algorithm, see table below)
* _hash_size_: pointer to a variable used to store the size of the hash computed
(in bytes) (can be NULL)
Supported hash algorithms:
[width="100%",cols="4,4,4,5,12",options="header"]
[width="100%",cols="2,2,3,6",options="header"]
|===
| Value | Algorithm | Hash size | Output (binary) | Notes
| `+crc32+` | CRC32 | 32 bits | 4 bytes | Not a hash algorithm in the cryptographic sense.
| `+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
| `+crc32+` | CRC32 | 32 bits | 8 hex chars | Not a hash algorithm in the cryptographic sense.
| `+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 |
| Value | Algorithm | Hash size | Notes
| `+crc32+` | CRC32 | 4 bytes (32 bits) | Not a hash algorithm in the cryptographic sense.
| `+md5+` | MD5 | 16 bytes (128 bits) | *Weak*, not recommended for cryptography usage.
| `+sha1+` | SHA-1 | 20 bytes (160 bits) | *Weak*, not recommended for cryptography usage.
| `+sha224+` | SHA-224 | 28 bytes (224 bits) |
| `+sha256+` | SHA-256 | 32 bytes (256 bits) |
| `+sha384+` | SHA-384 | 48 bytes (384 bits) |
| `+sha512+` | SHA-512 | 64 bytes (512 bits) |
| `+sha3-224+` | SHA3-224 | 28 bytes (224 bits) |
| `+sha3-256+` | SHA3-256 | 32 bytes (256 bits) |
| `+sha3-384+` | SHA3-384 | 48 bytes (384 bits) |
| `+sha3-512+` | SHA3-512 | 64 bytes (512 bits) |
|===
Return value:
* string with hash of data as hexadecimal (must be freed by calling "free"
after use), NULL if error
* 1 if OK, 0 if error
C example:
[source,C]
----
const char *data = "abcdefghijklmnopqrstuvwxyz";
char *hash;
hash = weechat_string_hash (data, strlen (data), "sha256");
/* hash == "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73"
char hash[256 / 8];
int rc, hash_size;
rc = weechat_string_hash (data, strlen (data), "sha256", hash, &hash_size);
/* rc == 1, hash_size == 32 and hash is a 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]

View File

@ -2099,7 +2099,7 @@ 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
==== string_hash
_WeeChat ≥ 2.8._
@ -2109,103 +2109,52 @@ Prototype :
[source,C]
----
void string_hash_binary (const char *data, int length_data, const char *hash_algo,
char **hash, int *length_hash);
int string_hash (const void *data, int data_size, const char *hash_algo, void *hash, int *hash_size);
----
Paramètres :
* _data_ : les données à hacher
* _length_data_ : nombre d'octets à hacher dans _data_
* _data_size_ : 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)
* _hash_ : pointeur vers la variable de hachage, qui est utilisée pour stocker
le résultat du hachage (le tampon doit être suffisamment grand, selon
l'algorithme, voir le tableau ci-dessous)
* _hash_size_ : pointeur vers une variable utiliser pour stocker la longueur
du résultat du hachage (en octets) (peut être NULL)
Algorithmes de hachage supportés :
[width="100%",cols="4,4,4,5,12",options="header"]
[width="100%",cols="2,2,3,6",options="header"]
|===
| Valeur | Algorithme | Taille du haché | Sortie (binaire) | Notes
| `+crc32+` | CRC32 | 32 bits | 4 octets | Pas un algorithme de hachage au sens cryptographique.
| `+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
| `+crc32+` | CRC32 | 32 bits | 8 caractères hexa | Pas un algorithme de hachage au sens cryptographique.
| `+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 | Algorithme | Taille du haché | Notes
| `+crc32+` | CRC32 | 4 octets (32 bits) | Pas un algorithme de hachage au sens cryptographique.
| `+md5+` | MD5 | 16 octets (128 bits) | *Faible*, non recommandé pour un usage cryptographique.
| `+sha1+` | SHA-1 | 20 octets (160 bits) | *Faible*, non recommandé pour un usage cryptographique.
| `+sha224+` | SHA-224 | 28 octets (224 bits) |
| `+sha256+` | SHA-256 | 32 octets (256 bits) |
| `+sha384+` | SHA-384 | 48 octets (384 bits) |
| `+sha512+` | SHA-512 | 64 octets (512 bits) |
| `+sha3-224+` | SHA3-224 | 28 octets (224 bits) |
| `+sha3-256+` | SHA3-256 | 32 octets (256 bits) |
| `+sha3-384+` | SHA3-384 | 48 octets (384 bits) |
| `+sha3-512+` | SHA3-512 | 64 octets (512 bits) |
|===
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
* 1 si OK, 0 si erreur
Exemple en C :
[source,C]
----
const char *data = "abcdefghijklmnopqrstuvwxyz";
char *hash;
hash = weechat_string_hash (data, strlen (data), "sha256");
/* hash == "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73"
char hash[256 / 8];
int rc, hash_size;
rc = weechat_string_hash (data, strlen (data), "sha256", hash, &hash_size);
/* rc == 1, hash_size == 32 et hash est un tampon avec :
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]

View File

@ -2153,7 +2153,7 @@ char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL);
Questa funzione non è disponibile nelle API per lo scripting.
// TRANSLATION MISSING
==== string_hash_binary
==== string_hash
_WeeChat ≥ 2.8._
@ -2163,104 +2163,52 @@ Prototipo:
[source,C]
----
void string_hash_binary (const char *data, int length_data, const char *hash_algo,
char **hash, int *length_hash);
int string_hash (const void *data, int data_size, const char *hash_algo, void *hash, int *hash_size);
----
Argomenti:
* _data_: the data to hash
* _length_data_: number of bytes to hash in _data_
* _data_size_: 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)
* _hash_: pointer to the hash variable, which is used to store the resulting hash
(the buffer must be large enough, according to the algorithm, see table below)
* _hash_size_: pointer to a variable used to store the length of the hash computed
(in bytes) (can be NULL)
Supported hash algorithms:
[width="100%",cols="4,4,4,5,12",options="header"]
[width="100%",cols="2,2,3,6",options="header"]
|===
| Value | Algorithm | Hash size | Output (binary) | Notes
| `+crc32+` | CRC32 | 32 bits | 4 bytes | Not a hash algorithm in the cryptographic sense.
| `+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
| `+crc32+` | CRC32 | 32 bits | 8 hex chars | Not a hash algorithm in the cryptographic sense.
| `+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 |
| Value | Algorithm | Hash size | Notes
| `+crc32+` | CRC32 | 4 bytes (32 bits) | Not a hash algorithm in the cryptographic sense.
| `+md5+` | MD5 | 16 bytes (128 bits) | *Weak*, not recommended for cryptography usage.
| `+sha1+` | SHA-1 | 20 bytes (160 bits) | *Weak*, not recommended for cryptography usage.
| `+sha224+` | SHA-224 | 28 bytes (224 bits) |
| `+sha256+` | SHA-256 | 32 bytes (256 bits) |
| `+sha384+` | SHA-384 | 48 bytes (384 bits) |
| `+sha512+` | SHA-512 | 64 bytes (512 bits) |
| `+sha3-224+` | SHA3-224 | 28 bytes (224 bits) |
| `+sha3-256+` | SHA3-256 | 32 bytes (256 bits) |
| `+sha3-384+` | SHA3-384 | 48 bytes (384 bits) |
| `+sha3-512+` | SHA3-512 | 64 bytes (512 bits) |
|===
Valore restituito:
* string with hash of data as hexadecimal (must be freed by calling "free"
after use), NULL if error
// TRANSLATION MISSING
* 1 if OK, 0 if error
Esempio in C:
[source,C]
----
const char *data = "abcdefghijklmnopqrstuvwxyz";
char *hash;
hash = weechat_string_hash (data, strlen (data), "sha256");
/* hash == "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73"
char hash[256 / 8];
int rc, hash_size;
rc = weechat_string_hash (data, strlen (data), "sha256", hash, &hash_size);
/* rc == 1, hash_size == 32 and hash is a 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]

View File

@ -2075,7 +2075,7 @@ char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL);
スクリプト API ではこの関数を利用できません。
// TRANSLATION MISSING
==== string_hash_binary
==== string_hash
_WeeChat バージョン 2.8 以上で利用可。_
@ -2085,104 +2085,51 @@ 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);
int string_hash (const void *data, int data_size, const char *hash_algo, void *hash, int *hash_size);
----
引数:
* _data_: the data to hash
* _length_data_: number of bytes to hash in _data_
* _data_size_: 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)
* _hash_: pointer to the hash variable, which is used to store the resulting hash
(the buffer must be large enough, according to the algorithm, see table below)
* _hash_size_: pointer to a variable used to store the length of the hash computed
(in bytes) (can be NULL)
Supported hash algorithms:
[width="100%",cols="4,4,4,5,12",options="header"]
[width="100%",cols="2,2,3,6",options="header"]
|===
| Value | Algorithm | Hash size | Output (binary) | Notes
| `+crc32+` | CRC32 | 32 bits | 4 bytes | Not a hash algorithm in the cryptographic sense.
| `+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
| `+crc32+` | CRC32 | 32 bits | 8 hex chars | Not a hash algorithm in the cryptographic sense.
| `+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 |
| Value | Algorithm | Hash size | Notes
| `+crc32+` | CRC32 | 4 bytes (32 bits) | Not a hash algorithm in the cryptographic sense.
| `+md5+` | MD5 | 16 bytes (128 bits) | *Weak*, not recommended for cryptography usage.
| `+sha1+` | SHA-1 | 20 bytes (160 bits) | *Weak*, not recommended for cryptography usage.
| `+sha224+` | SHA-224 | 28 bytes (224 bits) |
| `+sha256+` | SHA-256 | 32 bytes (256 bits) |
| `+sha384+` | SHA-384 | 48 bytes (384 bits) |
| `+sha512+` | SHA-512 | 64 bytes (512 bits) |
| `+sha3-224+` | SHA3-224 | 28 bytes (224 bits) |
| `+sha3-256+` | SHA3-256 | 32 bytes (256 bits) |
| `+sha3-384+` | SHA3-384 | 48 bytes (384 bits) |
| `+sha3-512+` | SHA3-512 | 64 bytes (512 bits) |
|===
戻り値:
* string with hash of data as hexadecimal (must be freed by calling "free"
after use), NULL if error
* 成功した場合は 1、失敗した場合は 0
C 言語での使用例:
[source,C]
----
const char *data = "abcdefghijklmnopqrstuvwxyz";
char *hash;
hash = weechat_string_hash (data, strlen (data), "sha256");
/* hash == "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73"
char hash[256 / 8];
int rc, hash_size;
rc = weechat_string_hash (data, strlen (data), "sha256", hash, &hash_size);
/* rc == 1, hash_size == 32 and hash is a 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]

View File

@ -66,150 +66,131 @@ int secure_data_encrypted = 0;
/*
* Computes hash of data, as binary buffer.
* Computes hash of data using the given algorithm.
*
* Note: "*hash" must be freed after use.
* The hash size depends on the algorithm, common ones are:
*
* GCRY_MD_CRC32 32 bits == 4 bytes
* GCRY_MD_MD5 128 bits == 16 bytes
* GCRY_MD_SHA1 160 bits == 20 bytes
* GCRY_MD_SHA224 224 bits == 28 bytes
* GCRY_MD_SHA256 256 bits == 32 bytes
* GCRY_MD_SHA384 384 bits == 48 bytes
* GCRY_MD_SHA512 512 bits == 64 bytes
* GCRY_MD_SHA3_224 224 bits == 28 bytes
* GCRY_MD_SHA3_256 256 bits == 32 bytes
* GCRY_MD_SHA3_384 384 bits == 48 bytes
* GCRY_MD_SHA3_512 512 bits == 64 bytes
*
* The result hash is stored in "hash" (the buffer must be large enough).
*
* If hash_size is not NULL, the length of hash is stored in *hash_size
* (in bytes).
*
* Returns 1 if OK, 0 if error.
*/
void
secure_hash_binary (const char *data, int length_data, int hash_algo,
char **hash, int *length_hash)
int
secure_hash (const void *data, int data_size, int hash_algo,
void *hash, int *hash_size)
{
gcry_md_hd_t *hd_md;
int hd_md_opened;
int rc, hd_md_opened, algo_size;
unsigned char *ptr_hash;
if (!hash || !length_hash)
return;
rc = 0;
hd_md = NULL;
hd_md_opened = 0;
*hash = NULL;
*length_hash = 0;
if (!data || (length_data < 1))
goto hash_binary_end;
if (!hash)
goto hash_end;
if (hash_size)
*hash_size = 0;
if (!data || (data_size < 1))
goto hash_end;
hd_md = malloc (sizeof (gcry_md_hd_t));
if (!hd_md)
goto hash_binary_end;
goto hash_end;
if (gcry_md_open (hd_md, hash_algo, 0) != 0)
goto hash_binary_end;
goto hash_end;
hd_md_opened = 1;
gcry_md_write (*hd_md, data, length_data);
gcry_md_write (*hd_md, data, data_size);
ptr_hash = gcry_md_read (*hd_md, hash_algo);
if (!ptr_hash)
goto hash_binary_end;
goto hash_end;
*length_hash = gcry_md_get_algo_dlen (hash_algo);
*hash = malloc (*length_hash);
if (!*hash)
{
*length_hash = 0;
goto hash_binary_end;
}
memcpy (*hash, ptr_hash, *length_hash);
algo_size = gcry_md_get_algo_dlen (hash_algo);
memcpy (hash, ptr_hash, algo_size);
if (hash_size)
*hash_size = algo_size;
hash_binary_end:
rc = 1;
hash_end:
if (hd_md)
{
if (hd_md_opened)
gcry_md_close (*hd_md);
free (hd_md);
}
}
/*
* Computes hash of data, as text (string with hexadecimal).
*
* Returns a string with the hash as hexadecimal, NULL if error.
*
* Note: result must be freed after use.
*/
char *
secure_hash (const char *data, int length_data, int hash_algo)
{
char *hash, *result;
int length_hash, i;
const char *hexa = "0123456789abcdef";
hash = NULL;
length_hash = 0;
result = NULL;
secure_hash_binary (data, length_data, hash_algo, &hash, &length_hash);
if (!hash || (length_hash < 1))
goto hash_end;
result = malloc (((length_hash) * 2) + 1);
if (!result)
goto hash_end;
for (i = 0; i < length_hash; i++)
{
result[i * 2] = hexa[(hash[i] & 0xFF) / 16];
result[(i * 2) + 1] = hexa[(hash[i] & 0xFF) % 16];
}
result[(length_hash * 2)] = '\0';
hash_end:
if (hash)
free (hash);
return result;
return rc;
}
/*
* Computes PKCS#5 Passphrase Based Key Derivation Function number 2 (PBKDF2)
* hash of data, as binary buffer.
*
* Returns 1 if OK, 0 if error.
* The hash size depends on the algorithm, common ones are:
*
* Note: if OK, "*hash" must be freed after use.
* GCRY_MD_SHA1 160 bits == 20 bytes
* GCRY_MD_SHA256 256 bits == 32 bytes
* GCRY_MD_SHA512 512 bits == 64 bytes
*
* The result hash is stored in "hash" (the buffer must be large enough).
*
* If hash_size is not NULL, the length of hash is stored in *hash_size
* (in bytes).
*
* Returns 1 if OK, 0 if error.
*/
int
secure_hash_pbkdf2 (const char *data, int length_data, int hash_subalgo,
const char *salt, int length_salt, int iterations,
char **hash, int *length_hash)
secure_hash_pbkdf2 (const void *data, int data_size, int hash_subalgo,
const void *salt, int salt_size, int iterations,
void *hash, int *hash_size)
{
int rc;
int rc, algo_size;
rc = 0;
if (!hash || !length_hash)
if (!hash)
goto hash_pbkdf2_end;
*hash = NULL;
*length_hash = 0;
if (hash_size)
*hash_size = 0;
if (!data || (length_data < 1) || !salt || (length_salt < 1)
if (!data || (data_size < 1) || !salt || (salt_size < 1)
|| (iterations < 1))
{
goto hash_pbkdf2_end;
}
*length_hash = gcry_md_get_algo_dlen (hash_subalgo);
*hash = malloc (*length_hash);
if (!*hash)
algo_size = gcry_md_get_algo_dlen (hash_subalgo);
if (gcry_kdf_derive (data, data_size, GCRY_KDF_PBKDF2, hash_subalgo,
salt, salt_size, iterations,
algo_size, hash) != 0)
{
*length_hash = 0;
goto hash_pbkdf2_end;
}
if (gcry_kdf_derive (data, length_data, GCRY_KDF_PBKDF2, hash_subalgo,
salt, length_salt, iterations,
*length_hash, *hash) != 0)
{
free (*hash);
*hash = NULL;
*length_hash = 0;
goto hash_pbkdf2_end;
}
if (hash_size)
*hash_size = algo_size;
rc = 1;
@ -229,7 +210,7 @@ int
secure_derive_key (const char *salt, const char *passphrase,
unsigned char *key, int length_key)
{
char *buffer, *hash;
char *buffer, hash[512 / 8];
int length, length_hash;
if (!salt || !passphrase || !key || (length_key < 1))
@ -247,8 +228,7 @@ secure_derive_key (const char *salt, const char *passphrase,
memcpy (buffer + SECURE_SALT_SIZE, passphrase, strlen (passphrase));
/* compute hash of buffer */
secure_hash_binary (buffer, length, GCRY_MD_SHA512, &hash, &length_hash);
if (!hash)
if (!secure_hash (buffer, length, GCRY_MD_SHA512, hash, &length_hash))
{
free (buffer);
return 0;
@ -258,7 +238,6 @@ secure_derive_key (const char *salt, const char *passphrase,
memcpy (key, hash,
(length_hash > length_key) ? length_key : length_hash);
free (hash);
free (buffer);
return 1;

View File

@ -55,14 +55,13 @@ extern int secure_cipher[];
extern int secure_data_encrypted;
extern char *secure_decrypt_error[];
extern void secure_hash_binary (const char *data, int length_data,
int hash_algo, char **hash, int *length_hash);
extern char *secure_hash (const char *data, int length_data, int hash_algo);
extern int secure_hash_pbkdf2 (const char *data, int length_data,
extern int secure_hash (const void *data, int data_size, int hash_algo,
void *hash, int *hash_size);
extern int secure_hash_pbkdf2 (const void *data, int data_size,
int hash_subalgo,
const char *salt, int length_salt,
const void *salt, int salt_size,
int iterations,
char **hash, int *length_hash);
void *hash, int *hash_size);
extern int secure_encrypt_data (const char *data, int length_data,
int hash_algo, int cipher,
const char *passphrase, char **encrypted,

View File

@ -3444,54 +3444,29 @@ string_get_hash_algo (const char *hash_algo)
}
/*
* Computes hash data, as binary buffer.
*
* Note: "*hash" must be freed after use.
* Computes hash data.
*/
void
string_hash_binary (const char *data, int length_data, const char *hash_algo,
char **hash, int *length_hash)
int
string_hash (const void *data, int data_size, const char *hash_algo,
void *hash, int *hash_size)
{
int algo;
if (!hash || !length_hash)
return;
if (!hash)
return 0;
*hash = NULL;
*length_hash = 0;
if (hash_size)
*hash_size = 0;
if (!data || (length_data < 1) || !hash_algo)
return;
if (!data || (data_size < 1) || !hash_algo)
return 0;
algo = string_get_hash_algo (hash_algo);
if (algo == GCRY_MD_NONE)
return;
return 0;
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);
return secure_hash (data, data_size, algo, hash, hash_size);
}
/*

View File

@ -118,11 +118,8 @@ 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_hash (const void *data, int data_size,
const char *hash_algo, void *hash, int *hash_size);
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,

View File

@ -623,7 +623,6 @@ 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;

View File

@ -187,7 +187,7 @@ char *
relay_websocket_build_handshake (struct t_relay_client *client)
{
const char *sec_websocket_key;
char *key, sec_websocket_accept[128], handshake[1024], *hash;
char *key, sec_websocket_accept[128], handshake[1024], hash[160 / 8];
int length, length_hash;
sec_websocket_key = weechat_hashtable_get (client->http_headers,
@ -207,9 +207,7 @@ relay_websocket_build_handshake (struct t_relay_client *client)
snprintf (key, length, "%s%s", sec_websocket_key, WEBSOCKET_GUID);
/* compute 160-bit SHA1 on the key and encode it with base64 */
weechat_string_hash_binary (key, strlen (key), "sha1",
&hash, &length_hash);
if (!hash)
if (!weechat_string_hash (key, strlen (key), "sha1", hash, &length_hash))
{
free (key);
return NULL;
@ -220,7 +218,6 @@ relay_websocket_build_handshake (struct t_relay_client *client)
sec_websocket_accept[0] = '\0';
}
free (hash);
free (key);
/* build the handshake (it will be sent as-is to client) */

View File

@ -755,7 +755,8 @@ script_repo_sha512sum_file (const char *filename)
{
struct stat st;
FILE *file;
char *data, *hash;
char *data, hash[512 / 8], hash_hexa[((512 / 8) * 2) + 1];
int length_hash;
if (stat (filename, &st) == -1)
return NULL;
@ -773,11 +774,17 @@ script_repo_sha512sum_file (const char *filename)
}
fclose (file);
hash = weechat_string_hash (data, st.st_size, "sha512");
if (!weechat_string_hash (data, st.st_size, "sha512", hash, &length_hash))
{
free (data);
return NULL;
}
weechat_string_base_encode (16, hash, length_hash, hash_hexa);
weechat_string_tolower (hash_hexa);
free (data);
return hash;
return strdup (hash_hexa);
}
/*

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 "20200229-01"
#define WEECHAT_PLUGIN_API_VERSION "20200301-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@ -341,11 +341,8 @@ 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_hash) (const void *data, int data_size,
const char *hash_algo, void *hash, int *hash_size);
int (*string_is_command_char) (const char *string);
const char *(*string_input_for_buffer) (const char *string);
char *(*string_eval_expression )(const char *expr,
@ -1259,13 +1256,10 @@ 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_hash(__data, __data_size, __hash_algo, \
__hash, __hash_size) \
(weechat_plugin->string_hash)(__data, __data_size, __hash_algo, \
__hash, __hash_size)
#define weechat_string_is_command_char(__string) \
(weechat_plugin->string_is_command_char)(__string)
#define weechat_string_input_for_buffer(__string) \

View File

@ -33,87 +33,52 @@ extern "C"
#define SECURE_PASSWORD "this_is_a_secret_password"
#define TOTP_SECRET "secretpasswordbase32"
#define WEE_CHECK_HASH_BIN(__result_hash, __data, __length_data, \
__hash_algo) \
#define WEE_CHECK_HASH(__result_code, __result_hash, \
__data, __data_size, __hash_algo) \
if (__result_hash) \
{ \
result_bin = (char *)malloc (4096); \
length_bin = string_base16_decode (__result_hash, \
(char *)result_bin); \
hash_size_expected = string_base16_decode (__result_hash, \
hash_expected); \
} \
else \
{ \
result_bin = NULL; \
length_bin = 0; \
hash_size_expected = 0; \
} \
hash_bin = NULL; \
length_hash_bin = -1; \
secure_hash_binary (__data, __length_data, __hash_algo, \
&hash_bin, &length_hash_bin); \
if (__result_hash == NULL) \
hash_size = -1; \
LONGS_EQUAL(__result_code, \
secure_hash (__data, __data_size, __hash_algo, \
hash, &hash_size)); \
if (__result_hash) \
{ \
POINTERS_EQUAL(NULL, hash_bin); \
MEMCMP_EQUAL(hash_expected, hash, hash_size); \
} \
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_hash, __data, __length_data, \
__hash_algo) \
hash = secure_hash (__data, __length_data, __hash_algo); \
if (__result_hash == NULL) \
{ \
POINTERS_EQUAL(NULL, hash); \
} \
else \
{ \
STRCMP_EQUAL(__result_hash, hash); \
} \
if (hash) \
free (hash);
LONGS_EQUAL(hash_size_expected, hash_size);
#define WEE_CHECK_HASH_PBKDF2(__result_code, __result_hash, \
__data, __length_data, \
__hash_subalgo, __salt, __length_salt, \
__data, __data_size, \
__hash_subalgo, __salt, __salt_size, \
__iterations) \
if (__result_hash) \
{ \
result_bin = (char *)malloc (4096); \
length_bin = string_base16_decode (__result_hash, \
(char *)result_bin); \
hash_size_expected = string_base16_decode (__result_hash, \
hash_expected); \
} \
else \
{ \
result_bin = NULL; \
length_bin = 0; \
hash_size_expected = 0; \
} \
hash_bin = NULL; \
length_hash_bin = -1; \
hash_size = -1; \
LONGS_EQUAL(__result_code, \
secure_hash_pbkdf2 (__data, __length_data, \
secure_hash_pbkdf2 (__data, __data_size, \
__hash_subalgo, \
__salt, __length_salt, \
__salt, __salt_size, \
__iterations, \
&hash_bin, &length_hash_bin)); \
if (__result_hash == NULL) \
hash, &hash_size)); \
if (__result_hash) \
{ \
POINTERS_EQUAL(NULL, hash_bin); \
MEMCMP_EQUAL(hash_expected, hash, hash_size); \
} \
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);
LONGS_EQUAL(hash_size_expected, hash_size);
#define WEE_CHECK_TOTP_GENERATE(__result, __secret, __time, __digits) \
totp = secure_totp_generate (__secret, __time, __digits); \
@ -143,56 +108,31 @@ TEST_GROUP(CoreSecure)
/*
* Tests functions:
* secure_hash_binary
* secure_hash
*/
TEST(CoreSecure, Hash)
{
const char *data = DATA_HASH;
char *result_bin, *hash_bin, *hash;
int length, length_bin, length_hash_bin;
char hash_expected[4096], hash[4096];
int data_size, hash_size_expected, hash_size;
length = strlen (data);
data_size = strlen (data);
WEE_CHECK_HASH_BIN(NULL, NULL, 0, 0);
WEE_CHECK_HASH_HEX(NULL, NULL, 0, 0);
WEE_CHECK_HASH(0, NULL, NULL, 0, 0);
WEE_CHECK_HASH(0, NULL, "test", 0, 0);
WEE_CHECK_HASH_BIN(NULL, "test", 0, 0);
WEE_CHECK_HASH_HEX(NULL, "test", 0, 0);
WEE_CHECK_HASH_BIN(DATA_HASH_CRC32, data, length, GCRY_MD_CRC32);
WEE_CHECK_HASH_HEX(DATA_HASH_CRC32, data, length, GCRY_MD_CRC32);
WEE_CHECK_HASH_BIN(DATA_HASH_MD5, data, length, GCRY_MD_MD5);
WEE_CHECK_HASH_HEX(DATA_HASH_MD5, data, length, GCRY_MD_MD5);
WEE_CHECK_HASH_BIN(DATA_HASH_SHA1, data, length, GCRY_MD_SHA1);
WEE_CHECK_HASH_HEX(DATA_HASH_SHA1, data, length, GCRY_MD_SHA1);
WEE_CHECK_HASH_BIN(DATA_HASH_SHA224, data, length, GCRY_MD_SHA224);
WEE_CHECK_HASH_HEX(DATA_HASH_SHA224, data, length, GCRY_MD_SHA224);
WEE_CHECK_HASH_BIN(DATA_HASH_SHA256, data, length, GCRY_MD_SHA256);
WEE_CHECK_HASH_HEX(DATA_HASH_SHA256, data, length, GCRY_MD_SHA256);
WEE_CHECK_HASH_BIN(DATA_HASH_SHA384, data, length, GCRY_MD_SHA384);
WEE_CHECK_HASH_HEX(DATA_HASH_SHA384, data, length, GCRY_MD_SHA384);
WEE_CHECK_HASH_BIN(DATA_HASH_SHA512, data, length, GCRY_MD_SHA512);
WEE_CHECK_HASH_HEX(DATA_HASH_SHA512, data, length, GCRY_MD_SHA512);
WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_224, data, length, GCRY_MD_SHA3_224);
WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_224, data, length, GCRY_MD_SHA3_224);
WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_256, data, length, GCRY_MD_SHA3_256);
WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_256, data, length, GCRY_MD_SHA3_256);
WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_384, data, length, GCRY_MD_SHA3_384);
WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_384, data, length, GCRY_MD_SHA3_384);
WEE_CHECK_HASH_BIN(DATA_HASH_SHA3_512, data, length, GCRY_MD_SHA3_512);
WEE_CHECK_HASH_HEX(DATA_HASH_SHA3_512, data, length, GCRY_MD_SHA3_512);
WEE_CHECK_HASH(1, DATA_HASH_CRC32, data, data_size, GCRY_MD_CRC32);
WEE_CHECK_HASH(1, DATA_HASH_MD5, data, data_size, GCRY_MD_MD5);
WEE_CHECK_HASH(1, DATA_HASH_SHA1, data, data_size, GCRY_MD_SHA1);
WEE_CHECK_HASH(1, DATA_HASH_SHA224, data, data_size, GCRY_MD_SHA224);
WEE_CHECK_HASH(1, DATA_HASH_SHA256, data, data_size, GCRY_MD_SHA256);
WEE_CHECK_HASH(1, DATA_HASH_SHA384, data, data_size, GCRY_MD_SHA384);
WEE_CHECK_HASH(1, DATA_HASH_SHA512, data, data_size, GCRY_MD_SHA512);
WEE_CHECK_HASH(1, DATA_HASH_SHA3_224, data, data_size, GCRY_MD_SHA3_224);
WEE_CHECK_HASH(1, DATA_HASH_SHA3_256, data, data_size, GCRY_MD_SHA3_256);
WEE_CHECK_HASH(1, DATA_HASH_SHA3_384, data, data_size, GCRY_MD_SHA3_384);
WEE_CHECK_HASH(1, DATA_HASH_SHA3_512, data, data_size, GCRY_MD_SHA3_512);
}
/*
@ -203,11 +143,11 @@ TEST(CoreSecure, Hash)
TEST(CoreSecure, HashPbkdf2)
{
const char *data = DATA_HASH, *salt = DATA_HASH_SALT;
char *result_bin, *hash_bin;
int length, length_salt, length_bin, length_hash_bin;
char hash_expected[4096], hash[4096];
int data_size, salt_size, hash_size_expected, hash_size;
length = strlen (data);
length_salt = strlen (salt);
data_size = strlen (data);
salt_size = strlen (salt);
WEE_CHECK_HASH_PBKDF2(0, NULL, NULL, 0, 0, NULL, 0, 0);
WEE_CHECK_HASH_PBKDF2(0, NULL, "test", 0, 0, NULL, 0, 0);
@ -215,38 +155,38 @@ TEST(CoreSecure, HashPbkdf2)
/* SHA1 */
WEE_CHECK_HASH_PBKDF2(1, DATA_HASH_PBKDF2_SHA1_1000,
data, length,
data, data_size,
GCRY_MD_SHA1,
DATA_HASH_SALT, length_salt,
DATA_HASH_SALT, salt_size,
1000);
WEE_CHECK_HASH_PBKDF2(1, DATA_HASH_PBKDF2_SHA1_100000,
data, length,
data, data_size,
GCRY_MD_SHA1,
DATA_HASH_SALT, length_salt,
DATA_HASH_SALT, salt_size,
100000);
/* SHA256 */
WEE_CHECK_HASH_PBKDF2(1, DATA_HASH_PBKDF2_SHA256_1000,
data, length,
data, data_size,
GCRY_MD_SHA256,
DATA_HASH_SALT, length_salt,
DATA_HASH_SALT, salt_size,
1000);
WEE_CHECK_HASH_PBKDF2(1, DATA_HASH_PBKDF2_SHA256_100000,
data, length,
data, data_size,
GCRY_MD_SHA256,
DATA_HASH_SALT, length_salt,
DATA_HASH_SALT, salt_size,
100000);
/* SHA512 */
WEE_CHECK_HASH_PBKDF2(1, DATA_HASH_PBKDF2_SHA512_1000,
data, length,
data, data_size,
GCRY_MD_SHA512,
DATA_HASH_SALT, length_salt,
DATA_HASH_SALT, salt_size,
1000);
WEE_CHECK_HASH_PBKDF2(1, DATA_HASH_PBKDF2_SHA512_100000,
data, length,
data, data_size,
GCRY_MD_SHA512,
DATA_HASH_SALT, length_salt,
DATA_HASH_SALT, salt_size,
100000);
}

View File

@ -108,46 +108,26 @@ extern "C"
STRCMP_EQUAL(__result, str); \
free (str);
#define WEE_CHECK_HASH_BIN(__result, __buffer, __length, __hash_algo) \
if (__result) \
#define WEE_CHECK_HASH(__result_code, __result_hash, \
__data, __data_size, __hash_algo) \
if (__result_hash) \
{ \
result_bin = (char *)malloc (4096); \
length_bin = string_base16_decode (__result, \
(char *)result_bin); \
hash_size_expected = string_base16_decode (__result_hash, \
hash_expected); \
} \
else \
{ \
result_bin = NULL; \
length_bin = 0; \
hash_size_expected = 0; \
} \
string_hash_binary (__buffer, __length, __hash_algo, \
&hash_bin, &length_hash_bin); \
if (__result == NULL) \
hash_size = -1; \
LONGS_EQUAL(__result_code, \
string_hash (__data, __data_size, __hash_algo, \
hash, &hash_size)); \
if (__result_hash) \
{ \
POINTERS_EQUAL(NULL, hash_bin); \
MEMCMP_EQUAL(hash_expected, hash, hash_size); \
} \
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);
LONGS_EQUAL(hash_size_expected, hash_size);
extern struct t_hashtable *string_hashtable_shared;
@ -1954,62 +1934,33 @@ TEST(CoreString, Hex_dump)
/*
* 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;
char hash_expected[4096], hash[4096];
int data_size, hash_size_expected, hash_size;
length = strlen (data);
data_size = strlen (data);
WEE_CHECK_HASH_BIN(NULL, NULL, 0, NULL);
WEE_CHECK_HASH_HEX(NULL, NULL, 0, NULL);
WEE_CHECK_HASH(0, NULL, NULL, 0, NULL);
WEE_CHECK_HASH(0, NULL, DATA_HASH, 0, NULL);
WEE_CHECK_HASH(0, NULL, DATA_HASH, data_size, NULL);
WEE_CHECK_HASH(0, NULL, DATA_HASH, data_size, "not_an_algo");
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_CRC32, data, length, "crc32");
WEE_CHECK_HASH_HEX(DATA_HASH_CRC32, data, length, "crc32");
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");
WEE_CHECK_HASH(1, DATA_HASH_CRC32, data, data_size, "crc32");
WEE_CHECK_HASH(1, DATA_HASH_MD5, data, data_size, "md5");
WEE_CHECK_HASH(1, DATA_HASH_SHA1, data, data_size, "sha1");
WEE_CHECK_HASH(1, DATA_HASH_SHA224, data, data_size, "sha224");
WEE_CHECK_HASH(1, DATA_HASH_SHA256, data, data_size, "sha256");
WEE_CHECK_HASH(1, DATA_HASH_SHA384, data, data_size, "sha384");
WEE_CHECK_HASH(1, DATA_HASH_SHA512, data, data_size, "sha512");
WEE_CHECK_HASH(1, DATA_HASH_SHA3_224, data, data_size, "sha3-224");
WEE_CHECK_HASH(1, DATA_HASH_SHA3_256, data, data_size, "sha3-256");
WEE_CHECK_HASH(1, DATA_HASH_SHA3_384, data, data_size, "sha3-384");
WEE_CHECK_HASH(1, DATA_HASH_SHA3_512, data, data_size, "sha3-512");
}
/*