created encrypt_decrypt_message fn

with ENCRYPTION_DECRYPTION enum
This commit is contained in:
Václav Šmejkal 2025-02-02 15:16:17 +01:00
parent 5cbb9375dd
commit ee104f3208
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -34,6 +34,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <why2/chat/crypto.h> #include <why2/chat/crypto.h>
#include <why2/chat/flags.h> #include <why2/chat/flags.h>
#include <why2/encrypter.h>
#include <why2/decrypter.h>
#include <why2/llist.h> #include <why2/llist.h>
#include <why2/memory.h> #include <why2/memory.h>
#include <why2/misc.h> #include <why2/misc.h>
@ -56,6 +58,12 @@ typedef struct _read_socket_raw_thread_node
char *key; char *key;
} read_socket_raw_thread_node_t; } read_socket_raw_thread_node_t;
enum ENCRYPTION_DECRYPTION
{
ENCRYPTION,
DECRYPTION
};
why2_list_t connection_list = WHY2_LIST_EMPTY; why2_list_t connection_list = WHY2_LIST_EMPTY;
why2_list_t waiting_list = WHY2_LIST_EMPTY; why2_list_t waiting_list = WHY2_LIST_EMPTY;
@ -165,6 +173,47 @@ void remove_non_ascii(char **text)
(*text)[j] = '\0'; (*text)[j] = '\0';
} }
void encrypt_decrypt_message(char **message, char *key, enum ENCRYPTION_DECRYPTION operation)
{
//NO ENCRYPTION
if (key == NULL) return;
//CALLBACKS
why2_output_flags (*operation_cb)(char*, char*) = NULL;
char *(*base64_cb)(char*, size_t*) = NULL;
//SELECT CORRECT CALLBACK
switch (operation)
{
case ENCRYPTION:
operation_cb = why2_encrypt_text;
base64_cb = why2_chat_base64_encode;
break;
case DECRYPTION:
operation_cb = why2_decrypt_text;
base64_cb = why2_chat_base64_decode;
break;
default:
why2_die("ENCRYPTION_DECRYPTION not implemented!");
break;
}
//SET FLAGS
if (why2_get_key_length() < strlen(key)) why2_set_key_length(strlen(key));
//ENCRYPT
why2_output_flags output = operation_cb(*message, key);
//COPY OUTPUT IN BASE64
why2_deallocate(*message);
*message = base64_cb(output.output_text, &output.output_text_length);
//DEALLOCATION
why2_deallocate_output(output);
}
char *read_socket_raw(int socket, WHY2_UNUSED char *key) char *read_socket_raw(int socket, WHY2_UNUSED char *key)
{ {
if (socket == -1) if (socket == -1)