pool: move logic to the source file
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
4b216a6f6e
commit
ebcc4f87d5
77
pool.cpp
77
pool.cpp
|
@ -6,7 +6,78 @@
|
||||||
namespace fortuna {
|
namespace fortuna {
|
||||||
namespace accumulator {
|
namespace accumulator {
|
||||||
|
|
||||||
} //namespace accumulator
|
auto Pool::set_id(unsigned int id) -> void {
|
||||||
} //namespace fortuna
|
pool_id = id;
|
||||||
|
}
|
||||||
|
|
||||||
#endif//FORTUNA_POOL_CPP
|
auto Pool::initialize_pool(unsigned int id) -> void {
|
||||||
|
set_id(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
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("{}", e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace accumulator
|
||||||
|
} // namespace fortuna
|
||||||
|
|
||||||
|
#endif // FORTUNA_POOL_CPP
|
||||||
|
|
69
pool.h
69
pool.h
|
@ -15,78 +15,23 @@ public:
|
||||||
Pool(const Pool& pool) = delete; // no copy
|
Pool(const Pool& pool) = delete; // no copy
|
||||||
~Pool() noexcept = default;
|
~Pool() noexcept = default;
|
||||||
|
|
||||||
// TODO(me): this public setter should be fixed?
|
auto set_id(unsigned int id) -> void;
|
||||||
auto set_id(unsigned int id) -> void {
|
|
||||||
pool_id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto get_id() const -> unsigned int;
|
auto get_id() const -> unsigned int;
|
||||||
|
|
||||||
auto initialize_pool(unsigned int id) -> void {
|
auto initialize_pool(unsigned int id) -> void;
|
||||||
set_id(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add entropy contained in a random event of 1 to 32 bytes
|
// add entropy contained in a random event of 1 to 32 bytes
|
||||||
auto add_entropy(const uint source, const std::vector<char>& event) -> int {
|
auto add_entropy(const uint source, const std::vector<char>& event) -> int;
|
||||||
std::string event_str;
|
|
||||||
const size_t event_size{event.size()};
|
|
||||||
|
|
||||||
try {
|
auto get_s_length() const -> uint64_t;
|
||||||
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 {
|
auto get_s() const -> std::string;
|
||||||
event_str = std::string(event.begin(), event.end());
|
|
||||||
}
|
|
||||||
catch (const std::exception& e) {
|
|
||||||
fmt::print("{}", e.what());
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
auto clear_pool() -> void;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto get_s_length() const -> uint64_t {
|
|
||||||
// returns total length of entropy contained in this pool
|
|
||||||
return this->s.length();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto get_s() const -> std::string {
|
|
||||||
return this->s;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto clear_pool() -> void {
|
|
||||||
this->s.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
auto append_s(const std::string& entropy_s) -> void {
|
auto append_s(const std::string& entropy_s) -> void;
|
||||||
try {
|
|
||||||
(this->s).append(std::string(entropy_s));
|
|
||||||
}
|
|
||||||
catch (const std::exception& e) {
|
|
||||||
fmt::print("{}", e.what());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int pool_id{0};
|
unsigned int pool_id{0};
|
||||||
|
|
Reference in New Issue