This repository has been archived on 2022-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
fortuna/fortuna.cpp

64 lines
1.7 KiB
C++
Raw Normal View History

#ifndef FORTUNA_FORTUNA_CPP
#define FORTUNA_FORTUNA_CPP
2021-11-10 23:55:58 +01:00
#include "fortuna.h"
#include <cryptopp/cryptlib.h>
#include <cryptopp/osrng.h>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <cryptopp/serpent.h>
#include <cryptopp/ccm.h>
#include <fmt/core.h>
#include <fmt/chrono.h>
#include <chrono>
2021-11-10 23:55:58 +01:00
namespace fortuna {
static constexpr const unsigned int min_pool_size{64};
2021-12-08 03:31:47 +01:00
Fortuna::Fortuna() {
try {
initialize_prng();
} catch(CryptoPP::Exception& e) {
fmt::print(stderr, "{}\n", e.what());
}
2021-11-10 23:55:58 +01:00
}
Fortuna::~Fortuna() = default;
auto Fortuna::random_data(unsigned int n_bytes) -> void {
const auto start{std::chrono::system_clock::now()};
fmt::print("random_data starting - {}\n", start);
auto now{std::chrono::steady_clock::now()};
auto elapsed{
std::chrono::duration_cast<std::chrono::milliseconds>(
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();
2021-12-07 15:46:01 +01:00
std::string n{R.Gen.generate_random_data(n_bytes)};
fmt::print("got you {} proper bytes from generate_random_data -> {}\n",
n_bytes, n);
2021-12-11 01:20:34 +01:00
n.clear();
const auto end{std::chrono::system_clock::now()};
std::chrono::duration<float> 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
2021-11-10 23:55:58 +01:00
} // namespace fortuna
#endif//FORTUNA_FORTUNA_CPP