maint: update utils

This commit is contained in:
6arelyFuture 2023-03-06 17:40:06 +00:00
parent f520b407ad
commit 4a1f96a831
No known key found for this signature in database
GPG Key ID: 22F9079C86CFAB31
3 changed files with 18 additions and 18 deletions

View File

@ -12,7 +12,7 @@ namespace crypto_key
{ {
bool load_key(utils::cryptography::ecc::key& key) bool load_key(utils::cryptography::ecc::key& key)
{ {
std::string data{}; std::string data;
if (!utils::io::read_file("./private.key", &data)) if (!utils::io::read_file("./private.key", &data))
{ {
return false; return false;

View File

@ -223,7 +223,7 @@ namespace utils
return std::string(cs(buffer), length); return std::string(cs(buffer), length);
} }
return ""; return {};
} }
void ecc::key::free() void ecc::key::free()
@ -273,7 +273,7 @@ namespace utils
std::string ecc::sign_message(const key& key, const std::string& message) std::string ecc::sign_message(const key& key, const std::string& message)
{ {
if (!key.is_valid()) return ""; if (!key.is_valid()) return {};
std::uint8_t buffer[512]{}; std::uint8_t buffer[512]{};
unsigned long length = sizeof(buffer); unsigned long length = sizeof(buffer);
@ -298,11 +298,11 @@ namespace utils
bool ecc::encrypt(const key& key, std::string& data) bool ecc::encrypt(const key& key, std::string& data)
{ {
std::string out_data{}; std::string out_data;
out_data.resize(std::max(ul(data.size() * 3), ul(0x100))); out_data.resize(std::max(ul(data.size() * 3), ul(0x100)));
auto out_len = ul(out_data.size()); auto out_len = ul(out_data.size());
auto crypt = [&]() auto crypt = [&]() -> int
{ {
return ecc_encrypt_key(cs(data.data()), ul(data.size()), const_cast<std::uint8_t*>(cs(out_data.data())), &out_len, return ecc_encrypt_key(cs(data.data()), ul(data.size()), const_cast<std::uint8_t*>(cs(out_data.data())), &out_len,
prng_.get_state(), prng_.get_id(), find_hash("sha512"), &key.get()); prng_.get_state(), prng_.get_id(), find_hash("sha512"), &key.get());
@ -328,11 +328,11 @@ namespace utils
bool ecc::decrypt(const key& key, std::string& data) bool ecc::decrypt(const key& key, std::string& data)
{ {
std::string out_data{}; std::string out_data;
out_data.resize(std::max(ul(data.size() * 3), ul(0x100))); out_data.resize(std::max(ul(data.size() * 3), ul(0x100)));
auto out_len = ul(out_data.size()); auto out_len = ul(out_data.size());
auto crypt = [&]() auto crypt = [&]() -> int
{ {
return ecc_decrypt_key(cs(data.data()), ul(data.size()), const_cast<std::uint8_t*>(cs(out_data.data())), &out_len, &key.get()); return ecc_decrypt_key(cs(data.data()), ul(data.size()), const_cast<std::uint8_t*>(cs(out_data.data())), &out_len, &key.get());
}; };
@ -359,7 +359,7 @@ namespace utils
{ {
rsa_key new_key; rsa_key new_key;
rsa_import(cs(key.data()), ul(key.size()), &new_key); rsa_import(cs(key.data()), ul(key.size()), &new_key);
const auto _ = gsl::finally([&]() const auto _ = gsl::finally([&]
{ {
rsa_free(&new_key); rsa_free(&new_key);
}); });
@ -369,7 +369,7 @@ namespace utils
out_data.resize(std::max(ul(data.size() * 3), ul(0x100))); out_data.resize(std::max(ul(data.size() * 3), ul(0x100)));
auto out_len = ul(out_data.size()); auto out_len = ul(out_data.size());
auto crypt = [&]() auto crypt = [&]() -> int
{ {
return rsa_encrypt_key(cs(data.data()), ul(data.size()), const_cast<std::uint8_t*>(cs(out_data.data())), &out_len, cs(hash.data()), return rsa_encrypt_key(cs(data.data()), ul(data.size()), const_cast<std::uint8_t*>(cs(out_data.data())), &out_len, cs(hash.data()),
ul(hash.size()), prng_.get_state(), prng_.get_id(), find_hash("sha512"), &new_key); ul(hash.size()), prng_.get_state(), prng_.get_id(), find_hash("sha512"), &new_key);
@ -439,7 +439,7 @@ namespace utils
std::string hash(cs(buffer), sizeof(buffer)); std::string hash(cs(buffer), sizeof(buffer));
if (!hex) return hash; if (!hex) return hash;
return string::dump_hex(hash, ""); return string::dump_hex(hash, {});
} }
std::string aes::encrypt(const std::string& data, const std::string& iv, const std::string& key) std::string aes::encrypt(const std::string& data, const std::string& iv, const std::string& key)
@ -505,7 +505,7 @@ namespace utils
std::string hash(cs(buffer), sizeof(buffer)); std::string hash(cs(buffer), sizeof(buffer));
if (!hex) return hash; if (!hex) return hash;
return string::dump_hex(hash, ""); return string::dump_hex(hash, {});
} }
std::string sha256::compute(const std::string& data, const bool hex) std::string sha256::compute(const std::string& data, const bool hex)
@ -525,7 +525,7 @@ namespace utils
std::string hash(cs(buffer), sizeof(buffer)); std::string hash(cs(buffer), sizeof(buffer));
if (!hex) return hash; if (!hex) return hash;
return string::dump_hex(hash, ""); return string::dump_hex(hash, {});
} }
std::string sha512::compute(const std::string& data, const bool hex) std::string sha512::compute(const std::string& data, const bool hex)
@ -545,7 +545,7 @@ namespace utils
std::string hash(cs(buffer), sizeof(buffer)); std::string hash(cs(buffer), sizeof(buffer));
if (!hex) return hash; if (!hex) return hash;
return string::dump_hex(hash, ""); return string::dump_hex(hash, {});
} }
std::string base64::encode(const std::uint8_t* data, const std::size_t len) std::string base64::encode(const std::uint8_t* data, const std::size_t len)
@ -615,7 +615,7 @@ namespace utils
std::string result; std::string result;
result.resize(sizeof(std::uint32_t)); result.resize(sizeof(std::uint32_t));
get_data(const_cast<char*>(result.data()), result.size()); get_data(const_cast<char*>(result.data()), result.size());
return string::dump_hex(result, ""); return string::dump_hex(result, {});
} }
void random::get_data(void* data, const std::size_t size) void random::get_data(void* data, const std::size_t size)

View File

@ -65,7 +65,7 @@ namespace utils
void* memory::allocate(const std::size_t length) void* memory::allocate(const std::size_t length)
{ {
return std::calloc(length, 1); return std::calloc(1, length);
} }
char* memory::duplicate_string(const std::string& string) char* memory::duplicate_string(const std::string& string)