blob: 4d4bc04a06ba96c4102e6cba34a0b05a60fcb885 [file] [log] [blame]
Ed Tanousfc76b8a2020-09-28 17:21:52 -07001#pragma once
2
Ed Tanousc160ae72024-03-27 18:45:20 -07003#include "logging.hpp"
4
Ed Tanous6dbe9be2024-04-14 10:24:20 -07005extern "C"
6{
Ed Tanousfc76b8a2020-09-28 17:21:52 -07007#include <openssl/rand.h>
Ed Tanous6dbe9be2024-04-14 10:24:20 -07008}
Ed Tanousfc76b8a2020-09-28 17:21:52 -07009
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080010#include <limits>
Ed Tanous2c6ffdb2023-06-28 11:28:38 -070011#include <string>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080012
Ed Tanousfc76b8a2020-09-28 17:21:52 -070013namespace bmcweb
14{
15
16struct 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 Tanousc160ae72024-03-27 18:45:20 -070024 BMCWEB_LOG_ERROR("Cannot get random number");
Ed Tanousfc76b8a2020-09-28 17:21:52 -070025 err = true;
26 }
27
28 return index;
29 }
30
Ed Tanousad0006e2021-05-11 15:55:47 -070031 static constexpr uint8_t max()
Ed Tanousfc76b8a2020-09-28 17:21:52 -070032 {
33 return std::numeric_limits<uint8_t>::max();
34 }
Ed Tanousad0006e2021-05-11 15:55:47 -070035 static constexpr uint8_t min()
Ed Tanousfc76b8a2020-09-28 17:21:52 -070036 {
37 return std::numeric_limits<uint8_t>::min();
38 }
39
Ed Tanous9eb808c2022-01-25 10:19:23 -080040 bool error() const
Ed Tanousfc76b8a2020-09-28 17:21:52 -070041 {
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 Tanous2c6ffdb2023-06-28 11:28:38 -070054std::string getRandomUUID();
55
Ed Tanousfc76b8a2020-09-28 17:21:52 -070056} // namespace bmcweb