blob: 2cbec849a934ffad9bed413ecf0448bca3e5f669 [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>
Ed Tanous2c6ffdb2023-06-28 11:28:38 -07007#include <string>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008
Ed Tanousfc76b8a2020-09-28 17:21:52 -07009namespace bmcweb
10{
11
12struct OpenSSLGenerator
13{
14 uint8_t operator()()
15 {
16 uint8_t index = 0;
17 int rc = RAND_bytes(&index, sizeof(index));
18 if (rc != opensslSuccess)
19 {
20 std::cerr << "Cannot get random number\n";
21 err = true;
22 }
23
24 return index;
25 }
26
Ed Tanousad0006e2021-05-11 15:55:47 -070027 static constexpr uint8_t max()
Ed Tanousfc76b8a2020-09-28 17:21:52 -070028 {
29 return std::numeric_limits<uint8_t>::max();
30 }
Ed Tanousad0006e2021-05-11 15:55:47 -070031 static constexpr uint8_t min()
Ed Tanousfc76b8a2020-09-28 17:21:52 -070032 {
33 return std::numeric_limits<uint8_t>::min();
34 }
35
Ed Tanous9eb808c2022-01-25 10:19:23 -080036 bool error() const
Ed Tanousfc76b8a2020-09-28 17:21:52 -070037 {
38 return err;
39 }
40
41 // all generators require this variable
42 using result_type = uint8_t;
43
44 private:
45 // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function
46 static constexpr int opensslSuccess = 1;
47 bool err = false;
48};
49
Ed Tanous2c6ffdb2023-06-28 11:28:38 -070050std::string getRandomUUID();
51
Ed Tanousfc76b8a2020-09-28 17:21:52 -070052} // namespace bmcweb