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