Virgil Security Crypto library  2.4.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
VirgilOperationRandom.h
1 
37 #ifndef VIRGIL_CRYPTO_VIRGIL_RANDOM_H
38 #define VIRGIL_CRYPTO_VIRGIL_RANDOM_H
39 
40 #include "../VirgilByteArray.h"
41 
42 #include <memory>
43 
44 namespace virgil { namespace crypto { inline namespace primitive {
45 
52 private:
53  template<class Impl>
54  struct Model;
55 
56 public:
62  template<class Impl>
63  VirgilOperationRandom(Impl impl) : self_(new Model<Impl>(std::move(impl))) {}
64 
70  VirgilByteArray randomize(size_t bytesNum) {
71  return self_->doRandomize(bytesNum);
72  }
73 
78 
80  VirgilOperationRandom(const VirgilOperationRandom& other) : self_(other.self_->doCopy()) {}
81 
82  VirgilOperationRandom(VirgilOperationRandom&& other) noexcept = default;
83 
84  VirgilOperationRandom& operator=(const VirgilOperationRandom& other) {
85  VirgilOperationRandom tmp(other);
86  *this = std::move(tmp);
87  return *this;
88  }
89 
90  VirgilOperationRandom& operator=(VirgilOperationRandom&& other) noexcept = default;
91 
92  ~VirgilOperationRandom() noexcept = default;
94 
95 private:
96  struct Concept {
97  virtual VirgilByteArray doRandomize(size_t bytesNum) = 0;
98 
99  virtual Concept* doCopy() const = 0;
100 
101  virtual ~Concept() noexcept = default;
102  };
103 
104  template<class Random>
105  struct Model : Concept {
106  explicit Model(Random randomImpl) : impl_(std::move(randomImpl)) {}
107 
108  VirgilByteArray doRandomize(size_t bytesNum) override {
109  return impl_.randomize(bytesNum);
110  }
111 
112  Concept* doCopy() const override {
113  return new Model(*this);
114  }
115 
116  private:
117  Random impl_;
118  };
119 
120 private:
121  std::unique_ptr<Concept> self_;
122 };
123 
124 }}}
125 
126 #endif //VIRGIL_CRYPTO_VIRGIL_RANDOM_H
VirgilByteArray randomize(size_t bytesNum)
Return random sequence.
Definition: VirgilOperationRandom.h:70
static VirgilOperationRandom getDefault()
Return default implementation.
std::vector< unsigned char > VirgilByteArray
This type represents a sequence of bytes.
Definition: VirgilByteArray.h:50
Define proxy interface for the Randomization functionality.
Definition: VirgilOperationRandom.h:51
VirgilOperationRandom(Impl impl)
Captures implementation object.
Definition: VirgilOperationRandom.h:63