From b143962dac2aa0723606d5344ec85b329fa24495 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Mon, 1 Sep 2025 10:24:32 -0500 Subject: [PATCH] refactor --- .../repertory_test/src/providers_test.cpp | 2 +- support/include/utils/encryption.hpp | 31 ++++++++----------- support/src/utils/encrypting_reader.cpp | 10 +++--- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/repertory/repertory_test/src/providers_test.cpp b/repertory/repertory_test/src/providers_test.cpp index 7c516e12..876a055a 100644 --- a/repertory/repertory_test/src/providers_test.cpp +++ b/repertory/repertory_test/src/providers_test.cpp @@ -30,7 +30,7 @@ #include "providers/s3/s3_provider.hpp" #include "providers/sia/sia_provider.hpp" #include "utils/collection.hpp" -#include "utils/file_utils.hpp" +#include "utils/file.hpp" #include "utils/path.hpp" #include "utils/string.hpp" #include "utils/time.hpp" diff --git a/support/include/utils/encryption.hpp b/support/include/utils/encryption.hpp index d03e94ad..11a68aa9 100644 --- a/support/include/utils/encryption.hpp +++ b/support/include/utils/encryption.hpp @@ -133,19 +133,7 @@ struct kdf_config final { [[nodiscard]] auto create_subkey(kdf_context ctx, std::size_t unique_id_, const hash_t &master_key) const -> std::pair { - REPERTORY_USES_FUNCTION_NAME(); - - hash_t sub_key; - auto res = crypto_kdf_derive_from_key( - sub_key.data(), sub_key.size(), unique_id_, - get_kdf_context_name(ctx).data(), master_key.data()); - if (res != 0) { - throw repertory::utils::error::create_exception( - function_name, { - "failed to create sub-key", - std::to_string(res), - }); - } + auto sub_key = derive_subkey(ctx, unique_id_, master_key); auto cfg = *this; cfg.unique_id = unique_id_; @@ -154,18 +142,19 @@ struct kdf_config final { } template - [[nodiscard]] auto recreate_subkey(kdf_context ctx, - const hash_t &master_key) const -> hash_t { + [[nodiscard]] static auto derive_subkey(kdf_context ctx, + std::size_t unique_id_, + const hash_t &master_key) -> hash_t { REPERTORY_USES_FUNCTION_NAME(); - hash_t sub_key; + hash_t sub_key{}; auto res = crypto_kdf_derive_from_key( - sub_key.data(), sub_key.size(), unique_id, + sub_key.data(), sub_key.size(), unique_id_, get_kdf_context_name(ctx).data(), master_key.data()); if (res != 0) { throw repertory::utils::error::create_exception( function_name, { - "failed to recreate sub-key", + "failed to derive sub-key", std::to_string(res), }); } @@ -173,6 +162,12 @@ struct kdf_config final { return sub_key; } + template + [[nodiscard]] auto recreate_subkey(kdf_context ctx, + const hash_t &master_key) const -> hash_t { + return derive_subkey(ctx, unique_id, master_key); + } + [[nodiscard]] static auto from_header(data_cspan data, kdf_config &cfg, bool ignore_checksum = false) -> bool; diff --git a/support/src/utils/encrypting_reader.cpp b/support/src/utils/encrypting_reader.cpp index 26d1e0d3..0132c865 100644 --- a/support/src/utils/encrypting_reader.cpp +++ b/support/src/utils/encrypting_reader.cpp @@ -299,10 +299,12 @@ encrypting_reader::encrypting_reader( : stop_requested_cb_(std::move(stop_requested_cb)), error_return_(error_return), source_file_(utils::file::file::open_or_create_file(source_path, true)) { - keys_.first = configs.first.recreate_subkey( - utils::encryption::kdf_context::data, master_key); - keys_.second = configs.second.recreate_subkey( - utils::encryption::kdf_context::path, master_key); + keys_ = { + configs.first.recreate_subkey(utils::encryption::kdf_context::data, + master_key), + configs.second.recreate_subkey(utils::encryption::kdf_context::path, + master_key), + }; kdf_headers_ = { configs.first.to_header(), configs.second.to_header(),