accumulator(pool): (partly) implement add_entropy
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
0ccd60ce1b
commit
2d20242b3e
56
pool.h
56
pool.h
|
@ -1,6 +1,13 @@
|
||||||
#ifndef FORTUNA_POOL_H
|
#ifndef FORTUNA_POOL_H
|
||||||
#define FORTUNA_POOL_H
|
#define FORTUNA_POOL_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include <cryptopp/sha3.h>
|
||||||
|
#include <cryptopp/hex.h>
|
||||||
|
#include <cryptopp/filters.h>
|
||||||
|
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
|
|
||||||
namespace fortuna {
|
namespace fortuna {
|
||||||
|
@ -22,8 +29,57 @@ public:
|
||||||
set_id(id);
|
set_id(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto add_entropy(const uint source, const std::vector<char> &event) -> int {
|
||||||
|
std::string digest{""};
|
||||||
|
std::string event_str;
|
||||||
|
const uint64_t event_size{event.size()};
|
||||||
|
CryptoPP::SHA3_256 sha3_256;
|
||||||
|
|
||||||
|
try {
|
||||||
|
event_str = std::string(event.begin(), event.end());
|
||||||
|
} catch(const std::exception& e) {
|
||||||
|
fmt::print("{}", e.what());
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
CryptoPP::StringSource event_hash(event_str,true,
|
||||||
|
new CryptoPP::HashFilter(sha3_256,
|
||||||
|
new CryptoPP::HexEncoder(
|
||||||
|
new CryptoPP::StringSink(digest),
|
||||||
|
false
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
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:
|
private:
|
||||||
unsigned int pool_id{0};
|
unsigned int pool_id{0};
|
||||||
|
std::string s{""};
|
||||||
|
uint64_t size{0};
|
||||||
|
|
||||||
}; // class Pool
|
}; // class Pool
|
||||||
|
|
||||||
|
|
Reference in New Issue