updated build system
This commit is contained in:
@ -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_
|
||||
|
@ -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()...;
|
||||
};
|
||||
|
146
support/include/utils/encrypting_reader.hpp
Normal file
146
support/include/utils/encrypting_reader.hpp
Normal 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_
|
@ -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
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user