utils: replace hex_to_u64 with std::from_chars

This commit is contained in:
oltolm
2025-11-14 22:22:01 +01:00
committed by Elad
parent ec70c9691f
commit d61f4101ad
3 changed files with 19 additions and 43 deletions
+4 -4
View File
@@ -11,10 +11,10 @@ SELF_KEY::SELF_KEY(u64 ver_start, u64 ver_end, u16 rev, u32 type, const std::str
version_end = ver_end;
revision = rev;
self_type = type;
hex_to_bytes(erk, e.c_str(), 0);
hex_to_bytes(riv, r.c_str(), 0);
hex_to_bytes(pub, pb.c_str(), 0);
hex_to_bytes(priv, pr.c_str(), 0);
hex_to_bytes(erk, e, 0);
hex_to_bytes(riv, r, 0);
hex_to_bytes(pub, pb, 0);
hex_to_bytes(priv, pr, 0);
curve_type = ct;
}
+12 -36
View File
@@ -7,9 +7,11 @@
#include "sha1.h"
#include "sha256.h"
#include "key_vault.h"
#include <charconv>
#include <cstdlib>
#include <cstring>
#include <stdio.h>
#include <time.h>
#include <cstdio>
#include <ctime>
#include "Utilities/StrUtil.h"
#include "Utilities/File.h"
@@ -21,50 +23,24 @@
// Auxiliary functions (endian swap, xor).
// Hex string conversion auxiliary functions.
u64 hex_to_u64(const char* hex_str)
void hex_to_bytes(unsigned char* data, std::string_view hex_str, unsigned int str_length)
{
auto length = std::strlen(hex_str);
u64 tmp = 0;
u64 result = 0;
char c;
while (length--)
{
c = *hex_str++;
if((c >= '0') && (c <= '9'))
tmp = c - '0';
else if((c >= 'a') && (c <= 'f'))
tmp = c - 'a' + 10;
else if((c >= 'A') && (c <= 'F'))
tmp = c - 'A' + 10;
else
tmp = 0;
result |= (tmp << (length * 4));
}
return result;
}
void hex_to_bytes(unsigned char* data, const char* hex_str, unsigned int str_length)
{
const auto strn_length = (str_length > 0) ? str_length : std::strlen(hex_str);
auto data_length = strn_length / 2;
char tmp_buf[3] = {0, 0, 0};
const auto strn_length = (str_length > 0) ? str_length : hex_str.size();
// Don't convert if the string length is odd.
if ((strn_length % 2) == 0)
{
while (data_length--)
for (size_t i = 0; i < strn_length; i += 2)
{
tmp_buf[0] = *hex_str++;
tmp_buf[1] = *hex_str++;
*data++ = static_cast<u8>(hex_to_u64(tmp_buf) & 0xFF);
const auto [ptr, err] = std::from_chars(hex_str.data() + i, hex_str.data() + i + 2, *data++, 16);
if (err != std::errc())
{
fmt::throw_exception("Failed to read hex string: %s", std::make_error_code(err).message());
}
}
}
}
// Crypto functions (AES128-CBC, AES128-ECB, SHA1-HMAC and AES-CMAC).
void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, usz len)
{
+3 -3
View File
@@ -6,7 +6,8 @@
#include "util/types.hpp"
#include <stdlib.h>
#include <cstdlib>
#include <string_view>
enum { CRYPTO_MAX_PATH = 4096 };
@@ -15,8 +16,7 @@ char* extract_file_name(const char* file_path, char real_file_name[CRYPTO_MAX_PA
std::string sha256_get_hash(const char* data, usz size, bool lower_case);
// Hex string conversion auxiliary functions.
u64 hex_to_u64(const char* hex_str);
void hex_to_bytes(unsigned char *data, const char *hex_str, unsigned int str_length);
void hex_to_bytes(unsigned char* data, std::string_view hex_str, unsigned int str_length);
// Crypto functions (AES128-CBC, AES128-ECB, SHA1-HMAC and AES-CMAC).
void aescbc128_decrypt(unsigned char *key, unsigned char *iv, unsigned char *in, unsigned char *out, usz len);