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 
62  HashContext& update(const Blob& blob)
63  {
64  return update(blob.data(), blob.size());
65  }
66 
68  {
69  uint8_t buf[256];
70  size_t offset = 0;
71  size_t len;
72  while((len = obj.read(offset, buf, sizeof(buf))) > 0) {
73  engine.update(buf, len);
74  offset += len;
75  }
76  return *this;
77  }
78 
79  HashContext& update(const void* data, size_t size)
80  {
81  engine.update(data, size);
82  return *this;
83  }
84 
85  template <size_t size_> HashContext& update(const ByteArray<size_>& array)
86  {
87  return update(array.data(), array.size());
88  }
96  {
97  Hash hash;
98  engine.final(hash.data());
99  return hash;
100  }
101 
109  {
110  State state;
111  state.count = engine.get_state(state.value.data());
112  return state;
113  }
114 
124  void setState(const State& state)
125  {
126  engine.set_state(state.value.data(), state.count);
127  }
128 
129 private:
130  Engine engine;
131 };
132 
133 } // namespace Crypto
ByteArray< Engine::statesize > value
Definition: HashContext.h:29
HashContext & update(const Blob &blob)
Definition: HashContext.h:62
Used when defining data structures.
Definition: ObjectBase.hpp:32
ByteArray< Engine::hashsize > Hash
Definition: HashContext.h:26
HashContext & update(const ByteArray< size_ > &array)
Definition: HashContext.h:85
HashContext(EngineArgs &&... engineArgs)
Definition: HashContext.h:33
HashContext & update(const void *data, size_t size)
Definition: HashContext.h:79
HashContext & update(const FSTR::ObjectBase &obj)
Definition: HashContext.h:67
State getState()
Get intermediate hash state.
Definition: HashContext.h:108
void setState(const State &state)
Restore intermediate hash state.
Definition: HashContext.h:124
std::array< uint8_t, size_ > ByteArray
Class template for fixed byte array.
Definition: ByteArray.h:24
HashContext & reset(EngineArgs &&... engineArgs)
Reset the context for a new calculation.
Definition: HashContext.h:41
size_t size() const
Definition: Blob.h:36
Class template for a Hash implementation &#39;Context&#39;.
Definition: HashContext.h:22
Engine_ Engine
Definition: HashContext.h:25
size_t read(size_t offset, void *buffer, size_t count) const
Read contents of a String into RAM.
Definition: ObjectBase.hpp:72
uint64_t count
Definition: HashContext.h:30
Hash calculate(Ts &&... args)
Calculate hash on some data.
Definition: HashContext.h:52
Wraps a pointer to some data with size.
Definition: Blob.h:20
Definition: Blake2s.h:18
const uint8_t * data() const
Definition: Blob.h:31
Hash getHash()
Finalise and return the final hash value.
Definition: HashContext.h:95
Definition: HashContext.h:28