diff options
Diffstat (limited to 'include/ossl_random.hpp')
-rw-r--r-- | include/ossl_random.hpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/include/ossl_random.hpp b/include/ossl_random.hpp new file mode 100644 index 0000000000..2cbec849a9 --- /dev/null +++ b/include/ossl_random.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include <openssl/rand.h> + +#include <iostream> +#include <limits> +#include <string> + +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; + } + + static constexpr uint8_t max() + { + return std::numeric_limits<uint8_t>::max(); + } + static constexpr uint8_t min() + { + return std::numeric_limits<uint8_t>::min(); + } + + bool error() const + { + 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; +}; + +std::string getRandomUUID(); + +} // namespace bmcweb |