updated build system

This commit is contained in:
2024-08-07 10:38:34 -05:00
parent 9d3e4b8767
commit eddc6bb67c
51 changed files with 2142 additions and 1748 deletions

View File

@ -24,13 +24,19 @@
#include "utils/config.hpp"
#include "utils/base64.hpp"
#include "utils/collection.hpp"
#include "utils/com_init_wrapper.hpp"
#include "utils/common.hpp"
#include "utils/encrypting_reader.hpp"
#include "utils/encryption.hpp"
#include "utils/error.hpp"
#include "utils/file.hpp"
#include "utils/hash.hpp"
#include "utils/path.hpp"
#include "utils/string.hpp"
#include "utils/time.hpp"
#include "utils/unix.hpp"
#include "utils/windows.hpp"
#endif // REPERTORY_INCLUDE_UTILS_ALL_HPP_
#endif // REPERTORY_INCLUDE_UTILS_ALL_HPP_

View File

@ -22,6 +22,8 @@
#ifndef REPERTORY_INCLUDE_UTILS_CONFIG_HPP_
#define REPERTORY_INCLUDE_UTILS_CONFIG_HPP_
#define NOMINMAX
#if defined(_WIN32)
#define WINVER 0x0602
#define _WIN32_WINNT WINVER
@ -300,9 +302,19 @@ namespace repertory {
using data_buffer = std::vector<unsigned char>;
using mutex_lock = std::lock_guard<std::mutex>;
using recur_mutex_lock = std::lock_guard<std::recursive_mutex>;
using stop_type = std::atomic_bool;
using unique_mutex_lock = std::unique_lock<std::mutex>;
using unique_recur_mutex_lock = std::unique_lock<std::recursive_mutex>;
#if defined(_WIN32)
using native_handle = HANDLE;
#else // !defined(_WIN32)
using native_handle = int;
#if !defined(INVALID_HANDLE_VALUE)
#define INVALID_HANDLE_VALUE (-1)
#endif // !defined(INVALID_HANDLE_VALUE)
#endif // defined(_WIN32)
template <class... Ts> struct overloaded : Ts... {
using Ts::operator()...;
};

View File

@ -0,0 +1,146 @@
/*
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.
*/
#ifndef REPERTORY_INCLUDE_UTILS_ENCRYPTING_READER_HPP_
#define REPERTORY_INCLUDE_UTILS_ENCRYPTING_READER_HPP_
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
#include "utils/config.hpp"
#include "utils/file.hpp"
#include "utils/hash.hpp"
namespace repertory::utils::encryption {
class encrypting_reader final {
public:
encrypting_reader(std::string_view file_name, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::optional<std::string_view> relative_parent_path,
std::size_t error_return = 0U);
encrypting_reader(std::string_view encrypted_file_path,
std::string_view source_path, stop_type &stop_requested,
std::string_view token, std::size_t error_return = 0U);
encrypting_reader(
std::string_view encrypted_file_path, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::vector<std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>
iv_list,
std::size_t error_return = 0U);
encrypting_reader(const encrypting_reader &reader);
encrypting_reader(encrypting_reader &&) = delete;
auto operator=(const encrypting_reader &) -> encrypting_reader & = delete;
auto operator=(encrypting_reader &&) -> encrypting_reader & = delete;
~encrypting_reader() noexcept = default;
public:
using iostream = std::basic_iostream<char, std::char_traits<char>>;
using streambuf = std::basic_streambuf<char, std::char_traits<char>>;
private:
utils::encryption::hash_256_t key_;
stop_type &stop_requested_;
size_t error_return_;
utils::file::file source_file_;
private:
std::unordered_map<std::size_t, data_buffer> chunk_buffers_;
std::string encrypted_file_name_;
std::string encrypted_file_path_;
std::vector<
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>
iv_list_;
std::size_t last_data_chunk_{};
std::size_t last_data_chunk_size_{};
std::uint64_t read_offset_{};
std::uint64_t total_size_{};
private:
static const std::size_t header_size_;
static const std::size_t data_chunk_size_;
static const std::size_t encrypted_chunk_size_;
private:
auto reader_function(char *buffer, size_t size, size_t nitems) -> size_t;
public:
[[nodiscard]] static auto
calculate_decrypted_size(std::uint64_t total_size) -> std::uint64_t;
[[nodiscard]] static auto
calculate_encrypted_size(std::string_view source_path) -> std::uint64_t;
[[nodiscard]] auto create_iostream() const -> std::shared_ptr<iostream>;
[[nodiscard]] static constexpr auto
get_encrypted_chunk_size() -> std::size_t {
return encrypted_chunk_size_;
}
[[nodiscard]] static constexpr auto get_data_chunk_size() -> std::size_t {
return data_chunk_size_;
}
[[nodiscard]] auto get_encrypted_file_name() const -> std::string {
return encrypted_file_name_;
}
[[nodiscard]] auto get_encrypted_file_path() const -> std::string {
return encrypted_file_path_;
}
[[nodiscard]] auto get_error_return() const -> std::size_t {
return error_return_;
}
[[nodiscard]] auto get_iv_list()
-> std::vector<std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> {
return iv_list_;
}
[[nodiscard]] auto get_stop_requested() const -> bool {
return stop_requested_;
}
[[nodiscard]] auto get_total_size() const -> std::uint64_t {
return total_size_;
}
[[nodiscard]] static auto reader_function(char *buffer, size_t size,
size_t nitems,
void *instream) -> size_t {
return reinterpret_cast<encrypting_reader *>(instream)->reader_function(
buffer, size, nitems);
}
void set_read_position(std::uint64_t position) { read_offset_ = position; }
};
} // namespace repertory::utils::encryption
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
#endif // REPERTORY_INCLUDE_UTILS_ENCRYPTING_READER_HPP_

View File

@ -33,38 +33,24 @@ inline constexpr const std::uint32_t encryption_header_size{
crypto_aead_xchacha20poly1305_IETF_ABYTES,
};
template <typename hash_t>
[[nodiscard]] inline auto default_create_hash(std::string_view) -> hash_t;
template <typename hash_t>
[[nodiscard]] inline auto default_create_hash(std::wstring_view) -> hash_t;
template <typename hash_t>
inline auto generate_key(
std::string_view password,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) -> hash_t;
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) -> hash_t;
template <typename hash_t>
inline auto generate_key(
std::wstring_view password,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) -> hash_t;
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) -> hash_t;
#if defined(PROJECT_ENABLE_BOOST)
[[nodiscard]] auto decrypt_data(std::string_view password,
std::string_view data) -> data_buffer;
[[nodiscard]] auto encrypt_data(std::string_view password,
std::string_view data) -> data_buffer;
template <typename result, typename arr_t, std::size_t arr_size>
template <typename result_t, typename arr_t, std::size_t arr_size>
[[nodiscard]] inline auto decrypt_data(const std::array<arr_t, arr_size> &key,
const unsigned char *buffer,
std::size_t buffer_size,
result &res) -> bool {
result_t &res) -> bool {
if (buffer_size > encryption_header_size) {
const std::uint32_t size =
boost::endian::native_to_big(static_cast<std::uint32_t>(buffer_size));
@ -80,43 +66,42 @@ template <typename result, typename arr_t, std::size_t arr_size>
return false;
}
template <typename buffer, typename result, typename arr_t,
template <typename buffer_t, typename result_t, typename arr_t,
std::size_t arr_size>
[[nodiscard]] inline auto decrypt_data(const std::array<arr_t, arr_size> &key,
const buffer &buf, result &res) -> bool {
return decrypt_data<result>(
const buffer_t &buf,
result_t &res) -> bool {
return decrypt_data<result_t>(
key, reinterpret_cast<const unsigned char *>(buf.data()), buf.size(),
res);
}
template <typename buffer, typename result, typename hash_t = hash_256_t>
template <typename buffer_t, typename result_t, typename hash_t = hash_256_t>
[[nodiscard]] inline auto decrypt_data(
std::string_view password, const buffer &buf, result &res,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) -> bool {
return decrypt_data<buffer, result>(generate_key(password, hasher), buf, res);
std::string_view password, const buffer_t &buf, result_t &res,
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) -> bool {
return decrypt_data<buffer_t, result_t>(generate_key(password, hasher), buf,
res);
}
template <typename result, typename hash_t = hash_256_t>
template <typename result_t, typename hash_t = hash_256_t>
[[nodiscard]] inline auto decrypt_data(
std::string_view password, const unsigned char *buffer,
std::size_t buffer_size, result &res,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) -> bool {
return decrypt_data<result>(generate_key(password, hasher), buffer,
buffer_size, res);
std::size_t buffer_size, result_t &res,
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) -> bool {
return decrypt_data<result_t>(generate_key(password, hasher), buffer,
buffer_size, res);
}
template <typename result, typename arr_t, std::size_t arr_size>
template <typename result_t, typename arr_t, std::size_t arr_size>
inline void
encrypt_data(const std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
const std::array<arr_t, arr_size> &key,
const unsigned char *buffer, std::size_t buffer_size,
result &res) {
result_t &res) {
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_ABYTES> mac{};
const std::uint32_t size = boost::endian::native_to_big(
@ -137,115 +122,74 @@ encrypt_data(const std::array<unsigned char,
std::memcpy(&res[iv.size()], mac.data(), mac.size());
}
template <typename result, typename s, std::size_t t>
inline void encrypt_data(const std::array<s, t> &key,
template <typename result_t, typename arr_t, std::size_t arr_size>
inline void encrypt_data(const std::array<arr_t, arr_size> &key,
const unsigned char *buffer, std::size_t buffer_size,
result &res) {
result_t &res) {
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> iv{};
randombytes_buf(iv.data(), iv.size());
encrypt_data<result>(iv, key, buffer, buffer_size, res);
encrypt_data<result_t>(iv, key, buffer, buffer_size, res);
}
template <typename result, typename hash_t = hash_256_t>
template <typename result_t, typename hash_t = hash_256_t>
inline void encrypt_data(
std::string_view password, const unsigned char *buffer,
std::size_t buffer_size, result &res,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) {
encrypt_data<result>(generate_key(password, hasher), buffer, buffer_size,
res);
std::size_t buffer_size, result_t &res,
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) {
encrypt_data<result_t>(generate_key(password, hasher), buffer, buffer_size,
res);
}
template <typename buffer, typename result, typename hash_t = hash_256_t>
template <typename buffer_t, typename result_t, typename hash_t = hash_256_t>
inline void encrypt_data(
std::string_view password, const buffer &buf, result &res,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) {
encrypt_data<result>(generate_key(password, hasher),
reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
std::string_view password, const buffer_t &buf, result_t &res,
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) {
encrypt_data<result_t>(generate_key(password, hasher),
reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
}
template <typename buffer, typename result, typename s, std::size_t t>
inline void encrypt_data(const std::array<s, t> &key, const buffer &buf,
result &res) {
encrypt_data<result>(key, reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
template <typename buffer_t, typename result_t, typename arr_t,
std::size_t arr_size>
inline void encrypt_data(const std::array<arr_t, arr_size> &key,
const buffer_t &buf, result_t &res) {
encrypt_data<result_t>(key,
reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
}
template <typename buffer, typename result, typename s, std::size_t t>
template <typename buffer_t, typename result_t, typename arr_t,
std::size_t arr_size>
inline void
encrypt_data(const std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
const std::array<s, t> &key, const buffer &buf, result &res) {
encrypt_data<result>(iv, key,
reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
const std::array<arr_t, arr_size> &key, const buffer_t &buf,
result_t &res) {
encrypt_data<result_t>(iv, key,
reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
}
#endif // defined(PROJECT_ENABLE_BOOST)
template <>
inline auto
default_create_hash<hash_256_t>(std::string_view data) -> hash_256_t {
return create_hash_sha256(data);
}
template <>
inline auto
default_create_hash<hash_256_t>(std::wstring_view data) -> hash_256_t {
return create_hash_sha256(data);
}
template <>
inline auto
default_create_hash<hash_384_t>(std::string_view data) -> hash_384_t {
return create_hash_blake2b_384(data);
}
template <>
inline auto
default_create_hash<hash_384_t>(std::wstring_view data) -> hash_384_t {
return create_hash_blake2b_384(data);
}
template <>
inline auto
default_create_hash<hash_512_t>(std::string_view data) -> hash_512_t {
return create_hash_sha512(data);
}
template <>
inline auto
default_create_hash<hash_512_t>(std::wstring_view data) -> hash_512_t {
return create_hash_sha512(data);
}
template <typename hash_t>
inline auto generate_key(
std::string_view password,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher) -> hash_t {
return hasher.has_value() ? (*hasher)(reinterpret_cast<const unsigned char *>(
password.data()),
password.size())
: default_create_hash<hash_t>(password);
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher)
-> hash_t {
return hasher(reinterpret_cast<const unsigned char *>(password.data()),
password.size());
}
template <typename hash_t>
inline auto generate_key(
std::wstring_view password,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher) -> hash_t {
return hasher.has_value()
? (*hasher)(
reinterpret_cast<const unsigned char *>(password.data()),
password.size() * sizeof(std::wstring_view::value_type))
: default_create_hash<hash_t>(password);
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher)
-> hash_t {
return hasher(reinterpret_cast<const unsigned char *>(password.data()),
password.size() * sizeof(wchar_t));
}
} // namespace repertory::utils::encryption

View File

@ -27,75 +27,103 @@
namespace repertory::utils::file {
class file final {
public:
[[nodiscard]] static auto open_file(std::filesystem::path path) -> file;
[[nodiscard]] static auto open_file(std::filesystem::path path,
bool read_only = false) -> file;
[[nodiscard]] static auto
open_or_create_file(std::filesystem::path path) -> file;
[[nodiscard]] static auto open_or_create_file(std::filesystem::path path,
bool read_only = false) -> file;
file() noexcept = default;
protected:
file(std::fstream stream, std::filesystem::path path)
: path_(std::move(path)), stream_(std::move(stream)) {}
file() = default;
file(file_t file_ptr, std::filesystem::path path)
: file_(std::move(file_ptr)), path_(std::move(path)) {}
public:
file(const file &) = delete;
file(file &&file_) noexcept = default;
file(file &&move_file) noexcept
: file_(std::move(move_file.file_)),
path_(std::move(move_file.path_))
#if defined(_WIN32)
,
mtx_()
#endif // defined(_WIN32)
{
}
~file() { close(); }
auto operator=(const file &) noexcept -> file & = delete;
auto operator=(file &&file_) noexcept -> file & = default;
auto operator=(file &&move_file) noexcept -> file & {
if (&move_file != this) {
file_ = std::move(move_file.file_);
path_ = std::move(move_file.path_);
}
return *this;
}
private:
std::error_code error_{};
file_t file_{nullptr};
std::filesystem::path path_;
std::fstream stream_;
#if defined(_WIN32)
mutable std::recursive_mutex mtx_{};
#endif // defined(_WIN32)
public:
void close();
[[nodiscard]] auto get_error_code() const -> std::error_code {
return error_;
}
void flush();
[[nodiscard]] auto get_handle() const -> native_handle;
[[nodiscard]] auto get_path() const -> std::filesystem::path { return path_; }
[[nodiscard]] auto move_to(std::filesystem::path new_path) -> bool;
[[nodiscard]] auto read(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool {
return read_(reinterpret_cast<unsigned char *>(data.data()), data.size(),
offset, total_read);
}
std::size_t *total_read = nullptr) -> bool;
[[nodiscard]] auto read(unsigned char *data, std::size_t to_read,
std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool;
[[nodiscard]] auto read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool;
[[nodiscard]] auto remove() -> bool;
[[nodiscard]] auto size() const -> std::uint64_t;
[[nodiscard]] auto truncate() -> bool { return truncate(0U); }
[[nodiscard]] auto truncate(std::size_t size) -> bool;
[[nodiscard]] auto write(const data_buffer &data, std::uint64_t offset,
std::size_t *total_written = nullptr) -> bool {
return write_(reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(data_buffer::value_type), offset,
total_written);
}
[[nodiscard]] auto write(std::string_view data, std::uint64_t offset,
std::size_t *total_written = nullptr) -> bool {
return write_(reinterpret_cast<const unsigned char *>(data.data()),
data.size(), offset, total_written);
}
#if defined(PROJECT_ENABLE_JSON)
[[nodiscard]] auto write_json(const nlohmann::json &data,
std::size_t *total_written = nullptr) -> bool {
auto str_data = data.dump();
return write_(reinterpret_cast<const unsigned char *>(str_data.c_str()),
str_data.size(), 0U, total_written);
[[nodiscard]] auto write(std::wstring_view data, std::uint64_t offset,
std::size_t *total_written = nullptr) -> bool {
return write_(reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(wchar_t), offset, total_written);
}
#endif // defined(PROJECT_ENABLE_JSON)
[[nodiscard]] operator bool() const { return stream_.is_open(); }
public:
[[nodiscard]] operator bool() const { return file_ != nullptr; }
private:
[[nodiscard]] auto read_(unsigned char *data, std::size_t to_read,
std::uint64_t offset,
std::size_t *total_read) -> bool;
[[nodiscard]] auto write_(const unsigned char *data, std::size_t to_write,
std::size_t offset,
std::size_t *total_written) -> bool;

View File

@ -35,219 +35,132 @@ using hash_512_t = std::array<unsigned char, 64U>;
[[nodiscard]] auto
create_hash_blake2b_256(std::wstring_view data) -> hash_256_t;
[[nodiscard]] auto
create_hash_blake2b_256(const data_buffer &data) -> hash_256_t;
[[nodiscard]] auto create_hash_blake2b_384(std::string_view data) -> hash_384_t;
[[nodiscard]] auto
create_hash_blake2b_384(std::wstring_view data) -> hash_384_t;
[[nodiscard]] auto create_hash_blake2b_512(std::string_view data) -> hash_512_t;
[[nodiscard]] auto
create_hash_blake2b_384(const data_buffer &data) -> hash_384_t;
[[nodiscard]] auto
create_hash_blake2b_512(std::wstring_view data) -> hash_512_t;
template <typename char_t, typename hash_t>
[[nodiscard]] auto
create_hash_blake2b_t(std::basic_string_view<char_t> data) -> hash_t;
create_hash_blake2b_512(const data_buffer &data) -> hash_512_t;
[[nodiscard]] auto create_hash_blake2b_512(std::string_view data) -> hash_512_t;
template <typename hash_t>
[[nodiscard]] auto create_hash_blake2b_t(const data_buffer &data) -> hash_t;
template <typename char_t, typename hash_t>
[[nodiscard]] auto create_hash_blake2b_t(
const std::vector<std::basic_string<char_t>> &data) -> hash_t;
template <typename arr_t, std::size_t arr_size>
[[nodiscard]] auto
create_hash_blake2b_t(const std::vector<std::array<arr_t, arr_size>> &data)
-> std::array<arr_t, arr_size>;
[[nodiscard]] auto create_hash_blake2b_t(const unsigned char *data,
std::size_t data_size) -> hash_t;
[[nodiscard]] auto create_hash_sha256(std::string_view data) -> hash_256_t;
[[nodiscard]] auto create_hash_sha256(std::wstring_view data) -> hash_256_t;
[[nodiscard]] auto create_hash_sha256(const data_buffer &data) -> hash_256_t;
[[nodiscard]] auto create_hash_sha256(const unsigned char *data,
std::size_t data_size) -> hash_256_t;
[[nodiscard]] auto create_hash_sha512(std::string_view data) -> hash_512_t;
[[nodiscard]] auto create_hash_sha512(std::wstring_view data) -> hash_512_t;
template <typename char_t>
[[nodiscard]] auto
create_hash_sha256_t(std::basic_string_view<char_t> data) -> hash_256_t;
[[nodiscard]] auto create_hash_sha512(const data_buffer &data) -> hash_512_t;
template <typename char_t>
[[nodiscard]] auto create_hash_sha512_t(std::basic_string_view<char_t> data)
-> repertory::utils::encryption::hash_512_t;
[[nodiscard]] auto create_hash_sha512(const unsigned char *data,
std::size_t data_size) -> hash_512_t;
template <typename hash_t>
auto create_hash_blake2b_t(const data_buffer &data) -> hash_t {
[[nodiscard]] inline auto default_create_hash() -> const
std::function<hash_t(const unsigned char *data, std::size_t size)> &;
template <typename hash_t>
auto create_hash_blake2b_t(const unsigned char *data,
std::size_t data_size) -> hash_t {
hash_t hash{};
crypto_generichash_blake2b_state state{};
auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size());
if (res != 0) {
throw std::runtime_error("failed to initialize blake2b|" +
throw std::runtime_error("failed to initialize blake2b-" +
std::to_string(hash.size() * 8U) + "|" +
std::to_string(res));
}
res = crypto_generichash_blake2b_update(
&state, reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(data_buffer::value_type));
res = crypto_generichash_blake2b_update(&state, data, data_size);
if (res != 0) {
throw std::runtime_error("failed to update blake2b|" + std::to_string(res));
throw std::runtime_error("failed to update blake2b-" +
std::to_string(hash.size() * 8U) + "|" +
std::to_string(res));
}
res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size());
if (res != 0) {
throw std::runtime_error("failed to finalize blake2b|" +
throw std::runtime_error("failed to finalize blake2b-" +
std::to_string(hash.size() * 8U) + "|" +
std::to_string(res));
}
return hash;
}
template <typename arr_t, std::size_t arr_size>
auto create_hash_blake2b_t(const std::vector<std::array<arr_t, arr_size>> &data)
-> std::array<arr_t, arr_size> {
using hash_t = std::array<arr_t, arr_size>;
hash_t hash{};
inline const std::function<hash_256_t(const unsigned char *data,
std::size_t size)>
blake2b_256_hasher =
[](const unsigned char *data, std::size_t data_size) -> hash_256_t {
return create_hash_blake2b_t<hash_256_t>(data, data_size);
};
crypto_generichash_blake2b_state state{};
auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size());
if (res != 0) {
throw std::runtime_error("failed to initialize blake2b|" +
std::to_string(res));
}
inline const std::function<hash_384_t(const unsigned char *data,
std::size_t size)>
blake2b_384_hasher =
[](const unsigned char *data, std::size_t data_size) -> hash_384_t {
return create_hash_blake2b_t<hash_384_t>(data, data_size);
};
for (const auto &item : data) {
res = crypto_generichash_blake2b_update(
&state, reinterpret_cast<const unsigned char *>(item.data()),
item.size());
if (res != 0) {
throw std::runtime_error("failed to update blake2b|" +
std::to_string(res));
}
}
inline const std::function<hash_512_t(const unsigned char *data,
std::size_t size)>
blake2b_512_hasher =
[](const unsigned char *data, std::size_t data_size) -> hash_512_t {
return create_hash_blake2b_t<hash_512_t>(data, data_size);
};
res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size());
if (res != 0) {
throw std::runtime_error("failed to finalize blake2b|" +
std::to_string(res));
}
inline const std::function<hash_256_t(const unsigned char *data,
std::size_t size)>
sha256_hasher =
[](const unsigned char *data, std::size_t data_size) -> hash_256_t {
return create_hash_sha256(data, data_size);
};
return hash;
inline const std::function<hash_512_t(const unsigned char *data,
std::size_t size)>
sha512_hasher =
[](const unsigned char *data, std::size_t data_size) -> hash_512_t {
return create_hash_sha512(data, data_size);
};
template <>
[[nodiscard]] inline auto default_create_hash<hash_256_t>() -> const
std::function<hash_256_t(const unsigned char *data, std::size_t size)> & {
return blake2b_256_hasher;
}
template <typename char_t, typename hash_t>
auto create_hash_blake2b_t(const std::vector<std::basic_string<char_t>> &data)
-> hash_t {
hash_t hash{};
crypto_generichash_blake2b_state state{};
auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size());
if (res != 0) {
throw std::runtime_error("failed to initialize blake2b|" +
std::to_string(res));
}
for (const auto &item : data) {
res = crypto_generichash_blake2b_update(
&state, reinterpret_cast<const unsigned char *>(item.data()),
item.size() * sizeof(char_t));
if (res != 0) {
throw std::runtime_error("failed to update blake2b|" +
std::to_string(res));
}
}
res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size());
if (res != 0) {
throw std::runtime_error("failed to finalize blake2b|" +
std::to_string(res));
}
return hash;
template <>
[[nodiscard]] inline auto default_create_hash<hash_384_t>() -> const
std::function<hash_384_t(const unsigned char *data, std::size_t size)> & {
return blake2b_384_hasher;
}
template <typename char_t, typename hash_t>
auto create_hash_blake2b_t(std::basic_string_view<char_t> data) -> hash_t {
hash_t hash{};
crypto_generichash_blake2b_state state{};
auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size());
if (res != 0) {
throw std::runtime_error("failed to initialize blake2b|" +
std::to_string(res));
}
res = crypto_generichash_blake2b_update(
&state, reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(char_t));
if (res != 0) {
throw std::runtime_error("failed to update blake2b|" + std::to_string(res));
}
res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size());
if (res != 0) {
throw std::runtime_error("failed to finalize blake2b|" +
std::to_string(res));
}
return hash;
}
template <typename char_t>
auto create_hash_sha256_t(std::basic_string_view<char_t> data)
-> repertory::utils::encryption::hash_256_t {
repertory::utils::encryption::hash_256_t hash{};
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 *>(data.data()),
data.size() * sizeof(char_t));
if (res != 0) {
throw std::runtime_error("failed to update sha256|" + std::to_string(res));
}
res = crypto_hash_sha256_final(&state, hash.data());
if (res != 0) {
throw std::runtime_error("failed to finalize sha256|" +
std::to_string(res));
}
return hash;
}
template <typename char_t>
auto create_hash_sha512_t(std::basic_string_view<char_t> data)
-> repertory::utils::encryption::hash_512_t {
repertory::utils::encryption::hash_512_t hash{};
crypto_hash_sha512_state state{};
auto res = crypto_hash_sha512_init(&state);
if (res != 0) {
throw std::runtime_error("failed to initialize sha512|" +
std::to_string(res));
}
res = crypto_hash_sha512_update(
&state, reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(char_t));
if (res != 0) {
throw std::runtime_error("failed to update sha512|" + std::to_string(res));
}
res = crypto_hash_sha512_final(&state, hash.data());
if (res != 0) {
throw std::runtime_error("failed to finalize sha512|" +
std::to_string(res));
}
return hash;
template <>
[[nodiscard]] inline auto default_create_hash<hash_512_t>() -> const
std::function<hash_512_t(const unsigned char *data, std::size_t size)> & {
return blake2b_512_hasher;
}
} // namespace repertory::utils::encryption

View File

@ -21,7 +21,6 @@
*/
#include "utils/common.hpp"
#include "utils/path.hpp"
#include "utils/string.hpp"
namespace repertory::utils {

View File

@ -0,0 +1,397 @@
/*
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/encrypting_reader.hpp"
#include "utils/collection.hpp"
#include "utils/common.hpp"
#include "utils/encryption.hpp"
#include "utils/error.hpp"
#include "utils/unix.hpp"
#include "utils/windows.hpp"
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
#if !defined(CURL_READFUNC_ABORT)
#define CURL_READFUNC_ABORT (-1)
#endif // !defined(CURL_READFUNC_ABORT)
namespace repertory::utils::encryption {
class encrypting_streambuf final : public encrypting_reader::streambuf {
public:
encrypting_streambuf(const encrypting_streambuf &) = default;
encrypting_streambuf(encrypting_streambuf &&) = delete;
auto
operator=(const encrypting_streambuf &) -> encrypting_streambuf & = delete;
auto operator=(encrypting_streambuf &&) -> encrypting_streambuf & = delete;
explicit encrypting_streambuf(const encrypting_reader &reader)
: reader_(reader) {
setg(reinterpret_cast<char *>(0), reinterpret_cast<char *>(0),
reinterpret_cast<char *>(reader_.get_total_size()));
}
~encrypting_streambuf() override = default;
private:
encrypting_reader reader_;
protected:
auto seekoff(off_type off, std::ios_base::seekdir dir,
std::ios_base::openmode which = std::ios_base::out |
std::ios_base::in)
-> pos_type override {
if ((which & std::ios_base::in) != std::ios_base::in) {
throw std::runtime_error("output is not supported");
}
const auto set_position = [this](char *next) -> pos_type {
if ((next <= egptr()) && (next >= eback())) {
setg(eback(), next, reinterpret_cast<char *>(reader_.get_total_size()));
return static_cast<std::streamoff>(
reinterpret_cast<std::uintptr_t>(gptr()));
}
return {traits_type::eof()};
};
switch (dir) {
case std::ios_base::beg:
return set_position(eback() + off);
case std::ios_base::cur:
return set_position(gptr() + off);
case std::ios_base::end:
return set_position(egptr() + off);
}
return encrypting_reader::streambuf::seekoff(off, dir, which);
}
auto seekpos(pos_type pos, std::ios_base::openmode which =
std::ios_base::out |
std::ios_base::in) -> pos_type override {
return seekoff(pos, std::ios_base::beg, which);
}
auto uflow() -> int_type override {
auto ret = underflow();
if (ret == traits_type::eof()) {
return ret;
}
gbump(1);
return ret;
}
auto underflow() -> int_type override {
if (gptr() == egptr()) {
return traits_type::eof();
}
reader_.set_read_position(reinterpret_cast<std::uintptr_t>(gptr()));
char c{};
const auto res = encrypting_reader::reader_function(&c, 1U, 1U, &reader_);
if (res != 1) {
return traits_type::eof();
}
return c;
}
auto xsgetn(char *ptr, std::streamsize count) -> std::streamsize override {
if (gptr() == egptr()) {
return traits_type::eof();
}
reader_.set_read_position(reinterpret_cast<std::uintptr_t>(gptr()));
const auto res = encrypting_reader::reader_function(
ptr, 1U, static_cast<std::size_t>(count), &reader_);
if ((res == reader_.get_error_return()) ||
(reader_.get_stop_requested() && (res == CURL_READFUNC_ABORT))) {
return traits_type::eof();
}
setg(eback(), gptr() + res,
reinterpret_cast<char *>(reader_.get_total_size()));
return static_cast<std::streamsize>(res);
}
};
class encrypting_reader_iostream final : public encrypting_reader::iostream {
public:
encrypting_reader_iostream(const encrypting_reader_iostream &) = delete;
encrypting_reader_iostream(encrypting_reader_iostream &&) = delete;
auto operator=(const encrypting_reader_iostream &)
-> encrypting_reader_iostream & = delete;
auto operator=(encrypting_reader_iostream &&)
-> encrypting_reader_iostream & = delete;
explicit encrypting_reader_iostream(
std::unique_ptr<encrypting_streambuf> buffer)
: encrypting_reader::iostream(buffer.get()), buffer_(std::move(buffer)) {}
~encrypting_reader_iostream() override = default;
private:
std::unique_ptr<encrypting_streambuf> buffer_;
};
const std::size_t encrypting_reader::header_size_ = ([]() {
return crypto_aead_xchacha20poly1305_IETF_NPUBBYTES +
crypto_aead_xchacha20poly1305_IETF_ABYTES;
})();
const std::size_t encrypting_reader::data_chunk_size_ = (8UL * 1024UL * 1024UL);
const std::size_t encrypting_reader::encrypted_chunk_size_ =
data_chunk_size_ + header_size_;
encrypting_reader::encrypting_reader(
std::string_view file_name, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::optional<std::string_view> relative_parent_path,
std::size_t error_return)
: key_(utils::encryption::generate_key<utils::encryption::hash_256_t>(
token)),
stop_requested_(stop_requested),
error_return_(error_return),
source_file_(utils::file::file::open_or_create_file(source_path)) {
if (not source_file_) {
throw std::runtime_error("file open failed|src|" +
std::string{source_path});
}
data_buffer result;
utils::encryption::encrypt_data(
key_, reinterpret_cast<const unsigned char *>(file_name.data()),
file_name.size(), result);
encrypted_file_name_ = utils::collection::to_hex_string(result);
if (relative_parent_path.has_value()) {
for (auto &&part : std::filesystem::path(relative_parent_path.value())) {
utils::encryption::encrypt_data(
key_, reinterpret_cast<const unsigned char *>(part.string().c_str()),
strnlen(part.string().c_str(), part.string().size()), result);
encrypted_file_path_ += '/' + utils::collection::to_hex_string(result);
}
encrypted_file_path_ += '/' + encrypted_file_name_;
}
auto file_size = source_file_.size();
const auto total_chunks = utils::divide_with_ceiling(
file_size, static_cast<std::uint64_t>(data_chunk_size_));
total_size_ = file_size + (total_chunks * encryption_header_size);
last_data_chunk_ = total_chunks - 1U;
last_data_chunk_size_ = (file_size <= data_chunk_size_) ? file_size
: (file_size % data_chunk_size_) == 0U
? data_chunk_size_
: file_size % data_chunk_size_;
iv_list_.resize(total_chunks);
for (auto &iv : iv_list_) {
randombytes_buf(iv.data(), iv.size());
}
}
encrypting_reader::encrypting_reader(std::string_view encrypted_file_path,
std::string_view source_path,
stop_type &stop_requested,
std::string_view token,
std::size_t error_return)
: key_(utils::encryption::generate_key<utils::encryption::hash_256_t>(
token)),
stop_requested_(stop_requested),
error_return_(error_return),
source_file_(utils::file::file::open_or_create_file(source_path)) {
if (not source_file_) {
throw std::runtime_error("file open failed|src|" +
std::string{source_path});
}
encrypted_file_path_ = encrypted_file_path;
encrypted_file_name_ =
std::filesystem::path(encrypted_file_path_).filename().string();
auto file_size = source_file_.size();
const auto total_chunks = utils::divide_with_ceiling(
file_size, static_cast<std::uint64_t>(data_chunk_size_));
total_size_ = file_size + (total_chunks * encryption_header_size);
last_data_chunk_ = total_chunks - 1U;
last_data_chunk_size_ = (file_size <= data_chunk_size_) ? file_size
: (file_size % data_chunk_size_) == 0U
? data_chunk_size_
: file_size % data_chunk_size_;
iv_list_.resize(total_chunks);
for (auto &iv : iv_list_) {
randombytes_buf(iv.data(), iv.size());
}
}
encrypting_reader::encrypting_reader(
std::string_view encrypted_file_path, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::vector<
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>
iv_list,
std::size_t error_return)
: key_(utils::encryption::generate_key<utils::encryption::hash_256_t>(
token)),
stop_requested_(stop_requested),
error_return_(error_return),
source_file_(utils::file::file::open_or_create_file(source_path)) {
if (not source_file_) {
throw std::runtime_error("file open failed|src|" +
std::string{source_path});
}
encrypted_file_path_ = encrypted_file_path;
encrypted_file_name_ =
std::filesystem::path(encrypted_file_path_).filename().string();
auto file_size = source_file_.size();
const auto total_chunks = utils::divide_with_ceiling(
file_size, static_cast<std::uint64_t>(data_chunk_size_));
total_size_ = file_size + (total_chunks * encryption_header_size);
last_data_chunk_ = total_chunks - 1U;
last_data_chunk_size_ = (file_size <= data_chunk_size_) ? file_size
: (file_size % data_chunk_size_) == 0U
? data_chunk_size_
: file_size % data_chunk_size_;
iv_list_ = std::move(iv_list);
}
encrypting_reader::encrypting_reader(const encrypting_reader &reader)
: key_(reader.key_),
stop_requested_(reader.stop_requested_),
error_return_(reader.error_return_),
source_file_(
utils::file::file::open_file(reader.source_file_.get_path())),
chunk_buffers_(reader.chunk_buffers_),
encrypted_file_name_(reader.encrypted_file_name_),
encrypted_file_path_(reader.encrypted_file_path_),
iv_list_(reader.iv_list_),
last_data_chunk_(reader.last_data_chunk_),
last_data_chunk_size_(reader.last_data_chunk_size_),
read_offset_(reader.read_offset_),
total_size_(reader.total_size_) {
if (not source_file_) {
throw std::runtime_error("file open failed|src|" +
source_file_.get_path().string());
}
}
auto encrypting_reader::calculate_decrypted_size(std::uint64_t total_size)
-> std::uint64_t {
return total_size - (utils::divide_with_ceiling(
total_size, static_cast<std::uint64_t>(
get_encrypted_chunk_size())) *
encryption_header_size);
}
auto encrypting_reader::calculate_encrypted_size(std::string_view source_path)
-> std::uint64_t {
std::uint64_t file_size{};
if (not utils::file::get_file_size(source_path, file_size)) {
throw std::runtime_error("get file size failed|src|" +
std::string{source_path} + '|' +
std::to_string(utils::get_last_error_code()));
}
const auto total_chunks = utils::divide_with_ceiling(
file_size, static_cast<std::uint64_t>(data_chunk_size_));
return file_size + (total_chunks * encryption_header_size);
}
auto encrypting_reader::create_iostream() const
-> std::shared_ptr<encrypting_reader::iostream> {
return std::make_shared<encrypting_reader_iostream>(
std::make_unique<encrypting_streambuf>(*this));
}
auto encrypting_reader::reader_function(char *buffer, size_t size,
size_t nitems) -> size_t {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
const auto read_size = static_cast<std::size_t>(std::min(
static_cast<std::uint64_t>(size * nitems), total_size_ - read_offset_));
auto chunk = read_offset_ / encrypted_chunk_size_;
auto chunk_offset = read_offset_ % encrypted_chunk_size_;
std::size_t total_read{};
auto ret = false;
if (read_offset_ < total_size_) {
try {
ret = true;
auto remain = read_size;
while (not stop_requested_ && ret && (remain != 0U)) {
if (chunk_buffers_.find(chunk) == chunk_buffers_.end()) {
auto &chunk_buffer = chunk_buffers_[chunk];
data_buffer file_data(chunk == last_data_chunk_
? last_data_chunk_size_
: data_chunk_size_);
chunk_buffer.resize(file_data.size() + encryption_header_size);
std::size_t bytes_read{};
if ((ret = source_file_.read(file_data, chunk * data_chunk_size_,
&bytes_read))) {
utils::encryption::encrypt_data(iv_list_.at(chunk), key_, file_data,
chunk_buffer);
}
} else if (chunk) {
chunk_buffers_.erase(chunk - 1u);
}
auto &chunk_buffer = chunk_buffers_[chunk];
const auto to_read = std::min(
static_cast<std::size_t>(chunk_buffer.size() - chunk_offset),
remain);
std::memcpy(buffer + total_read, &chunk_buffer[chunk_offset], to_read);
total_read += to_read;
remain -= to_read;
chunk_offset = 0u;
chunk++;
read_offset_ += to_read;
}
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
ret = false;
} catch (...) {
utils::error::handle_exception(function_name);
ret = false;
}
}
return stop_requested_ ? CURL_READFUNC_ABORT
: ret ? total_read
: error_return_;
}
} // namespace repertory::utils::encryption
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)

View File

@ -27,184 +27,12 @@
#include "utils/string.hpp"
namespace repertory::utils::file {
auto file::open_file(std::filesystem::path path) -> file {
path = utils::path::absolute(path.string());
if (not is_file(path.string())) {
throw std::runtime_error("file not found: " + path.string());
}
auto stream = std::fstream{
path,
std::ios_base::binary | std::ios_base::in | std::ios_base::out,
};
return {
std::move(stream),
path,
};
}
auto file::open_or_create_file(std::filesystem::path path) -> file {
path = utils::path::absolute(path.string());
auto stream = std::fstream{
path.string().c_str(),
std::ios_base::binary | std::ios_base::trunc | std::ios_base::in |
std::ios_base::out,
};
return {
std::move(stream),
path,
};
}
void file::close() {
if (stream_.is_open()) {
stream_.close();
}
}
auto file::move_to(std::filesystem::path new_path) -> bool {
new_path = utils::path::absolute(new_path.string());
auto reopen{false};
if (stream_.is_open()) {
reopen = true;
close();
}
std::filesystem::rename(path_, new_path, error_);
if (not error_) {
path_ = new_path;
if (reopen) {
*this = open_file(path_);
}
return true;
}
if (reopen) {
*this = open_file(path_);
}
return false;
}
auto file::read_(unsigned char *data, std::size_t to_read, std::uint64_t offset,
std::size_t *total_read) -> bool {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
if (total_read != nullptr) {
(*total_read) = 0U;
}
try {
stream_.seekg(static_cast<std::streamoff>(offset));
auto before = stream_.tellg();
if (before == -1) {
throw std::runtime_error("failed to tellg() before read");
}
stream_.read(reinterpret_cast<char *>(data),
static_cast<std::streamoff>(to_read));
if (total_read != nullptr) {
auto after = stream_.tellg();
if (after >= 0) {
(*total_read) = static_cast<std::size_t>(after - before);
} else if (after == -1 && not stream_.eof()) {
throw std::runtime_error("failed to tellg() after read");
}
}
return true;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return false;
}
auto file::remove() -> bool {
close();
return std::filesystem::remove(path_, error_);
}
auto file::truncate(std::size_t size) -> bool {
auto reopen{false};
if (stream_.is_open()) {
reopen = true;
close();
}
std::filesystem::resize_file(path_, size, error_);
if (reopen) {
*this = open_file(path_);
}
return not error_;
}
auto file::write_(const unsigned char *data, std::size_t to_write,
std::size_t offset, std::size_t *total_written) -> bool {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
if (total_written != nullptr) {
(*total_written) = 0U;
}
try {
stream_.seekp(static_cast<std::streamoff>(offset));
auto before = stream_.tellp();
if (before == -1) {
throw std::runtime_error("failed to tellp() before write");
}
stream_.write(reinterpret_cast<const char *>(data),
static_cast<std::streamoff>(to_write));
auto after = stream_.tellp();
if (after == -1) {
throw std::runtime_error("failed to tellp() after write");
}
if (total_written != nullptr) {
(*total_written) = static_cast<std::size_t>(after - before);
}
return true;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return false;
}
auto get_file_size(std::string_view path, std::uint64_t &file_size) -> bool {
auto abs_path = utils::path::absolute(path);
file_size = 0U;
#if defined(_WIN32)
struct _stat64 st {};
if (_stat64(abs_path.c_str(), &st) != 0) {
#else // !defined(_WIN32)
struct stat st {};
if (stat(abs_path.c_str(), &st) != 0) {
#endif // defined(_WIN32)
return false;
}
if (st.st_size >= 0) {
file_size = static_cast<std::uint64_t>(st.st_size);
}
return (st.st_size >= 0);
std::error_code ec{};
file_size = std::filesystem::file_size(abs_path, ec);
return ec.value() == 0;
}
auto get_file_size(std::wstring_view path, std::uint64_t &file_size) -> bool {
@ -255,44 +83,42 @@ auto read_json_file(std::string_view path, nlohmann::json &data) -> bool {
try {
auto abs_path = utils::path::absolute(path);
if (not is_file(abs_path)) {
return true;
auto file = file::open_file(abs_path);
if (not file) {
return false;
}
std::ifstream file_stream{
abs_path.c_str(),
std::ios_base::binary | std::ios::in,
};
if (not file_stream.is_open()) {
throw std::runtime_error("failed to open file: " + abs_path);
}
auto ret{true};
try {
std::stringstream stream;
stream << file_stream.rdbuf();
data_buffer buffer{};
if (not file.read_all(buffer, 0U)) {
return false;
}
auto json_text = stream.str();
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
if (password.has_value()) {
auto decrypted_data =
utils::encryption::decrypt_data(*password, json_text);
json_text = {decrypted_data.begin(), decrypted_data.end()};
data_buffer decrypted_data{};
if (not utils::encryption::decrypt_data(*password, buffer,
decrypted_data)) {
return false;
}
buffer = decrypted_data;
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
if (not json_text.empty()) {
data = nlohmann::json::parse(json_text.c_str());
std::string json_str(buffer.begin(), buffer.end());
if (not json_str.empty()) {
data = nlohmann::json::parse(json_str);
}
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
ret = false;
return false;
} catch (...) {
utils::error::handle_exception(function_name);
ret = false;
return false;
}
file_stream.close();
return ret;
return true;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
@ -316,18 +142,22 @@ auto write_json_file(std::string_view path,
try {
auto file = file::open_or_create_file(path);
if (not file.truncate()) {
throw std::runtime_error("failed to truncate file: " +
file.get_error_code().message());
throw std::runtime_error("failed to truncate file");
}
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
if (password.has_value()) {
return file.write(utils::encryption::encrypt_data(*password, data.dump()),
0U);
const auto str_data = data.dump();
data_buffer encrypted_data{};
utils::encryption::encrypt_data(
*password, reinterpret_cast<const unsigned char *>(str_data.c_str()),
str_data.size(), encrypted_data);
return file.write(encrypted_data, 0U);
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
return file.write(data, 0U);
return file.write(data.dump(), 0U);
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {

View File

@ -0,0 +1,353 @@
/*
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/file.hpp"
#include "utils/encryption.hpp"
#include "utils/error.hpp"
#include "utils/path.hpp"
#include "utils/string.hpp"
namespace repertory::utils::file {
auto file::open_file(std::filesystem::path path, bool read_only) -> file {
path = utils::path::absolute(path.string());
if (not is_file(path.string())) {
throw std::runtime_error("file not found: " + path.string());
}
if (not read_only) {
#if defined(_WIN32)
_chmod(path.string().c_str(), 0600U);
#else // !defined(_WIN32)
chmod(path.string().c_str(), 0600U);
#endif // defined(_WIN32)
}
#if defined(_WIN32)
auto *ptr =
_fsopen(path.string().c_str(), read_only ? "rb" : "rb+", _SH_DENYNO);
std::cout << errno << std::endl;
#else // !defined(_WIN32)
auto *ptr = fopen(path.string().c_str(), read_only ? "rb" : "rb+");
#endif // defined(_WIN32)
return file{
file_t{ptr},
path,
};
}
auto file::open_or_create_file(std::filesystem::path path,
bool read_only) -> file {
path = utils::path::absolute(path.string());
#if defined(_WIN32)
int old_mode{};
_umask_s(0600U, &old_mode);
#else // !defined(_WIN32)
auto old_mode = umask(0600U);
#endif // defined(_WIN32)
#if defined(_WIN32)
auto *ptr = _fsopen(path.string().c_str(), "ab+", _SH_DENYNO);
#else // !defined(_WIN32)
auto *ptr = fopen(path.string().c_str(), "ab+");
#endif // defined(_WIN32)
#if defined(_WIN32)
_umask_s(old_mode, nullptr);
#else // !defined(_WIN32)
umask(old_mode);
#endif // defined(_WIN32)
if (ptr != nullptr) {
fclose(ptr);
}
return open_file(path, read_only);
}
void file::close() {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
file_.reset();
}
void file::flush() {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
if (file_) {
fflush(file_.get());
}
}
auto file::get_handle() const -> native_handle {
if (file_) {
#if defined(_WIN32)
return reinterpret_cast<native_handle>(
_get_osfhandle(_fileno(file_.get())));
#else // !defined(_WIN32)
return fileno(file_.get());
#endif // defined(_WIN32)
}
return INVALID_HANDLE_VALUE;
}
auto file::move_to(std::filesystem::path new_path) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
new_path = utils::path::absolute(new_path.string());
auto reopen{false};
if (file_) {
reopen = true;
close();
}
std::error_code ec{};
std::filesystem::rename(path_, new_path, ec);
if (not ec) {
path_ = new_path;
if (reopen) {
*this = open_file(path_);
}
return true;
}
if (reopen) {
*this = open_file(path_);
}
return false;
}
auto file::read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
data_buffer buffer;
buffer.resize(65536U);
std::size_t current_read{};
while (read(reinterpret_cast<unsigned char *>(buffer.data()),
buffer.size() * sizeof(data_buffer::value_type), offset,
&current_read)) {
if (total_read != nullptr) {
*total_read += current_read;
}
if (current_read != 0U) {
offset += current_read;
data.insert(
data.end(), buffer.begin(),
std::next(buffer.begin(),
static_cast<std::int64_t>(
current_read / sizeof(data_buffer::value_type))));
continue;
}
return true;
}
return false;
}
auto file::read(data_buffer &data, std::uint64_t offset,
std::size_t *total_read) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
std::size_t bytes_read{};
auto ret =
read(reinterpret_cast<unsigned char *>(data.data()),
data.size() * sizeof(data_buffer::value_type), offset, &bytes_read);
data.resize(bytes_read / sizeof(data_buffer::value_type));
if (total_read != nullptr) {
(*total_read) = bytes_read;
}
return ret;
}
auto file::read(unsigned char *data, std::size_t to_read, std::uint64_t offset,
std::size_t *total_read) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
if (total_read != nullptr) {
(*total_read) = 0U;
}
try {
if (not file_) {
throw std::runtime_error("file is not open for reading");
}
auto res = fseeko(file_.get(), static_cast<std::int64_t>(offset), SEEK_SET);
if (res == -1) {
throw std::runtime_error("failed to seek before read");
}
auto bytes_read = fread(data, 1U, to_read, file_.get());
if (not feof(file_.get()) && ferror(file_.get())) {
throw std::runtime_error("failed to read file bytes");
}
if (total_read != nullptr) {
(*total_read) = bytes_read;
}
return true;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return false;
}
auto file::remove() -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
close();
std::error_code ec{};
return std::filesystem::remove(path_, ec);
}
auto file::truncate(std::size_t size) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
auto reopen{false};
if (file_) {
reopen = true;
close();
}
std::error_code ec{};
std::filesystem::resize_file(path_, size, ec);
if (reopen) {
*this = open_file(path_);
}
return ec.value() == 0;
}
auto file::write_(const unsigned char *data, std::size_t to_write,
std::size_t offset, std::size_t *total_written) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
if (total_written != nullptr) {
(*total_written) = 0U;
}
try {
if (not file_) {
throw std::runtime_error("file is not open for writing");
}
auto res = fseeko(file_.get(), static_cast<std::int64_t>(offset), SEEK_SET);
if (res == -1) {
throw std::runtime_error("failed to seek before write");
}
auto bytes_written =
fwrite(reinterpret_cast<const char *>(data), 1U, to_write, file_.get());
if (not feof(file_.get()) && ferror(file_.get())) {
throw std::runtime_error("failed to read file bytes");
}
flush();
if (total_written != nullptr) {
(*total_written) = bytes_written;
}
return true;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return false;
}
auto file::size() const -> std::uint64_t {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
try {
if (file_) {
if (fseeko(file_.get(), 0, SEEK_END) == -1) {
throw std::runtime_error("failed to seek");
}
auto size = ftello(file_.get());
if (size == -1) {
throw std::runtime_error("failed to get position");
}
return static_cast<std::uint64_t>(size);
}
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return 0U;
}
} // namespace repertory::utils::file

View File

@ -23,47 +23,140 @@
#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);
return create_hash_blake2b_t<hash_256_t>(
reinterpret_cast<const unsigned char *>(data.data()), data.size());
}
auto create_hash_blake2b_256(std::wstring_view data) -> hash_256_t {
return create_hash_blake2b_t<wchar_t, hash_256_t>(data);
return create_hash_blake2b_t<hash_256_t>(
reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(wchar_t));
}
auto create_hash_blake2b_256(const data_buffer &data) -> hash_256_t {
return create_hash_blake2b_t<hash_256_t>(
reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(data_buffer::value_type));
}
auto create_hash_blake2b_384(std::string_view data) -> hash_384_t {
return create_hash_blake2b_t<char, hash_384_t>(data);
return create_hash_blake2b_t<hash_384_t>(
reinterpret_cast<const unsigned char *>(data.data()), data.size());
}
auto create_hash_blake2b_384(std::wstring_view data) -> hash_384_t {
return create_hash_blake2b_t<wchar_t, hash_384_t>(data);
return create_hash_blake2b_t<hash_384_t>(
reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(wchar_t));
}
auto create_hash_blake2b_384(const data_buffer &data) -> hash_384_t {
return create_hash_blake2b_t<hash_384_t>(
reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(data_buffer::value_type));
}
auto create_hash_blake2b_512(std::string_view data) -> hash_512_t {
return create_hash_blake2b_t<char, hash_512_t>(data);
return create_hash_blake2b_t<hash_512_t>(
reinterpret_cast<const unsigned char *>(data.data()), data.size());
}
auto create_hash_blake2b_512(std::wstring_view data) -> hash_512_t {
return create_hash_blake2b_t<wchar_t, hash_512_t>(data);
return create_hash_blake2b_t<hash_512_t>(
reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(wchar_t));
}
auto create_hash_blake2b_512(const data_buffer &data) -> hash_512_t {
return create_hash_blake2b_t<hash_512_t>(
reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(data_buffer::value_type));
}
auto create_hash_sha256(std::string_view data) -> hash_256_t {
return create_hash_sha256_t<char>(data);
return create_hash_sha256(
reinterpret_cast<const unsigned char *>(data.data()), data.size());
}
auto create_hash_sha256(std::wstring_view data) -> hash_256_t {
return create_hash_sha256_t<wchar_t>(data);
return create_hash_sha256(
reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(wchar_t));
}
auto create_hash_sha256(const data_buffer &data) -> hash_256_t {
return create_hash_sha256(
reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(data_buffer::value_type));
}
auto create_hash_sha512(std::string_view data) -> hash_512_t {
return create_hash_sha512_t<char>(data);
return create_hash_sha512(
reinterpret_cast<const unsigned char *>(data.data()), data.size());
}
auto create_hash_sha512(std::wstring_view data) -> hash_512_t {
return create_hash_sha512_t<wchar_t>(data);
return create_hash_sha512(
reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(wchar_t));
}
auto create_hash_sha512(const data_buffer &data) -> hash_512_t {
return create_hash_sha512(
reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(data_buffer::value_type));
}
auto create_hash_sha512(const unsigned char *data,
std::size_t data_size) -> hash_512_t {
hash_512_t hash{};
crypto_hash_sha512_state state{};
auto res = crypto_hash_sha512_init(&state);
if (res != 0) {
throw std::runtime_error("failed to initialize sha-512|" +
std::to_string(res));
}
res = crypto_hash_sha512_update(&state, data, data_size);
if (res != 0) {
throw std::runtime_error("failed to update sha-512|" + std::to_string(res));
}
res = crypto_hash_sha512_final(&state, hash.data());
if (res != 0) {
throw std::runtime_error("failed to finalize sha-512|" +
std::to_string(res));
}
return hash;
}
auto create_hash_sha256(const unsigned char *data,
std::size_t data_size) -> hash_256_t {
hash_256_t hash{};
crypto_hash_sha256_state state{};
auto res = crypto_hash_sha256_init(&state);
if (res != 0) {
throw std::runtime_error("failed to initialize sha-256|" +
std::to_string(res));
}
res = crypto_hash_sha256_update(&state, data, data_size);
if (res != 0) {
throw std::runtime_error("failed to update sha-256|" + std::to_string(res));
}
res = crypto_hash_sha256_final(&state, hash.data());
if (res != 0) {
throw std::runtime_error("failed to finalize sha-256|" +
std::to_string(res));
}
return hash;
}
} // namespace repertory::utils::encryption

View File

@ -19,36 +19,40 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "utils/encryption.hpp"
#ifndef REPERTORY_TEST_INCLUDE_TEST_HPP_
#define REPERTORY_TEST_INCLUDE_TEST_HPP_
#if defined(PROJECT_ENABLE_LIBSODIUM)
namespace repertory::utils::encryption {
#if defined(PROJECT_ENABLE_BOOST)
auto decrypt_data(std::string_view password,
std::string_view data) -> data_buffer {
auto key = generate_key<hash_256_t>(password);
#if defined(U)
#undef U
#endif // defined(U)
data_buffer buf{};
if (not decrypt_data(key,
reinterpret_cast<const unsigned char *>(data.data()),
data.size(), buf)) {
throw std::runtime_error("decryption failed");
}
#include "gmock/gmock.h"
#include "gtest/gtest.h"
return buf;
using ::testing::_;
using namespace ::testing;
#define COMMA ,
#include "utils/config.hpp"
#include "utils/all.hpp"
namespace repertory::test {
[[nodiscard]] auto create_random_file(std::size_t size) -> utils::file::file;
[[nodiscard]] auto
generate_test_file_name(std::string_view file_name_no_extension) -> std::string;
template <typename buffer_t, typename result_t>
static void decrypt_and_verify(const buffer_t &buffer, std::string_view token,
result_t &result) {
EXPECT_TRUE(utils::encryption::decrypt_data(token, buffer, result));
}
auto encrypt_data(std::string_view password,
std::string_view data) -> data_buffer {
auto key = generate_key<hash_256_t>(password);
[[nodiscard]] auto get_test_input_dir() -> std::string;
data_buffer buf{};
encrypt_data(key, reinterpret_cast<const unsigned char *>(data.data()),
data.size(), buf);
[[nodiscard]] auto get_test_output_dir() -> std::string;
} // namespace repertory::test
return buf;
}
#endif // defined(PROJECT_ENABLE_BOOST)
} // namespace repertory::utils::encryption
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
#endif // REPERTORY_TEST_INCLUDE_TEST_HPP_

120
support/test/src/test.cpp Normal file
View File

@ -0,0 +1,120 @@
/*
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 "test.hpp"
namespace {
static std::recursive_mutex file_mtx{};
static std::vector<std::string> generated_files;
static void delete_generated_files() {
repertory::recur_mutex_lock lock{file_mtx};
std::optional<std::string> parent_path;
for (auto &&path : generated_files) {
if (parent_path->empty()) {
parent_path = std::filesystem::path(path).parent_path().string();
}
std::error_code ec{};
std::filesystem::remove(path, ec);
}
generated_files.clear();
if (parent_path.has_value()) {
std::error_code ec{};
std::filesystem::remove_all(*parent_path, ec);
}
}
struct file_deleter final {
~file_deleter() { delete_generated_files(); }
};
static auto deleter{std::make_unique<file_deleter>()};
} // namespace
namespace repertory::test {
auto create_random_file(std::size_t size) -> utils::file::file {
recur_mutex_lock lock{file_mtx};
auto path = generate_test_file_name("random");
auto file = utils::file::file::open_or_create_file(path);
EXPECT_TRUE(file);
if (file) {
data_buffer buf(size);
randombytes_buf(buf.data(), buf.size());
std::size_t bytes_written{};
EXPECT_TRUE(file.write(buf, 0U, &bytes_written));
EXPECT_EQ(size, bytes_written);
EXPECT_EQ(size, file.size());
generated_files.emplace_back(path);
}
return file;
}
auto generate_test_file_name(std::string_view file_name_no_extension)
-> std::string {
recur_mutex_lock lock{file_mtx};
auto path = utils::path::combine(
get_test_output_dir(), {
std::string{file_name_no_extension} +
std::to_string(generated_files.size()),
});
generated_files.emplace_back(path);
return path;
}
auto get_test_input_dir() -> std::string {
static auto test_path = ([]() -> std::string {
auto dir = utils::get_environment_variable("PROJECT_TEST_DIR");
return utils::path::combine(dir.empty() ? "." : dir, {"test_config"});
})();
return test_path;
}
auto get_test_output_dir() -> std::string {
static auto test_path = ([]() -> std::string {
std::string name{"project_test_XXXXXX"};
std::string temp{mktemp(name.data())};
#if defined(_WIN32)
auto path = utils::path::combine("%TEMP%", {temp});
#else // !defined(_WIN32)
auto path = utils::path::combine("/tmp", {temp});
#endif // defined(_WIN32)
if (not utils::file::is_directory(path)) {
std::filesystem::create_directories(path);
}
return path;
})();
return test_path;
}
} // namespace repertory::test

View File

@ -19,12 +19,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gtest/gtest.h"
#include "utils/common.hpp"
#include "utils/collection.hpp"
#include "utils/string.hpp"
#include "test.hpp"
namespace repertory {
TEST(utils_common, calculate_read_size) {
@ -298,7 +293,7 @@ TEST(utils_common, get_environment_variable) {
#if defined(PROJECT_ENABLE_BOOST)
TEST(utils_common, get_next_available_port) {
std::uint16_t available_port{};
for (std::uint16_t port = 1U; port < 65535; ++port) {
for (std::uint16_t port = 1025U; port < 1030U; ++port) {
EXPECT_TRUE(utils::get_next_available_port(port, available_port));
EXPECT_GE(available_port, port);
}

View File

@ -0,0 +1,224 @@
/*
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 "test.hpp"
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
namespace repertory {
TEST(utils_encrypting_reader, read_file_data) {
const auto token = std::string("moose");
auto source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path().string(), stop_requested, token);
for (std::uint8_t i = 0U; i < 8U; i++) {
data_buffer buffer(
utils::encryption::encrypting_reader::get_encrypted_chunk_size());
for (std::uint8_t j = 0U; j < 2U; j++) {
ASSERT_EQ(
buffer.size() / 2U,
utils::encryption::encrypting_reader::reader_function(
reinterpret_cast<char *>(&buffer[(buffer.size() / 2U) * j]),
buffer.size() / 2U, 1U, &reader));
}
data_buffer decrypted_data;
EXPECT_TRUE(
utils::encryption::decrypt_data(token, buffer, decrypted_data));
EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(),
decrypted_data.size());
std::size_t bytes_read{};
data_buffer file_data(decrypted_data.size());
EXPECT_TRUE(source_file.read(
file_data,
utils::encryption::encrypting_reader::get_data_chunk_size() * i,
&bytes_read));
EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(),
file_data.size()));
}
}
}
TEST(utils_encrypting_reader, read_file_data_in_multiple_chunks) {
const auto token = std::string("moose");
auto source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path().string(), stop_requested, token);
for (std::uint8_t i = 0U; i < 8U; i += 2U) {
data_buffer buffer(
utils::encryption::encrypting_reader::get_encrypted_chunk_size() *
2U);
EXPECT_EQ(buffer.size(),
utils::encryption::encrypting_reader::reader_function(
reinterpret_cast<char *>(buffer.data()), buffer.size(), 1U,
&reader));
for (std::uint8_t j = 0U; j < 2U; j++) {
data_buffer decrypted_data;
const auto offset = (j * (buffer.size() / 2U));
EXPECT_TRUE(utils::encryption::decrypt_data(
token,
data_buffer(
std::next(buffer.begin(), static_cast<std::int64_t>(offset)),
std::next(buffer.begin(), static_cast<std::int64_t>(
offset + (buffer.size() / 2U)))),
decrypted_data));
EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(),
decrypted_data.size());
std::size_t bytes_read{};
data_buffer file_data(decrypted_data.size());
EXPECT_TRUE(source_file.read(
file_data,
(utils::encryption::encrypting_reader::get_data_chunk_size() * i) +
(j *
utils::encryption::encrypting_reader::get_data_chunk_size()),
&bytes_read));
EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(),
file_data.size()));
}
}
}
}
TEST(utils_encrypting_reader, read_file_data_as_stream) {
const auto token = std::string("moose");
auto source_file = test::create_random_file(
8U * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path().string(), stop_requested, token);
auto io_stream = reader.create_iostream();
EXPECT_FALSE(io_stream->seekg(0, std::ios_base::end).fail());
EXPECT_TRUE(io_stream->good());
EXPECT_EQ(reader.get_total_size(),
static_cast<std::uint64_t>(io_stream->tellg()));
EXPECT_FALSE(io_stream->seekg(0, std::ios_base::beg).fail());
EXPECT_TRUE(io_stream->good());
for (std::uint8_t i = 0U; i < 8U; i++) {
data_buffer buffer(
utils::encryption::encrypting_reader::get_encrypted_chunk_size());
EXPECT_FALSE(
io_stream->seekg(static_cast<std::streamoff>(i * buffer.size()))
.fail());
EXPECT_TRUE(io_stream->good());
for (std::uint8_t j = 0U; j < 2U; j++) {
EXPECT_FALSE(
io_stream
->read(
reinterpret_cast<char *>(&buffer[(buffer.size() / 2U) * j]),
static_cast<std::streamsize>(buffer.size()) / 2U)
.fail());
EXPECT_TRUE(io_stream->good());
}
data_buffer decrypted_data;
EXPECT_TRUE(
utils::encryption::decrypt_data(token, buffer, decrypted_data));
EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(),
decrypted_data.size());
std::size_t bytes_read{};
data_buffer file_data(decrypted_data.size());
EXPECT_TRUE(source_file.read(
file_data,
utils::encryption::encrypting_reader::get_data_chunk_size() * i,
&bytes_read));
EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(),
file_data.size()));
}
}
}
TEST(utils_encrypting_reader, read_file_data_in_multiple_chunks_as_stream) {
const auto token = std::string("moose");
auto source_file = test::create_random_file(
8u * utils::encryption::encrypting_reader::get_data_chunk_size());
EXPECT_TRUE(source_file);
if (source_file) {
stop_type stop_requested{false};
utils::encryption::encrypting_reader reader(
"test.dat", source_file.get_path().string(), stop_requested, token);
auto io_stream = reader.create_iostream();
EXPECT_FALSE(io_stream->seekg(0, std::ios_base::end).fail());
EXPECT_TRUE(io_stream->good());
EXPECT_EQ(reader.get_total_size(),
static_cast<std::uint64_t>(io_stream->tellg()));
EXPECT_FALSE(io_stream->seekg(0, std::ios_base::beg).fail());
EXPECT_TRUE(io_stream->good());
for (std::uint8_t i = 0U; i < 8U; i += 2U) {
data_buffer buffer(
utils::encryption::encrypting_reader::get_encrypted_chunk_size() *
2U);
EXPECT_FALSE(io_stream
->read(reinterpret_cast<char *>(buffer.data()),
static_cast<std::streamsize>(buffer.size()))
.fail());
EXPECT_TRUE(io_stream->good());
for (std::uint8_t j = 0U; j < 2U; j++) {
data_buffer decrypted_data;
const auto offset = (j * (buffer.size() / 2U));
EXPECT_TRUE(utils::encryption::decrypt_data(
token,
data_buffer(
std::next(buffer.begin(), static_cast<std::int64_t>(offset)),
std::next(buffer.begin(), static_cast<std::int64_t>(
offset + (buffer.size() / 2U)))),
decrypted_data));
EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(),
decrypted_data.size());
std::size_t bytes_read{};
data_buffer file_data(decrypted_data.size());
EXPECT_TRUE(source_file.read(
file_data,
(utils::encryption::encrypting_reader::get_data_chunk_size() * i) +
(j *
utils::encryption::encrypting_reader::get_data_chunk_size()),
&bytes_read));
EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(),
file_data.size()));
}
}
}
}
} // namespace repertory
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)

View File

@ -19,13 +19,10 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "test.hpp"
#if defined(PROJECT_ENABLE_LIBSODIUM)
#include "gtest/gtest.h"
#include "utils/collection.hpp"
#include "utils/encryption.hpp"
namespace repertory {
static const std::string token{"moose"};
static const std::wstring token_w{L"moose"};
@ -34,7 +31,7 @@ TEST(utils_encryption, generate_key) {
auto key1 =
utils::encryption::generate_key<utils::encryption::hash_256_t>(token);
EXPECT_STREQ(
"182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481",
"ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96",
utils::collection::to_hex_string(key1).c_str());
auto key2 =
@ -50,9 +47,15 @@ TEST(utils_encryption, generate_key) {
auto key1_w =
utils::encryption::generate_key<utils::encryption::hash_256_t>(token_w);
EXPECT_NE(key1, key1_w);
#if defined(_WIN32)
EXPECT_STREQ(
L"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f",
L"4f5eb2a2ab34e3777b230465283923080b9ba59311e74058ccd74185131d11fe",
utils::collection::to_hex_wstring(key1_w).c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
L"0392d95ed3eee9772fbb9af68fedf829a8eb0adbe8575d9691cc9a752196766a",
utils::collection::to_hex_wstring(key1_w).c_str());
#endif
auto key2_w =
utils::encryption::generate_key<utils::encryption::hash_256_t>(L"moose");
@ -68,12 +71,12 @@ TEST(utils_encryption, generate_key) {
EXPECT_NE(key4_w, key4);
}
TEST(utils_encryption, generate_key_default_hasher_is_sha256) {
TEST(utils_encryption, generate_key_default_hasher_is_blake2b_256) {
auto key1 =
utils::encryption::generate_key<utils::encryption::hash_256_t>(token);
auto key2 = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_sha256(
return utils::encryption::create_hash_blake2b_256(
std::string_view(reinterpret_cast<const char *>(data), size));
});
EXPECT_EQ(key1, key2);
@ -82,7 +85,7 @@ TEST(utils_encryption, generate_key_default_hasher_is_sha256) {
utils::encryption::generate_key<utils::encryption::hash_256_t>(token_w);
auto key2_w = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token_w, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_sha256(std::wstring_view(
return utils::encryption::create_hash_blake2b_256(std::wstring_view(
reinterpret_cast<const wchar_t *>(data), size / sizeof(wchar_t)));
});
EXPECT_EQ(key1_w, key2_w);
@ -93,44 +96,44 @@ TEST(utils_encryption, generate_key_default_hasher_is_sha256) {
TEST(utils_encryption, generate_key_with_hasher) {
auto key1 = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_sha256(
std::string_view(reinterpret_cast<const char *>(data), size));
});
token, utils::encryption::blake2b_256_hasher);
EXPECT_STREQ(
"182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481",
"ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96",
utils::collection::to_hex_string(key1).c_str());
auto key2 = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_blake2b_256(
std::string_view(reinterpret_cast<const char *>(data), size));
});
token, utils::encryption::sha256_hasher);
EXPECT_NE(key1, key2);
EXPECT_STREQ(
"ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96",
"182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481",
utils::collection::to_hex_string(key2).c_str());
auto key1_w = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token_w, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_sha256(std::wstring_view(
reinterpret_cast<const wchar_t *>(data), size / sizeof(wchar_t)));
});
token_w, utils::encryption::blake2b_256_hasher);
#if defined(_WIN32)
EXPECT_STREQ(
L"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f",
L"4f5eb2a2ab34e3777b230465283923080b9ba59311e74058ccd74185131d11fe",
utils::collection::to_hex_wstring(key1_w).c_str());
auto key2_w = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token_w, [](auto &&data, auto &&size) -> auto {
return utils::encryption::create_hash_blake2b_256(std::wstring_view(
reinterpret_cast<const wchar_t *>(data), size / sizeof(wchar_t)));
});
EXPECT_NE(key1_w, key2_w);
#else // !defined(_WIN32)
EXPECT_STREQ(
L"0392d95ed3eee9772fbb9af68fedf829a8eb0adbe8575d9691cc9a752196766a",
utils::collection::to_hex_wstring(key1_w).c_str());
#endif
auto key2_w = utils::encryption::generate_key<utils::encryption::hash_256_t>(
token_w, utils::encryption::sha256_hasher);
EXPECT_NE(key1_w, key2_w);
#if defined(_WIN32)
EXPECT_STREQ(
L"918e4c6d39bb373f139b5fac8ec0548a9770da399b2835608974ffeac7fab6c4",
utils::collection::to_hex_wstring(key2_w).c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
L"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f",
utils::collection::to_hex_wstring(key2_w).c_str());
#endif
EXPECT_NE(key1_w, key1);
EXPECT_NE(key2_w, key2);

View File

@ -0,0 +1,65 @@
/*
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 "test.hpp"
namespace repertory {
#if defined(PROJECT_ENABLE_JSON)
TEST(utils_file, read_and_write_json_file) {
auto path = test::generate_test_file_name("utils_file");
auto data = nlohmann::json({{"moose", "cow"}});
EXPECT_TRUE(utils::file::write_json_file(path, data));
nlohmann::json data2{};
EXPECT_TRUE(utils::file::read_json_file(path, data2));
EXPECT_STREQ(data.dump().c_str(), data2.dump().c_str());
}
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
TEST(utils_file, read_and_write_json_file_encrypted) {
auto path = test::generate_test_file_name("utils_file");
auto data = nlohmann::json({{"moose", "cow"}});
EXPECT_TRUE(utils::file::write_json_file(path, data, "moose"));
nlohmann::json data2{};
EXPECT_TRUE(utils::file::read_json_file(path, data2, "moose"));
EXPECT_STREQ(data.dump().c_str(), data2.dump().c_str());
{
auto file = utils::file::file::open_file(path);
data_buffer encrypted_data{};
EXPECT_TRUE(file.read_all(encrypted_data, 0U));
data_buffer decrypted_data{};
EXPECT_TRUE(utils::encryption::decrypt_data("moose", encrypted_data,
decrypted_data));
EXPECT_STREQ(data.dump().c_str(),
nlohmann::json::parse(
std::string{decrypted_data.begin(), decrypted_data.end()})
.dump()
.c_str());
}
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
#endif // defined(PROJECT_ENABLE_JSON)
} // namespace repertory

View File

@ -0,0 +1,186 @@
/*
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 "test.hpp"
#if defined(PROJECT_ENABLE_LIBSODIUM)
namespace repertory {
TEST(utils_hash, hash_type_sizes) {
EXPECT_EQ(32U, utils::encryption::hash_256_t{}.size());
EXPECT_EQ(48U, utils::encryption::hash_384_t{}.size());
EXPECT_EQ(64U, utils::encryption::hash_512_t{}.size());
}
TEST(utils_hash, default_hasher_is_blake2b) {
EXPECT_EQ(
&utils::encryption::blake2b_256_hasher,
&utils::encryption::default_create_hash<utils::encryption::hash_256_t>());
EXPECT_EQ(
&utils::encryption::blake2b_384_hasher,
&utils::encryption::default_create_hash<utils::encryption::hash_384_t>());
EXPECT_EQ(
&utils::encryption::blake2b_512_hasher,
&utils::encryption::default_create_hash<utils::encryption::hash_512_t>());
}
TEST(utils_hash, blake2b_256) {
auto hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_256("a"));
EXPECT_STREQ(
"8928aae63c84d87ea098564d1e03ad813f107add474e56aedd286349c0c03ea4",
hash.c_str());
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_256(L"a"));
#if defined(_WIN32)
EXPECT_STREQ(
"d2373b17cd8a8e19e39f52fa4905a274f93805fbb8bb4c7f3cb4b2cd6708ec8a",
hash.c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
"9fdf5757d7eea386f0d34d2c0e202527986febf1ebb4315fcf7fff40776fa41d",
hash.c_str());
#endif
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_256({1U}));
EXPECT_STREQ(
"ee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25",
hash.c_str());
}
TEST(utils_hash, blake2b_384) {
auto hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_384("a"));
EXPECT_STREQ("7d40de16ff771d4595bf70cbda0c4ea0a066a6046fa73d34471cd4d93d827d7"
"c94c29399c50de86983af1ec61d5dcef0",
hash.c_str());
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_384(L"a"));
#if defined(_WIN32)
EXPECT_STREQ("637fe31d1e955760ef31043d525d9321826a778ddbe82fcde45a98394241380"
"96675e2f87e36b53ab223a7fd254198fd",
hash.c_str());
#else // !defined(_WIN32)
EXPECT_STREQ("9d469bd9dab9d4b48b8688de7c22704a8de1b081294f9be294100dfa9f05c92"
"e8d3616476e46cd14f9e613fed80fd157",
hash.c_str());
#endif
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_384({1U}));
EXPECT_STREQ("42cfe875d08d816538103b906bb0b05202e0b09c4e981680c1110684fc7845b"
"c91c178fa167afcc445490644b2bf5f5b",
hash.c_str());
}
TEST(utils_hash, blake2b_512) {
auto hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_512("a"));
EXPECT_STREQ(
"333fcb4ee1aa7c115355ec66ceac917c8bfd815bf7587d325aec1864edd24e34d5abe2c6"
"b1b5ee3face62fed78dbef802f2a85cb91d455a8f5249d330853cb3c",
hash.c_str());
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_512(L"a"));
#if defined(_WIN32)
EXPECT_STREQ(
"05970b95468b0b1941066ff189091493e73859ce41cde5ad08118e93ea1d81a57a144296"
"a26a9fe7781481bde97b886725e36e30b305d8bd5cce1ae36bf1564a",
hash.c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
"bbc187c6e4d8525655d0ada62d16eed59f3db3ab07e04fb0483fd4ae21d88b984774add9"
"b3fbcff56f9638091013994f8e2d4646fdbbcb4879e2b5160bbb755d",
hash.c_str());
#endif
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_blake2b_512({1U}));
EXPECT_STREQ(
"9545ba37b230d8a2e716c4707586542780815b7c4088edcb9af6a9452d50f32474d5ba9a"
"ab52a67aca864ef2696981c2eadf49020416136afd838fb048d21653",
hash.c_str());
}
TEST(utils_hash, sha256) {
auto hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha256("a"));
EXPECT_STREQ(
"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb",
hash.c_str());
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha256(L"a"));
#if defined(_WIN32)
EXPECT_STREQ(
"ffe9aaeaa2a2d5048174df0b80599ef0197ec024c4b051bc9860cff58ef7f9f3",
hash.c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
"a2d398922901344d08180dc41d3e9d73d8c148c7f6e092835bbb28e02dbcf184",
hash.c_str());
#endif
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha256({1U}));
EXPECT_STREQ(
"4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a",
hash.c_str());
}
TEST(utils_hash, sha512) {
auto hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha512("a"));
EXPECT_STREQ(
"1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c65"
"2bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75",
hash.c_str());
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha512(L"a"));
#if defined(_WIN32)
EXPECT_STREQ(
"5c2ca3d50f46ece6066c53bd1a490cbe5f72d2738ae9417332e91e5c3f75205c639d71a9"
"a41d67d965fa137dddf439e0ab9443a6ea44915e90d8b5b566d1c076",
hash.c_str());
#else // !defined(_WIN32)
EXPECT_STREQ(
"a93498d992e81915075144cb304d2bdf040b336283f888252244882d8366dd3a6e2d9749"
"077114dda1a9aa1a7b69d33f7a781f003ccd12e599a6341014f29aaf",
hash.c_str());
#endif
hash = utils::collection::to_hex_string(
utils::encryption::create_hash_sha512({1U}));
EXPECT_STREQ(
"7b54b66836c1fbdd13d2441d9e1434dc62ca677fb68f5fe66a464baadecdbd00576f8d6b"
"5ac3bcc80844b7d50b1cc6603444bbe7cfcf8fc0aa1ee3c636d9e339",
hash.c_str());
}
} // namespace repertory
#endif // defined(PROJECT_ENABLE_LIBSODIUM)

View File

@ -19,11 +19,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gtest/gtest.h"
#include "utils/common.hpp"
#include "utils/path.hpp"
#include "utils/string.hpp"
#include "test.hpp"
namespace repertory {
TEST(utils_path, constants) {

View File

@ -19,9 +19,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gtest/gtest.h"
#include "utils/string.hpp"
#include "test.hpp"
namespace repertory {
TEST(utils_string, begins_with) {