AWS SDK for C++

AWS SDK for C++ Version 1.11.716

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