AWS SDK for C++
AWS SDK for C++
Loading...
Searching...
No Matches
SecureRandom.h
Go to the documentation of this file.
1
5#pragma once
6#include <type_traits>
7
8namespace Aws
9{
10 namespace Utils
11 {
12 namespace Crypto
13 {
21 {
22 public:
24 {
25 }
26
27 virtual ~SecureRandomBytes() = default;
28
32 virtual void GetBytes(unsigned char* buffer, size_t bufferSize) = 0;
33
37 operator bool() const { return !m_failure; }
38
39 protected:
41 };
42
46 template <typename DataType = uint64_t>
48 {
49 public:
56 SecureRandom(const std::shared_ptr<SecureRandomBytes>& entropySource) : m_entropy(entropySource)
57 { static_assert(std::is_unsigned<DataType>::value, "Type DataType must be integral"); }
58
59 virtual ~SecureRandom() = default;
60
61 virtual void Reset() {}
62
66 virtual DataType operator()()
67 {
68 DataType value(0);
69 unsigned char buffer[sizeof(DataType)];
70 m_entropy->GetBytes(buffer, sizeof(DataType));
71
72 assert(*m_entropy);
73 if(*m_entropy)
74 {
75 for (size_t i = 0; i < sizeof(DataType); ++i)
76 {
77 value <<= 8;
78 value |= buffer[i];
79
80 }
81 }
82
83 return value;
84 }
85
86 operator bool() const { return *m_entropy; }
87
88 private:
89 std::shared_ptr<SecureRandomBytes> m_entropy;
90 };
91
93 {
94 public:
95 virtual ~SecureRandomFactory() = default;
96
100 virtual std::shared_ptr<SecureRandomBytes> CreateImplementation() const = 0;
101
106 virtual void InitStaticState() {}
107
112 virtual void CleanupStaticState() {}
113 };
114 }
115 }
116}
char * buffer
Definition: cJSON.h:174
virtual void GetBytes(unsigned char *buffer, size_t bufferSize)=0
virtual std::shared_ptr< SecureRandomBytes > CreateImplementation() const =0
virtual DataType operator()()
Definition: SecureRandom.h:66
SecureRandom(const std::shared_ptr< SecureRandomBytes > &entropySource)
Definition: SecureRandom.h:56