updated build system
This commit is contained in:
@ -21,7 +21,6 @@
|
||||
*/
|
||||
#include "utils/common.hpp"
|
||||
|
||||
#include "utils/path.hpp"
|
||||
#include "utils/string.hpp"
|
||||
|
||||
namespace repertory::utils {
|
||||
|
397
support/src/utils/encrypting_reader.cpp
Normal file
397
support/src/utils/encrypting_reader.cpp
Normal 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)
|
@ -1,54 +0,0 @@
|
||||
/*
|
||||
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/encryption.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);
|
||||
|
||||
data_buffer buf{};
|
||||
if (not decrypt_data(key,
|
||||
reinterpret_cast<const unsigned char *>(data.data()),
|
||||
data.size(), buf)) {
|
||||
throw std::runtime_error("decryption failed");
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
auto encrypt_data(std::string_view password,
|
||||
std::string_view data) -> data_buffer {
|
||||
auto key = generate_key<hash_256_t>(password);
|
||||
|
||||
data_buffer buf{};
|
||||
encrypt_data(key, reinterpret_cast<const unsigned char *>(data.data()),
|
||||
data.size(), buf);
|
||||
|
||||
return buf;
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_BOOST)
|
||||
} // namespace repertory::utils::encryption
|
||||
|
||||
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
|
@ -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 (...) {
|
||||
|
353
support/src/utils/file_file.cpp
Normal file
353
support/src/utils/file_file.cpp
Normal 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,
|
||||
¤t_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
|
@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user