This repository has been archived on 2022-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
fortuna/seed_file_management.cpp
surtur c615076182
All checks were successful
continuous-integration/drone/push Build is passing
chore(sfm): uint -> unsigned int
2022-01-17 07:20:16 +01:00

106 lines
2.9 KiB
C++

#ifndef FORTUNA_SEED_FILE_MANAGER_CPP
#define FORTUNA_SEED_FILE_MANAGER_CPP
#include "seed_file_management.h"
#include "accumulator.h"
#include "util.h"
#include <cryptopp/secblock.h>
#include <fmt/core.h>
#include <cstdint>
#include <fstream>
#include <stdexcept>
#include <utility>
namespace fortuna {
constinit bool SeedFileManager::RUNNING{false};
SeedFileManager::SeedFileManager(
const fortuna::accumulator::Accumulator& da_accumulator) noexcept {
this->accumulator = da_accumulator;
}
SeedFileManager::~SeedFileManager() noexcept {
set_job_running(false); // RIP, well, yeah, not running when the obj dies
}
auto SeedFileManager::is_job_running() -> bool {
return RUNNING;
}
auto SeedFileManager::set_job_running(bool running) -> void {
SeedFileManager::RUNNING = running;
}
auto SeedFileManager::do_stuff() -> void {
update_seed_file();
do_task.thread_pls(config.write_interval, [this] { write_seed_file(); });
}
auto SeedFileManager::update_seed_file() -> void {
CryptoPP::SecByteBlock buff{config.seed_f_length};
{
std::ifstream f_stream{config.seed_f_path, std::ios::binary};
if (!f_stream) {
std::string msg{"error opening seed file"};
fmt::print("{} {}\n", msg, config.seed_f_path);
fortuna::SeedFileManager::RUNNING = false;
// FIXME: perhaps create a seed file instead of bailing...
throw std::runtime_error("error opening seed file");
}
fortuna::SeedFileManager::RUNNING = true;
f_stream.read(reinterpret_cast<char*>(buff.BytePtr()),
static_cast<int64_t>(config.seed_f_length));
if (static_cast<std::size_t>(f_stream.gcount()) !=
config.seed_f_length) {
std::string msg{"error reading seed from file"};
fmt::print("{} {}, length: {}\n",
msg,
config.seed_f_path,
config.seed_f_length);
throw std::runtime_error(msg);
}
}
try {
std::string str_buff(reinterpret_cast<const char*>(&buff[0]),
buff.SizeInBytes() *
8); // we need the size in bits
accumulator.call_reseed(str_buff);
write_seed_file();
}
catch (std::exception& e) {
fmt::print("{}", e.what());
}
}
auto SeedFileManager::write_seed_file() -> void {
const std::size_t seed_file_length_blocks{
fortuna::Util::b2b(config.seed_f_length)};
CryptoPP::SecByteBlock buff{seed_file_length_blocks *
fortuna::Util::gen_block_size};
std::string da_buff{accumulator.get_random_data(
static_cast<unsigned int>(seed_file_length_blocks))};
fmt::print("[*] sfm: writing seed file\n");
try {
std::ofstream f_stream{config.seed_f_path,
std::ios::binary | std::ios::trunc};
f_stream.write(reinterpret_cast<const char*>(buff.BytePtr()),
static_cast<int64_t>(config.seed_f_length));
fortuna::SeedFileManager::RUNNING = true;
}
catch (std::exception& e) {
fmt::print("[!] sfm: error writing to seed file!\n");
fmt::print("{}\n", e.what());
fortuna::SeedFileManager::RUNNING = false;
}
}
} // namespace fortuna
#endif // FORTUNA_SEED_FILE_MANAGER_CPP