77 lines
1.5 KiB
C++
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
|