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/pool.cpp

92 lines
1.8 KiB
C++
Raw Normal View History

#ifndef FORTUNA_POOL_CPP
#define FORTUNA_POOL_CPP
#include "pool.h"
#include <fmt/core.h>
namespace fortuna {
namespace accumulator {
auto Pool::set_id(const unsigned int& id) noexcept -> void {
this->pool_id = id;
}
auto Pool::get_id() const noexcept -> unsigned int {
return this->pool_id;
2022-01-10 05:07:18 +01:00
}
auto Pool::initialize_pool(const unsigned int& id) -> void {
fmt::print("\tpool init: {}\n", id);
2022-01-10 05:07:18 +01:00
set_id(id);
fmt::print("\tid set to: {}\n", this->get_id());
2022-01-10 05:07:18 +01:00
}
auto Pool::add_entropy(const uint source, const std::vector<char>& event)
-> int {
std::string event_str;
const size_t event_size{event.size()};
try {
if (source > 255) {
throw std::invalid_argument(
"source number outside of interval <0,255>\n");
}
if (event_size < 1 || event_size > 32) {
throw std::invalid_argument("the length of the event needs to "
"be from the interval <1,32>\n");
}
}
catch (const std::exception& e) {
fmt::print("{}", e.what());
}
try {
event_str = std::string(event.begin(), event.end());
}
catch (const std::exception& e) {
fmt::print("{}", e.what());
}
try {
std::string digest(fortuna::Util::do_sha(event_str));
size += event_size;
append_s(digest);
digest.clear();
}
catch (const std::exception& e) {
fmt::print("{}", e.what());
}
return 0;
} // add_entropy
auto Pool::get_s_length() const -> uint64_t {
// returns total length of entropy contained in this pool
return this->s.length();
}
auto Pool::get_s() const -> std::string {
return this->s;
}
auto Pool::clear_pool() -> void {
this->s.clear();
}
auto Pool::append_s(const std::string& entropy_s) -> void {
try {
(this->s).append(std::string(entropy_s));
}
catch (const std::exception& e) {
fmt::print("{}\n", e.what());
2022-01-10 05:07:18 +01:00
}
}
} // namespace accumulator
} // namespace fortuna
#endif // FORTUNA_POOL_CPP