lib: add SHA256 functions

This commit is contained in:
Jussi Kuokkanen 2019-12-01 13:53:07 +02:00
parent f96c26b5c3
commit b8300c0b6e
3 changed files with 22 additions and 3 deletions

View File

@ -59,10 +59,13 @@ typedef struct tc_bin_node_ {
// Create a new node with key and data in the appropriate position
tc_bin_node_t *tc_bin_node_insert(tc_bin_node_t* node, void *key, void *value);
// Find the value associated with the key
void *tc_bin_node_find_value(tc_bin_node_t *node, void *key);
void *tc_bin_node_find_value(tc_bin_node_t *node, const void *key);
// Destroy a node and its children
void tc_bin_node_destroy(tc_bin_node_t *node);
// Function for const char* -> SHA256. Don't free().
const char *tc_sha256(const char *string, uint32_t string_length);
#ifdef __cplusplus
}
#endif

View File

@ -1,8 +1,11 @@
#include <tc_common.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
char **tc_str_arr_dup(uint16_t str_count, char **const strings) {
char **ptr_arr = calloc(str_count, sizeof(char*));
for (uint16_t i = 0; i < str_count; i++) {
@ -54,7 +57,7 @@ tc_bin_node_t *tc_bin_node_insert(tc_bin_node_t *node, void *key, void *value) {
return node;
}
void *tc_bin_node_find_value(tc_bin_node_t *node, void *key) {
void *tc_bin_node_find_value(tc_bin_node_t *node, const void *key) {
if (node == NULL) {
return NULL;
}
@ -71,3 +74,15 @@ void *tc_bin_node_find_value(tc_bin_node_t *node, void *key) {
void tc_bin_node_destroy(tc_bin_node_t *node) {
traverse_postorder(node, &single_destroy);
}
const char *tc_sha256(const char *string, uint32_t string_length) {
unsigned char *d = SHA256((unsigned char*) string, string_length, 0);
static char out[(SHA256_DIGEST_LENGTH * 2) + 1];
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(out + (i * 2), "%02x", d[i]);
}
out[SHA256_DIGEST_LENGTH * 2] = '\0';
return out;
}

View File

@ -66,8 +66,9 @@ tc_readable_result_t tc_readable_result_create(enum tc_data_types type, void *da
break;
case TC_TYPE_INT:
res.data.int_value = *(int64_t*) data;
break;
case TC_TYPE_DOUBLE:
res.data.uint_value = *(double*) data;
res.data.double_value = *(double*) data;
break;
default:
res.valid = false;