blob: 0e28944d832baeec1f79f053fb3cadd58c11af8b [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 <iostream>
11#include <limits>
Ed Tanous2c6ffdb2023-06-28 11:28:38 -070012#include <string>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080013
Ed Tanousfc76b8a2020-09-28 17:21:52 -070014namespace bmcweb
15{
16
17struct 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 Tanousc160ae72024-03-27 18:45:20 -070025 BMCWEB_LOG_ERROR("Cannot get random number");
Ed Tanousfc76b8a2020-09-28 17:21:52 -070026 err = true;
27 }
28
29 return index;
30 }
31
Ed Tanousad0006e2021-05-11 15:55:47 -070032 static constexpr uint8_t max()
Ed Tanousfc76b8a2020-09-28 17:21:52 -070033 {
34 return std::numeric_limits<uint8_t>::max();
35 }
Ed Tanousad0006e2021-05-11 15:55:47 -070036 static constexpr uint8_t min()
Ed Tanousfc76b8a2020-09-28 17:21:52 -070037 {
38 return std::numeric_limits<uint8_t>::min();
39 }
40
Ed Tanous9eb808c2022-01-25 10:19:23 -080041 bool error() const
Ed Tanousfc76b8a2020-09-28 17:21:52 -070042 {
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 Tanous2c6ffdb2023-06-28 11:28:38 -070055std::string getRandomUUID();
56
Ed Tanousfc76b8a2020-09-28 17:21:52 -070057} // namespace bmcweb