mirror of
https://github.com/weechat/weechat.git
synced 2026-07-05 17:23:15 +02:00
core: move crypto functions to wee-crypto.c, rename API function string_hash to crypto_hash
This commit is contained in:
@@ -31,10 +31,10 @@
|
||||
|
||||
#include "../core/weechat.h"
|
||||
#include "../core/wee-config.h"
|
||||
#include "../core/wee-crypto.h"
|
||||
#include "../core/wee-hook.h"
|
||||
#include "../core/wee-infolist.h"
|
||||
#include "../core/wee-proxy.h"
|
||||
#include "../core/wee-secure.h"
|
||||
#include "../core/wee-string.h"
|
||||
#include "../core/wee-url.h"
|
||||
#include "../core/wee-util.h"
|
||||
@@ -780,7 +780,7 @@ plugin_api_info_totp_generate_cb (const void *pointer, void *data,
|
||||
digits = number;
|
||||
}
|
||||
|
||||
totp = secure_totp_generate (ptr_secret, totp_time, digits);
|
||||
totp = weecrypto_totp_generate (ptr_secret, totp_time, digits);
|
||||
if (!totp)
|
||||
goto error;
|
||||
|
||||
@@ -853,7 +853,7 @@ plugin_api_info_totp_validate_cb (const void *pointer, void *data,
|
||||
window = number;
|
||||
}
|
||||
|
||||
rc = secure_totp_validate (ptr_secret, totp_time, window, ptr_otp);
|
||||
rc = weecrypto_totp_validate (ptr_secret, totp_time, window, ptr_otp);
|
||||
|
||||
snprintf (value, sizeof (value), "%d", rc);
|
||||
|
||||
|
||||
@@ -25,9 +25,11 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gcrypt.h>
|
||||
|
||||
#include "../core/weechat.h"
|
||||
#include "../core/wee-config.h"
|
||||
#include "../core/wee-crypto.h"
|
||||
#include "../core/wee-hashtable.h"
|
||||
#include "../core/wee-hook.h"
|
||||
#include "../core/wee-infolist.h"
|
||||
@@ -134,6 +136,32 @@ plugin_api_string_base_decode (int base, const char *from, char *to)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Computes hash of data using the given algorithm.
|
||||
*/
|
||||
|
||||
int
|
||||
plugin_api_crypto_hash (const void *data, int data_size, const char *hash_algo,
|
||||
void *hash, int *hash_size)
|
||||
{
|
||||
int algo;
|
||||
|
||||
if (!hash)
|
||||
return 0;
|
||||
|
||||
if (hash_size)
|
||||
*hash_size = 0;
|
||||
|
||||
if (!data || (data_size < 1) || !hash_algo)
|
||||
return 0;
|
||||
|
||||
algo = weecrypto_get_hash_algo (hash_algo);
|
||||
if (algo == GCRY_MD_NONE)
|
||||
return 0;
|
||||
|
||||
return weecrypto_hash (data, data_size, algo, hash, hash_size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Frees an option.
|
||||
*/
|
||||
|
||||
@@ -33,6 +33,11 @@ extern int plugin_api_string_base_encode (int base, const char *from,
|
||||
extern int plugin_api_string_base_decode (int base, const char *from,
|
||||
char *to);
|
||||
|
||||
/* crypto */
|
||||
extern int plugin_api_crypto_hash (const void *data, int data_size,
|
||||
const char *hash_algo,
|
||||
void *hash, int *hash_size);
|
||||
|
||||
/* config */
|
||||
extern void plugin_api_config_file_option_free (struct t_config_option *option);
|
||||
extern struct t_config_option *plugin_api_config_get (const char *option_name);
|
||||
|
||||
@@ -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 = &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;
|
||||
@@ -650,6 +649,8 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv)
|
||||
new_plugin->utf8_pos = &utf8_pos;
|
||||
new_plugin->utf8_strndup = &utf8_strndup;
|
||||
|
||||
new_plugin->crypto_hash = &plugin_api_crypto_hash;
|
||||
|
||||
new_plugin->mkdir_home = &util_mkdir_home;
|
||||
new_plugin->mkdir = &util_mkdir;
|
||||
new_plugin->mkdir_parents = &util_mkdir_parents;
|
||||
|
||||
@@ -207,7 +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 */
|
||||
if (!weechat_string_hash (key, strlen (key), "sha1", hash, &length_hash))
|
||||
if (!weechat_crypto_hash (key, strlen (key), "sha1", hash, &length_hash))
|
||||
{
|
||||
free (key);
|
||||
return NULL;
|
||||
|
||||
@@ -774,7 +774,7 @@ script_repo_sha512sum_file (const char *filename)
|
||||
}
|
||||
fclose (file);
|
||||
|
||||
if (!weechat_string_hash (data, st.st_size, "sha512", hash, &length_hash))
|
||||
if (!weechat_crypto_hash (data, st.st_size, "sha512", hash, &length_hash))
|
||||
{
|
||||
free (data);
|
||||
return NULL;
|
||||
|
||||
@@ -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 "20200301-01"
|
||||
#define WEECHAT_PLUGIN_API_VERSION "20200301-02"
|
||||
|
||||
/* macros for defining plugin infos */
|
||||
#define WEECHAT_PLUGIN_NAME(__name) \
|
||||
@@ -341,8 +341,6 @@ struct t_weechat_plugin
|
||||
char *(*string_hex_dump) (const char *data, int data_size,
|
||||
int bytes_per_line, const char *prefix,
|
||||
const char *suffix);
|
||||
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,
|
||||
@@ -374,6 +372,10 @@ struct t_weechat_plugin
|
||||
int (*utf8_pos) (const char *string, int real_pos);
|
||||
char *(*utf8_strndup) (const char *string, int length);
|
||||
|
||||
/* crypto */
|
||||
int (*crypto_hash) (const void *data, int data_size,
|
||||
const char *hash_algo, void *hash, int *hash_size);
|
||||
|
||||
/* directories/files */
|
||||
int (*mkdir_home) (const char *directory, int mode);
|
||||
int (*mkdir) (const char *directory, int mode);
|
||||
@@ -1256,10 +1258,6 @@ 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(__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) \
|
||||
@@ -1313,6 +1311,12 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
|
||||
#define weechat_utf8_strndup(__string, __length) \
|
||||
(weechat_plugin->utf8_strndup)(__string, __length)
|
||||
|
||||
/* crypto */
|
||||
#define weechat_crypto_hash(__data, __data_size, __hash_algo, \
|
||||
__hash, __hash_size) \
|
||||
(weechat_plugin->crypto_hash)(__data, __data_size, __hash_algo, \
|
||||
__hash, __hash_size)
|
||||
|
||||
/* directories */
|
||||
#define weechat_mkdir_home(__directory, __mode) \
|
||||
(weechat_plugin->mkdir_home)(__directory, __mode)
|
||||
|
||||
Reference in New Issue
Block a user