surtur
65c476dbd6
All checks were successful
continuous-integration/drone/push Build is passing
* rm duplicate do_sha() code, consolidate in Util * make reseed() public so that it can be called from outside * rm reseed() from do_crypto() where it has no place
81 lines
1.6 KiB
C++
81 lines
1.6 KiB
C++
#ifndef FORTUNA_POOL_H
|
|
#define FORTUNA_POOL_H
|
|
|
|
#include "util.h"
|
|
|
|
#include <fmt/core.h>
|
|
#include <stdexcept>
|
|
|
|
namespace fortuna {
|
|
namespace accumulator {
|
|
|
|
class Pool {
|
|
public:
|
|
Pool(){};
|
|
~Pool() = default;
|
|
|
|
// TODO(me): this public setter should be fixed?
|
|
auto set_id(unsigned int id) -> void {
|
|
pool_id = id;
|
|
}
|
|
|
|
auto get_id() -> unsigned int;
|
|
|
|
auto initialize_pool(unsigned int id) -> void {
|
|
set_id(id);
|
|
}
|
|
|
|
auto 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 < 0 || 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;
|
|
set_s(digest);
|
|
digest.clear();
|
|
} catch(const std::exception& e) {
|
|
fmt::print("{}", e.what());
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
protected:
|
|
auto set_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:
|
|
unsigned int pool_id{0};
|
|
std::string s{""};
|
|
uint64_t size{0};
|
|
|
|
}; // class Pool
|
|
|
|
} //namespace accumulator
|
|
} //namespace fortuna
|
|
|
|
#endif//FORTUNA_POOL_H
|