updated build system
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit

This commit is contained in:
2024-08-05 19:38:13 -05:00
parent 760a1e4322
commit dca0752189
13 changed files with 910 additions and 337 deletions

View File

@ -22,33 +22,11 @@
#include "utils/encryption.hpp"
#if defined(PROJECT_ENABLE_LIBSODIUM)
namespace {
using nonce_t =
std::array<unsigned char, crypto_aead_xchacha20poly1305_ietf_NPUBBYTES>;
static constexpr const auto nonce_size{sizeof(nonce_t)};
[[nodiscard]] static auto create_hash_256(std::string_view data)
-> repertory::utils::encryption::hash_256_t {
repertory::utils::encryption::hash_256_t hash{};
crypto_generichash_blake2b_state state{};
crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size());
crypto_generichash_blake2b_update(
&state, reinterpret_cast<const unsigned char *>(data.data()),
data.size());
crypto_generichash_blake2b_final(&state, hash.data(), hash.size());
return hash;
}
} // namespace
namespace repertory::utils::encryption {
#if defined(PROJECT_ENABLE_BOOST)
auto decrypt_data(std::string_view data, std::string_view password,
std::optional<hash_256_func_t> hasher) -> data_buffer {
auto key =
hasher.has_value() ? (*hasher)(password) : create_hash_256(password);
auto decrypt_data(std::string_view password,
std::string_view data) -> data_buffer {
auto key = generate_key<hash_256_t>(password);
data_buffer buf{};
if (not decrypt_data(key,
@ -60,10 +38,9 @@ auto decrypt_data(std::string_view data, std::string_view password,
return buf;
}
auto encrypt_data(std::string_view data, std::string_view password,
std::optional<hash_256_func_t> hasher) -> data_buffer {
auto key =
hasher.has_value() ? (*hasher)(password) : create_hash_256(password);
auto encrypt_data(std::string_view password,
std::string_view data) -> data_buffer {
auto key = generate_key<hash_256_t>(password);
data_buffer buf{};
encrypt_data(key, reinterpret_cast<const unsigned char *>(data.data()),
@ -72,30 +49,6 @@ auto encrypt_data(std::string_view data, std::string_view password,
return buf;
}
#endif // defined(PROJECT_ENABLE_BOOST)
auto generate_key(std::string_view encryption_token) -> key_type {
crypto_hash_sha256_state state{};
auto res = crypto_hash_sha256_init(&state);
if (res != 0) {
throw std::runtime_error("failed to initialize sha256|" +
std::to_string(res));
}
res = crypto_hash_sha256_update(
&state, reinterpret_cast<const unsigned char *>(encryption_token.data()),
encryption_token.size());
if (res != 0) {
throw std::runtime_error("failed to update sha256|" + std::to_string(res));
}
key_type ret{};
res = crypto_hash_sha256_final(&state, ret.data());
if (res != 0) {
throw std::runtime_error("failed to finalize sha256|" +
std::to_string(res));
}
return ret;
}
} // namespace repertory::utils::encryption
#endif // defined(PROJECT_ENABLE_LIBSODIUM)