HashContext.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HashContext.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "Blob.h"
14 #include "ByteArray.h"
15 
16 namespace Crypto
17 {
22 template <class Engine_> class HashContext
23 {
24 public:
25  using Engine = Engine_;
26  using Hash = ByteArray<Engine::hashsize>;
27 
28  struct State {
29  ByteArray<Engine::statesize> value;
30  uint64_t count;
31  };
32 
33  template <typename... EngineArgs> HashContext(EngineArgs&&... engineArgs)
34  {
35  reset(std::forward<EngineArgs>(engineArgs)...);
36  }
37 
41  template <typename... EngineArgs> HashContext& reset(EngineArgs&&... engineArgs)
42  {
43  engine.init(std::forward<EngineArgs>(engineArgs)...);
44  return *this;
45  }
46 
52  template <typename... Ts> Hash calculate(Ts&&... args)
53  {
54  update(std::forward<Ts>(args)...);
55  return getHash();
56  }
57 
63  HashContext& update(const Blob& blob)
65  {
66  return update(blob.data(), blob.size());
67  }
68 
71  {
72  uint8_t buf[256];
73  size_t offset = 0;
74  size_t len;
75  while((len = obj.read(offset, buf, sizeof(buf))) > 0) {
76  engine.update(buf, len);
77  offset += len;
78  }
79  return *this;
80  }
81 
87  HashContext& update(const void* data, size_t size)
88  {
89  engine.update(data, size);
90  return *this;
91  }
92 
94  template <size_t size_> HashContext& update(const ByteArray<size_>& array)
95  {
96  return update(array.data(), array.size());
97  }
104  Hash getHash()
105  {
106  Hash hash{};
107  engine.final(hash.data());
108  return hash;
109  }
110 
117  State getState()
118  {
119  State state;
120  state.count = engine.get_state(state.value.data());
121  return state;
122  }
123 
133  void setState(const State& state)
134  {
135  engine.set_state(state.value.data(), state.count);
136  }
137 
138 private:
139  Engine engine;
140 };
141 
142 } // namespace Crypto
size_t size() const
Definition: Blob.h:52
uint64_t count
Definition: HashContext.h:46
size_t read(size_t offset, void *buffer, size_t count) const
Read contents of a String into RAM.
Definition: ObjectBase.hpp:110
Wraps a pointer to some data with size.
Definition: Blob.h:28
void setState(const State &state)
Restore intermediate hash state.
Definition: HashContext.h:149
Used when defining data structures.
Definition: ObjectBase.hpp:51
HashContext(EngineArgs &&... engineArgs)
Definition: HashContext.h:49
Class template for a Hash implementation 'Context'.
Definition: HashContext.h:30
Hash getHash()
Finalise and return the final hash value.
Definition: HashContext.h:120
Definition: HashContext.h:44
ByteArray< Engine::hashsize > Hash
Definition: HashContext.h:42
ByteArray< Engine::statesize > value
Definition: HashContext.h:45
State getState()
Get intermediate hash state.
Definition: HashContext.h:133
const uint8_t * data() const
Definition: Blob.h:47
Definition: Blake2s.h:18
Engine_ Engine
Definition: HashContext.h:41
HashContext & update(const Blob &blob)
Data from Blob.
Definition: HashContext.h:80
HashContext & reset(EngineArgs &&... engineArgs)
Reset the context for a new calculation.
Definition: HashContext.h:57
Hash calculate(Ts &&... args)
Calculate hash on some data.
Definition: HashContext.h:68