Virgil Security Crypto library  2.6.3
VirgilOperationHash.h
1 
37 #ifndef VIRGIL_CRYPTO_VIRGIL_HASH_H
38 #define VIRGIL_CRYPTO_VIRGIL_HASH_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  VirgilOperationHash(Impl impl) : self_(new Model<Impl>(std::move(impl))) {}
64 
71  self_->doStart();
72  self_->doUpdate(data);
73  return self_->doFinish();
74  }
75 
79  void start() {
80  self_->doStart();
81  }
82 
87  void update(const VirgilByteArray& data) {
88  self_->doUpdate(data);
89  }
90 
96  return self_->doFinish();
97  }
98 
103 
105  VirgilOperationHash(const VirgilOperationHash& other) : self_(other.self_->doCopy()) {}
106 
107  VirgilOperationHash(VirgilOperationHash&& other)noexcept = default;
108 
109  VirgilOperationHash& operator=(const VirgilOperationHash& other) {
110  VirgilOperationHash tmp(other);
111  *this = std::move(tmp);
112  return *this;
113  }
114 
115  VirgilOperationHash& operator=(VirgilOperationHash&& other) noexcept= default;
116 
117  ~VirgilOperationHash() noexcept = default;
119 
120 private:
121  struct Concept {
122  virtual Concept* doCopy() const = 0;
123 
124  virtual void doStart() = 0;
125 
126  virtual void doUpdate(const VirgilByteArray& data) = 0;
127 
128  virtual VirgilByteArray doFinish() = 0;
129 
130  virtual ~Concept() noexcept = default;
131 };
132 
133  template<class Impl>
134  struct Model : Concept {
135 
136  explicit Model(Impl impl) : impl_(std::move(impl)) {}
137 
138  Concept* doCopy() const override {
139  return new Model(*this);
140  }
141 
142  void doStart() override {
143  return impl_.start();
144  }
145 
146  void doUpdate(const VirgilByteArray& data) override {
147  impl_.update(data);
148  }
149 
150  VirgilByteArray doFinish() override {
151  return impl_.finish();
152  }
153 
154  private:
155  Impl impl_;
156  };
157 
158 private:
159  std::unique_ptr<Concept> self_;
160 };
161 
162 }}}
163 
164 #endif //VIRGIL_CRYPTO_VIRGIL_HASH_H
Define proxy interface for the Hash (Message Digest) functionality.
Definition: VirgilOperationHash.h:51
VirgilByteArray finish()
Finalize hashing.
Definition: VirgilOperationHash.h:95
Definition: VirgilHash.h:265
void update(const VirgilByteArray &data)
Hash new portion of the data.
Definition: VirgilOperationHash.h:87
Root namespace for all Virgil Security libraries.
Definition: VirgilAsn1Compatible.h:46
std::vector< unsigned char > VirgilByteArray
This type represents a sequence of bytes.
Definition: VirgilByteArray.h:50
VirgilOperationHash(Impl impl)
Captures implementation object.
Definition: VirgilOperationHash.h:63
VirgilByteArray hash(const VirgilByteArray &data)
Calculate digest for given data.
Definition: VirgilOperationHash.h:70
static VirgilOperationHash getDefault()
Return default implementation.
void start()
Prepare internal state for the new hashing.
Definition: VirgilOperationHash.h:79