diff --git a/generator.cpp b/generator.cpp index dcc9c83..0b86f94 100644 --- a/generator.cpp +++ b/generator.cpp @@ -4,10 +4,6 @@ #include "generator.h" #include "util.h" -#include -#include -#include - #include #include #include @@ -15,6 +11,10 @@ #include #include +#include +#include +#include + namespace fortuna { namespace generator { @@ -38,13 +38,6 @@ void Generator::initialize_generator(){ fmt::print(stderr, "{}\n", e.what()); exit(1); } - try { - // FIXME: hardcoded seed for the time being - reseed("fortuna"); - } catch(CryptoPP::Exception& e) { - fmt::print(stderr, "{}\n", e.what()); - exit(1); - } }; auto Generator::get_state() -> G_state { @@ -60,9 +53,11 @@ auto Generator::reseed(const std::string& s) -> void { // fmt::print("concat \"da_key + s\" -> {}\n", to_be_hashed); // debugging try { + std::lock_guard lg(mtx); std::string a{fortuna::Util::do_sha(da_key + s)}; std::memmove(&G.k[0], &a[0], G.k.SizeInBytes()); ++G.ctr; + fmt::print("[i] generator: reseeded\n"); } catch(std::exception& e) { fmt::print("{}", e.what()); } @@ -153,6 +148,7 @@ auto Generator::generate_blocks(unsigned int k_blocks) -> std::string { } auto Generator::generate_random_data(uint n) -> std::string { + std::lock_guard lg(mtx); // fmt::print("n -> {}\n", n); // debugging if (n == 0) { // do not do this..? diff --git a/generator.h b/generator.h index 86c94ba..8a5245a 100644 --- a/generator.h +++ b/generator.h @@ -4,6 +4,8 @@ #include #include +#include +#include #include namespace fortuna { @@ -12,6 +14,7 @@ namespace generator { class Generator { public: std::chrono::milliseconds reseed_interval{100}; + std::mutex mtx; Generator(); // ad noexcept: perhaps _do_ throw* Generator(const Generator& Gen) = delete; // no @@ -27,7 +30,6 @@ public: return !(this->G.ctr == 0x00); }; - auto generator_service() -> int; private: struct G_state {