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

@ -86,12 +86,21 @@ auto create_uuid_wstring() -> std::wstring {
#if defined(PROJECT_ENABLE_LIBSODIUM)
auto generate_random_string(std::size_t length) -> std::string {
std::string ret;
if (length == 0U) {
return ret;
}
ret.resize(length);
for (std::size_t i = 0U; i < length; i++) {
do {
ret[i] = static_cast<char>(generate_random<std::uint8_t>() % 74 + 48);
} while (((ret.at(i) >= 91) && (ret.at(i) <= 96)) ||
((ret.at(i) >= 58) && (ret.at(i) <= 64)));
for (auto &ch : ret) {
std::array<unsigned int, 3U> random_list{
generate_random_between(0U, 57U),
generate_random_between(65U, 90U),
generate_random_between(97U, 255U),
};
ch = static_cast<char>(
random_list.at(repertory::utils::generate_random_between(0U, 2U)) %
74U +
48U);
}
return ret;
@ -118,6 +127,10 @@ auto get_environment_variable(std::wstring_view variable) -> std::wstring {
#if defined(PROJECT_ENABLE_BOOST)
auto get_next_available_port(std::uint16_t first_port,
std::uint16_t &available_port) -> bool {
if (first_port == 0U) {
return false;
}
using namespace boost::asio;
using ip::tcp;
@ -141,13 +154,4 @@ auto get_next_available_port(std::uint16_t first_port,
return not error_code;
}
#endif // defined(PROJECT_ENABLE_BOOST)
auto resolve_variables(std::string str) -> std::string {
return utils::path::absolute(str);
}
auto resolve_variables(std::wstring_view str) -> std::wstring {
return utils::string::from_utf8(
resolve_variables(utils::string::to_utf8(str)));
}
} // namespace repertory::utils

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)

View File

@ -276,7 +276,7 @@ auto read_json_file(std::string_view path, nlohmann::json &data) -> bool {
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
if (password.has_value()) {
auto decrypted_data =
utils::encryption::decrypt_data(json_text, *password);
utils::encryption::decrypt_data(*password, json_text);
json_text = {decrypted_data.begin(), decrypted_data.end()};
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
@ -322,7 +322,7 @@ auto write_json_file(std::string_view path,
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
if (password.has_value()) {
return file.write(utils::encryption::encrypt_data(data.dump(), *password),
return file.write(utils::encryption::encrypt_data(*password, data.dump()),
0U);
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)

View File

@ -0,0 +1,70 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "utils/hash.hpp"
#if defined(PROJECT_ENABLE_LIBSODIUM)
namespace {} // namespace
namespace repertory::utils::encryption {
auto create_hash_blake2b_256(std::string_view data) -> hash_256_t {
return create_hash_blake2b_t<char, hash_256_t>(data);
}
auto create_hash_blake2b_256(std::wstring_view data) -> hash_256_t {
return create_hash_blake2b_t<wchar_t, hash_256_t>(data);
}
auto create_hash_blake2b_384(std::string_view data) -> hash_384_t {
return create_hash_blake2b_t<char, hash_384_t>(data);
}
auto create_hash_blake2b_384(std::wstring_view data) -> hash_384_t {
return create_hash_blake2b_t<wchar_t, hash_384_t>(data);
}
auto create_hash_blake2b_512(std::string_view data) -> hash_512_t {
return create_hash_blake2b_t<char, hash_512_t>(data);
}
auto create_hash_blake2b_512(std::wstring_view data) -> hash_512_t {
return create_hash_blake2b_t<wchar_t, hash_512_t>(data);
}
auto create_hash_sha256(std::string_view data) -> hash_256_t {
return create_hash_sha256_t<char>(data);
}
auto create_hash_sha256(std::wstring_view data) -> hash_256_t {
return create_hash_sha256_t<wchar_t>(data);
}
auto create_hash_sha512(std::string_view data) -> hash_512_t {
return create_hash_sha512_t<char>(data);
}
auto create_hash_sha512(std::wstring_view data) -> hash_512_t {
return create_hash_sha512_t<wchar_t>(data);
}
} // namespace repertory::utils::encryption
#endif // defined(PROJECT_ENABLE_LIBSODIUM)