accumulator: add urandom_entropy_src
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2021-12-11 02:24:40 +01:00
parent a5b988ba71
commit d0e9b08886
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 47 additions and 1 deletions

View File

@ -183,7 +183,7 @@ add_subdirectory(lib/fmt EXCLUDE_FROM_ALL)
endif(NOT CMAKE_EXE_LINKER_FLAGS MATCHES "-fuse-ld=lld")
endif()
add_executable(fortuna main.cpp generator.cpp generator.h fortuna.cpp fortuna.h accumulator.cpp accumulator.h pool.cpp pool.h event_adder.h event_adder_impl.h event_scheduler.h entropy_src.h)
add_executable(fortuna main.cpp generator.cpp generator.h fortuna.cpp fortuna.h accumulator.cpp accumulator.h pool.cpp pool.h event_adder.h event_adder_impl.h event_scheduler.h entropy_src.h urandom_entropy_src.h)
# ref: https://cmake.org/pipermail/cmake/2016-May/063400.html
target_link_libraries(fortuna
PRIVATE cryptopp

46
urandom_entropy_src.h Normal file
View File

@ -0,0 +1,46 @@
#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());
}
}
private:
static std::vector<char> bytes[max_event_length];
};
} //namespace accumulator
} //namespace fortuna
#endif//FORTUNA_URANDOM_ENTROPY_SRC_H