Virgil Security Crypto library  2.2.2
VirgilOperationHash.h
1 
37 #ifndef VIRGIL_CRYPTO_VIRGIL_HASH_H
38 #define VIRGIL_CRYPTO_VIRGIL_HASH_H
39 
40 #include <virgil/crypto/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 
131  template<class Impl>
132  struct Model : Concept {
133 
134  explicit Model(Impl impl) : impl_(std::move(impl)) {}
135 
136  Concept* doCopy() const override {
137  return new Model(*this);
138  }
139 
140  void doStart() override {
141  return impl_.start();
142  }
143 
144  void doUpdate(const VirgilByteArray& data) override {
145  impl_.update(data);
146  }
147 
148  VirgilByteArray doFinish() override {
149  return impl_.finish();
150  }
151 
152  private:
153  Impl impl_;
154  };
155 
156 private:
157  std::unique_ptr<Concept> self_;
158 };
159 
160 }}}
161 
162 #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:54
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