Move Openssl Generator
This commit moves the openssl random number generator into its own file,
so it can be used in EventService, and moves it to its own file. Seeding a
random number generator with time is bad practice in general, so much so
that there's a CERT rule about it as well as a clang-tidy check.
https://clang.llvm.org/extra/clang-tidy/checks/cert-msc51-cpp.html
This doesn't matter much in this case, as we're generating a randomized
int for an ID, but it will matter in other cases, and we'd like to have
the check on to verify that.
Change-Id: I8e6aebb7962d259045ffd558eea22f07f9c23821
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/include/random.hpp b/include/random.hpp
new file mode 100644
index 0000000..d11c249
--- /dev/null
+++ b/include/random.hpp
@@ -0,0 +1,46 @@
+#pragma once
+
+#include <openssl/rand.h>
+
+namespace bmcweb
+{
+
+struct OpenSSLGenerator
+{
+ uint8_t operator()()
+ {
+ uint8_t index = 0;
+ int rc = RAND_bytes(&index, sizeof(index));
+ if (rc != opensslSuccess)
+ {
+ std::cerr << "Cannot get random number\n";
+ err = true;
+ }
+
+ return index;
+ }
+
+ uint8_t max()
+ {
+ return std::numeric_limits<uint8_t>::max();
+ }
+ uint8_t min()
+ {
+ return std::numeric_limits<uint8_t>::min();
+ }
+
+ bool error()
+ {
+ return err;
+ }
+
+ // all generators require this variable
+ using result_type = uint8_t;
+
+ private:
+ // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function
+ static constexpr int opensslSuccess = 1;
+ bool err = false;
+};
+
+} // namespace bmcweb