blob: 082c4e300831d4061dee34885de092eb5a62a8ed [file] [log] [blame]
Ed Tanousfc76b8a2020-09-28 17:21:52 -07001#pragma once
2
3#include <openssl/rand.h>
4
5namespace bmcweb
6{
7
8struct OpenSSLGenerator
9{
10 uint8_t operator()()
11 {
12 uint8_t index = 0;
13 int rc = RAND_bytes(&index, sizeof(index));
14 if (rc != opensslSuccess)
15 {
16 std::cerr << "Cannot get random number\n";
17 err = true;
18 }
19
20 return index;
21 }
22
Ed Tanousad0006e2021-05-11 15:55:47 -070023 static constexpr uint8_t max()
Ed Tanousfc76b8a2020-09-28 17:21:52 -070024 {
25 return std::numeric_limits<uint8_t>::max();
26 }
Ed Tanousad0006e2021-05-11 15:55:47 -070027 static constexpr uint8_t min()
Ed Tanousfc76b8a2020-09-28 17:21:52 -070028 {
29 return std::numeric_limits<uint8_t>::min();
30 }
31
Ed Tanous9eb808c2022-01-25 10:19:23 -080032 bool error() const
Ed Tanousfc76b8a2020-09-28 17:21:52 -070033 {
34 return err;
35 }
36
37 // all generators require this variable
38 using result_type = uint8_t;
39
40 private:
41 // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function
42 static constexpr int opensslSuccess = 1;
43 bool err = false;
44};
45
46} // namespace bmcweb