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