#ifndef FORTUNA_SEED_FILE_MANAGER_CPP #define FORTUNA_SEED_FILE_MANAGER_CPP #include "seed_file_management.h" #include "accumulator.h" #include "util.h" #include #include #include #include #include #include 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(buff.BytePtr()), static_cast(config.seed_f_length)); if (static_cast(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(&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(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(buff.BytePtr()), static_cast(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