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

52 lines
1.2 KiB
C
Raw Normal View History

2021-12-11 02:24:40 +01:00
#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;
2022-01-10 04:25:03 +01:00
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
2021-12-11 02:24:40 +01:00
try {
2022-01-10 04:25:03 +01:00
std::ifstream urandom("/dev/urandom",
std::ios::in | std::ios::binary);
2021-12-11 02:24:40 +01:00
// check if stream is open
2022-01-10 04:25:03 +01:00
if (urandom) {
2021-12-11 02:24:40 +01:00
urandom.read(reinterpret_cast<char*>(&bytes), bytes.length());
urandom.close();
2022-01-10 04:25:03 +01:00
}
else {
2021-12-11 02:24:40 +01:00
// open failed
fmt::print(stderr, "Failed to open /dev/urandom\n");
}
2022-01-10 04:25:03 +01:00
}
catch (std::ifstream::failure& e) {
2021-12-11 02:24:40 +01:00
fmt::print("io exception caugth: {}\n", e.what());
}
}
auto urandom_entropy_src_service() -> int;
2021-12-11 02:24:40 +01:00
private:
static std::vector<char> bytes[max_event_length];
};
2022-01-10 04:25:03 +01:00
} // namespace accumulator
} // namespace fortuna
#endif // FORTUNA_URANDOM_ENTROPY_SRC_H
2021-12-11 02:24:40 +01:00