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_;
27 
28  struct State {
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 
64  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  }
105  {
106  Hash hash{};
107  engine.final(hash.data());
108  return hash;
109  }
110 
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
Wraps a pointer to some data with size.
Definition: Blob.h:21
const uint8_t * data() const
Definition: Blob.h:31
size_t size() const
Definition: Blob.h:36
Class template for a Hash implementation 'Context'.
Definition: HashContext.h:23
ByteArray< Engine::hashsize > Hash
Definition: HashContext.h:26
HashContext & update(const FSTR::ObjectBase &obj)
Data from flash object.
Definition: HashContext.h:70
Engine_ Engine
Definition: HashContext.h:25
HashContext & update(const ByteArray< size_ > &array)
Data in ByteArray.
Definition: HashContext.h:94
State getState()
Get intermediate hash state.
Definition: HashContext.h:117
HashContext(EngineArgs &&... engineArgs)
Definition: HashContext.h:33
Hash getHash()
Finalise and return the final hash value.
Definition: HashContext.h:104
Hash calculate(Ts &&... args)
Calculate hash on some data.
Definition: HashContext.h:52
HashContext & reset(EngineArgs &&... engineArgs)
Reset the context for a new calculation.
Definition: HashContext.h:41
HashContext & update(const void *data, size_t size)
Pointer to data + size.
Definition: HashContext.h:87
void setState(const State &state)
Restore intermediate hash state.
Definition: HashContext.h:133
HashContext & update(const Blob &blob)
Data from Blob.
Definition: HashContext.h:64
Used when defining data structures.
Definition: ObjectBase.hpp:33
size_t read(size_t offset, void *buffer, size_t count) const
Read contents of a String into RAM.
Definition: ObjectBase.hpp:72
Definition: Blake2s.h:19
std::array< uint8_t, size_ > ByteArray
Class template for fixed byte array.
Definition: ByteArray.h:24
Definition: HashContext.h:28
ByteArray< Engine::statesize > value
Definition: HashContext.h:29
uint64_t count
Definition: HashContext.h:30