new_build_system (#18)
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good
Reviewed-on: #18
This commit is contained in:
@ -33,38 +33,30 @@ 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 decrypt_file_name(std::string_view encryption_token,
|
||||
std::string &file_name) -> bool;
|
||||
|
||||
[[nodiscard]] auto encrypt_data(std::string_view password,
|
||||
std::string_view data) -> data_buffer;
|
||||
[[nodiscard]] auto decrypt_file_path(std::string_view encryption_token,
|
||||
std::string &file_path) -> bool;
|
||||
|
||||
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 +72,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 +128,86 @@ 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);
|
||||
}
|
||||
|
||||
#if defined(PROJECT_ENABLE_CURL)
|
||||
using reader_func_t =
|
||||
std::function<bool(data_buffer &cypher_text, std::uint64_t start_offset,
|
||||
std::uint64_t end_offset)>;
|
||||
|
||||
[[nodiscard]] auto
|
||||
read_encrypted_range(const http_range &range,
|
||||
const utils::encryption::hash_256_t &key,
|
||||
reader_func_t reader_func, std::uint64_t total_size,
|
||||
data_buffer &data) -> bool;
|
||||
#endif // defined(PROJECT_ENABLE_CURL)
|
||||
#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
|
||||
|
||||
|
Reference in New Issue
Block a user