AWS SDK for C++

AWS SDK for C++ Version 1.11.440

Loading...
Searching...
No Matches
SecureRandom.h
1
5#pragma once
6#include <type_traits>
7#include <memory>
8#include <cassert>
9
10namespace Aws
11{
12 namespace Utils
13 {
14 namespace Crypto
15 {
23 {
24 public:
26 {
27 }
28
29 virtual ~SecureRandomBytes() = default;
30
34 virtual void GetBytes(unsigned char* buffer, size_t bufferSize) = 0;
35
39 operator bool() const { return !m_failure; }
40
41 protected:
43 };
44
48 template <typename DataType = uint64_t>
50 {
51 public:
58 SecureRandom(const std::shared_ptr<SecureRandomBytes>& entropySource) : m_entropy(entropySource)
59 { static_assert(std::is_unsigned<DataType>::value, "Type DataType must be integral"); }
60
61 virtual ~SecureRandom() = default;
62
63 virtual void Reset() {}
64
68 virtual DataType operator()()
69 {
70 DataType value(0);
71 unsigned char buffer[sizeof(DataType)];
72 m_entropy->GetBytes(buffer, sizeof(DataType));
73
74 assert(*m_entropy);
75 if(*m_entropy)
76 {
77 for (size_t i = 0; i < sizeof(DataType); ++i)
78 {
79 value <<= 8;
80 value |= buffer[i];
81
82 }
83 }
84
85 return value;
86 }
87
88 operator bool() const { return *m_entropy; }
89
90 private:
91 std::shared_ptr<SecureRandomBytes> m_entropy;
92 };
93
95 {
96 public:
97 virtual ~SecureRandomFactory() = default;
98
102 virtual std::shared_ptr<SecureRandomBytes> CreateImplementation() const = 0;
103
108 virtual void InitStaticState() {}
109
114 virtual void CleanupStaticState() {}
115 };
116 }
117 }
118}
virtual void GetBytes(unsigned char *buffer, size_t bufferSize)=0
virtual std::shared_ptr< SecureRandomBytes > CreateImplementation() const =0
SecureRandom(const std::shared_ptr< SecureRandomBytes > &entropySource)