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