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/urandom_entropy_src.cpp
surtur 03578f9016
All checks were successful
continuous-integration/drone/push Build is passing
accumulator: refactor entropy_src,add files
* add event_scheduler_impl.h and properly override its base class
* add urandom_entropy_src.cpp, move there logic from header
* add urandom_entropy_src.cpp to CMakeLists.txt
* add unique_ptr per impl: one for both EventSchedulerImpl and
  EventAdderImpl each
* wrap a call in reinterpret_cast and reformat code
* add missing includes (best effort)
2022-01-13 05:40:33 +01:00

44 lines
1.1 KiB
C++

#ifndef FORTUNA_URANDOM_ENTROPY_SRC_CPP
#define FORTUNA_URANDOM_ENTROPY_SRC_CPP
#include "urandom_entropy_src.h"
namespace fortuna {
namespace accumulator {
std::vector<char> UrandomEntropySrc::bytes{};
auto UrandomEntropySrc::schedule(accumulator::EventSchedulerImpl scheduler)
-> void {
scheduler.schedule(std::chrono::milliseconds(100));
}
auto UrandomEntropySrc::event(accumulator::EventAdderImpl adder) -> void {
std::ifstream file;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
std::ifstream urandom("/dev/urandom", std::ios::in | std::ios::binary);
// check if stream is open
if (urandom) {
urandom.read(
reinterpret_cast<char*>(&UrandomEntropySrc::bytes),
reinterpret_cast<uint64_t>(UrandomEntropySrc::bytes.size()));
urandom.close();
}
else {
// open failed
fmt::print(stderr, "Failed to open /dev/urandom\n");
}
}
catch (std::ifstream::failure& e) {
fmt::print("io exception caugth: {}\n", e.what());
}
}
} // namespace accumulator
} // namespace fortuna
#endif // FORTUNA_URANDOM_ENTROPY_SRC_CPP