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