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 e78ac038db
All checks were successful
continuous-integration/drone/push Build is passing
accumulator: don't move &gen
2022-01-17 07:32:57 +01:00

78 lines
1.6 KiB
C++

#ifndef FORTUNA_ACCUMULATOR_CPP
#define FORTUNA_ACCUMULATOR_CPP
#include "accumulator.h"
#include <fmt/core.h>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <thread>
#include <vector>
namespace fortuna {
namespace accumulator {
auto wait_for(const unsigned int& 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{};
if (std::any_of(this->entropy_sources.begin(),
this->entropy_sources.end(),
[&](auto& src_id) { return src_id == id; })) {
reg = true;
_src_id = id;
}
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); // TODO(me): does this make sense?
this->Gen = &gen;
}
auto Accumulator::get_random_data(const unsigned int& 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