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