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/accumulator.cpp
surtur 767da88531
All checks were successful
continuous-integration/drone/push Build is passing
accumulator: check entropy sources before adding
2022-01-11 05:38:02 +01:00

77 lines
1.5 KiB
C++

#ifndef FORTUNA_ACCUMULATOR_CPP
#define FORTUNA_ACCUMULATOR_CPP
#include "accumulator.h"
#include <fmt/core.h>
#include <chrono>
#include <cstdint>
#include <thread>
#include <vector>
namespace fortuna {
namespace accumulator {
auto wait_for(uint milliseconds) -> void {
std::this_thread::sleep_for(std::chrono::milliseconds{milliseconds});
}
Accumulator::Accumulator() noexcept {
this->Gen = nullptr;
}
Accumulator::~Accumulator() noexcept {};
// check if given source id is an id of an already registered entropy source
auto Accumulator::src_is_registered(const uint8_t& id) -> bool {
bool reg{false};
static uint8_t _src_id{};
for (auto& src_id: this->entropy_sources) {
if (src_id == id) {
reg = true;
_src_id = id;
break;
}
}
if (reg) {
fmt::print(
"[!] acc(add_source): entropy source \"{}\" already registered",
_src_id);
return true;
}
return false;
}
auto Accumulator::set_gen(fortuna::generator::Generator& gen) -> void {
this->Gen = std::move(&gen);
}
auto Accumulator::get_random_data(uint n_bytes) -> std::string {
std::string data{""};
try {
data = this->Gen->generate_random_data(n_bytes);
}
catch (std::exception& e) {
fmt::print("{}", e.what());
}
return data;
}
auto Accumulator::call_reseed(const std::string& seed) -> void {
try {
this->Gen->reseed(seed);
}
catch (std::exception& e) {
fmt::print("{}", e.what());
}
}
} // namespace accumulator
} // namespace fortuna
#endif // FORTUNA_ACCUMULATOR_CPP