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.h
surtur 4b216a6f6e
All checks were successful
continuous-integration/drone/push Build is passing
refactor: clang-format
2022-01-10 04:25:03 +01:00

52 lines
1.2 KiB
C++

#ifndef FORTUNA_URANDOM_ENTROPY_SRC_H
#define FORTUNA_URANDOM_ENTROPY_SRC_H
#include "entropy_src.h"
#include <fmt/core.h>
#include <fstream>
namespace fortuna {
namespace accumulator {
class UrandomEntropySrc : public EntropySrc {
public:
static constexpr const std::size_t max_event_length{32};
auto schedule(accumulator::EventScheduler scheduler) -> void {
scheduler.schedule(std::chrono::milliseconds(100));
}
auto event(accumulator::EventAdder 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*>(&bytes), bytes.length());
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());
}
}
auto urandom_entropy_src_service() -> int;
private:
static std::vector<char> bytes[max_event_length];
};
} // namespace accumulator
} // namespace fortuna
#endif // FORTUNA_URANDOM_ENTROPY_SRC_H