blob: b392410986dd2e6d2c720b6e30fc2adc240d9dcd [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 Tanousfc76b8a2020-09-28 17:21:52 -07005#include <openssl/rand.h>
6
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08007#include <iostream>
8#include <limits>
Ed Tanous2c6ffdb2023-06-28 11:28:38 -07009#include <string>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080010
Ed Tanousfc76b8a2020-09-28 17:21:52 -070011namespace bmcweb
12{
13
14struct 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 Tanousc160ae72024-03-27 18:45:20 -070022 BMCWEB_LOG_ERROR("Cannot get random number");
Ed Tanousfc76b8a2020-09-28 17:21:52 -070023 err = true;
24 }
25
26 return index;
27 }
28
Ed Tanousad0006e2021-05-11 15:55:47 -070029 static constexpr uint8_t max()
Ed Tanousfc76b8a2020-09-28 17:21:52 -070030 {
31 return std::numeric_limits<uint8_t>::max();
32 }
Ed Tanousad0006e2021-05-11 15:55:47 -070033 static constexpr uint8_t min()
Ed Tanousfc76b8a2020-09-28 17:21:52 -070034 {
35 return std::numeric_limits<uint8_t>::min();
36 }
37
Ed Tanous9eb808c2022-01-25 10:19:23 -080038 bool error() const
Ed Tanousfc76b8a2020-09-28 17:21:52 -070039 {
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 Tanous2c6ffdb2023-06-28 11:28:38 -070052std::string getRandomUUID();
53
Ed Tanousfc76b8a2020-09-28 17:21:52 -070054} // namespace bmcweb