summaryrefslogtreecommitdiff
path: root/include/ossl_random.hpp
blob: a007556f8b813852b31714c51c6cb48a576d22a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once

#include <cstddef>
#include <cstdint>
#include <limits>
#include <string>
#include <string_view>

namespace bmcweb
{

struct OpenSSLGenerator
{
    uint8_t operator()();

    static constexpr uint8_t max()
    {
        return std::numeric_limits<uint8_t>::max();
    }
    static constexpr uint8_t min()
    {
        return std::numeric_limits<uint8_t>::min();
    }

    bool error() const
    {
        return err;
    }

    // all generators require this variable
    using result_type = uint8_t;

  private:
    // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function
    static constexpr int opensslSuccess = 1;
    bool err = false;
};

std::string getRandomUUID();

std::string getRandomIdOfLength(size_t length);

bool constantTimeStringCompare(std::string_view a, std::string_view b);
struct ConstantTimeCompare
{
    bool operator()(std::string_view a, std::string_view b) const;
};

} // namespace bmcweb