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