52 lines
1.2 KiB
C++
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
|
|
|