diff --git a/fortuna.cpp b/fortuna.cpp index d2ef561..a0703c5 100644 --- a/fortuna.cpp +++ b/fortuna.cpp @@ -16,6 +16,9 @@ namespace fortuna { + static constexpr const unsigned int min_pool_size{64}; + + Fortuna::Fortuna() { try { initialize_prng(); @@ -28,7 +31,19 @@ namespace fortuna { auto Fortuna::random_data(unsigned int n_bytes) -> void { const auto start{std::chrono::system_clock::now()}; fmt::print("random_data starting - {}\n", start); - incr_reseed_ctr(); + auto now{std::chrono::steady_clock::now()}; + auto elapsed{ + std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch() - + now.time_since_epoch() + ) + }; + fmt::print("last_reseed: {} ago\n", elapsed); + if (sizeof(R.pools[0]) >= min_pool_size && elapsed > R.Gen.reseed_interval) { + // TODO(me): call to generate_random_data will be moved here + incr_reseed_ctr(); + } + R.last_reseed = std::chrono::steady_clock::now(); std::string n{R.Gen.generate_random_data(n_bytes)}; fmt::print("got you {} proper bytes from generate_random_data -> {}\n", @@ -39,6 +54,8 @@ namespace fortuna { std::chrono::duration diff = end-start; fmt::print("random_data done - {}\n", end); fmt::print("getting random data took {:.{}f}s\n", diff.count(), 12); + // TODO(me): eventually call reseed here + // R.Gen.reseed(""); } //random_data } // namespace fortuna diff --git a/fortuna.h b/fortuna.h index a092a16..9744ac3 100644 --- a/fortuna.h +++ b/fortuna.h @@ -6,6 +6,8 @@ #include +#include + namespace fortuna { class Fortuna { @@ -66,6 +68,8 @@ public: unsigned __int128 reseed_ctr; #pragma GCC diagnostic pop accumulator::Pool pools[num_of_pools]; + + std::chrono::steady_clock::time_point last_reseed; }; // class R_state fortuna::Fortuna::R_state R; diff --git a/generator.h b/generator.h index e1bc5a3..4fcf4ab 100644 --- a/generator.h +++ b/generator.h @@ -11,6 +11,8 @@ namespace generator { class Generator { public: + std::chrono::milliseconds reseed_interval{100}; + Generator(); // ad noexcept: perhaps _do_ throw* ~Generator() noexcept;