Ed Tanous | fc76b8a | 2020-09-28 17:21:52 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <openssl/rand.h> |
| 4 | |
| 5 | namespace bmcweb |
| 6 | { |
| 7 | |
| 8 | struct OpenSSLGenerator |
| 9 | { |
| 10 | uint8_t operator()() |
| 11 | { |
| 12 | uint8_t index = 0; |
| 13 | int rc = RAND_bytes(&index, sizeof(index)); |
| 14 | if (rc != opensslSuccess) |
| 15 | { |
| 16 | std::cerr << "Cannot get random number\n"; |
| 17 | err = true; |
| 18 | } |
| 19 | |
| 20 | return index; |
| 21 | } |
| 22 | |
| 23 | uint8_t max() |
| 24 | { |
| 25 | return std::numeric_limits<uint8_t>::max(); |
| 26 | } |
| 27 | uint8_t min() |
| 28 | { |
| 29 | return std::numeric_limits<uint8_t>::min(); |
| 30 | } |
| 31 | |
| 32 | bool error() |
| 33 | { |
| 34 | return err; |
| 35 | } |
| 36 | |
| 37 | // all generators require this variable |
| 38 | using result_type = uint8_t; |
| 39 | |
| 40 | private: |
| 41 | // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function |
| 42 | static constexpr int opensslSuccess = 1; |
| 43 | bool err = false; |
| 44 | }; |
| 45 | |
| 46 | } // namespace bmcweb |