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 "HashEngine.h"
14 #include "Blob.h"
15 #include "ByteArray.h"
16 
17 namespace Crypto
18 {
23 template <class Engine_> class HashContext
24 {
25 public:
26  using Engine = Engine_;
28 
29  struct State {
31  uint64_t count;
32  };
33 
35  {
36  reset();
37  }
38 
43  {
44  engine.init();
45  return *this;
46  }
47 
53  template <typename... Ts> Hash calculate(Ts&&... args)
54  {
55  update(std::forward<Ts>(args)...);
56  return getHash();
57  }
58 
63  HashContext& update(const Blob& blob)
64  {
65  return update(blob.data(), blob.size());
66  }
67 
69  {
70  uint8_t buf[256];
71  size_t offset = 0;
72  size_t len;
73  while((len = obj.read(offset, buf, sizeof(buf))) > 0) {
74  engine.update(buf, len);
75  offset += len;
76  }
77  return *this;
78  }
79 
80  HashContext& update(const void* data, size_t size)
81  {
82  engine.update(data, size);
83  return *this;
84  }
85 
86  template <size_t size_> HashContext& update(const ByteArray<size_>& array)
87  {
88  return update(array.data(), array.size());
89  }
97  {
98  Hash hash;
99  engine.final(hash.data());
100  return hash;
101  }
102 
109  {
110  State state;
111  state.count = engine.get_state(state.value.data());
112  return state;
113  }
114 
123  void setState(const State& state)
124  {
125  engine.set_state(state.value.data(), state.count);
126  }
127 
128 private:
129  Engine engine;
130 };
131 
132 /*
133  * Context definitions
134  */
135 
142 
143 } // namespace Crypto
ByteArray< Engine::statesize > value
Definition: HashContext.h:30
HashContext & update(const Blob &blob)
Definition: HashContext.h:63
Used when defining data structures.
Definition: ObjectBase.hpp:32
ByteArray< Engine::hashsize > Hash
Definition: HashContext.h:27
HashContext & update(const ByteArray< size_ > &array)
Definition: HashContext.h:86
HashContext & update(const void *data, size_t size)
Definition: HashContext.h:80
HashContext & update(const FSTR::ObjectBase &obj)
Definition: HashContext.h:68
State getState()
Get intermediate hash state.
Definition: HashContext.h:108
void setState(const State &state)
Restore intermediate hash state.
Definition: HashContext.h:123
HashContext & reset()
Reset the context for a new calculation.
Definition: HashContext.h:42
std::array< uint8_t, size_ > ByteArray
Class template for fixed byte array.
Definition: ByteArray.h:24
size_t size() const
Definition: Blob.h:36
Class template for a Hash implementation &#39;Context&#39;.
Definition: HashContext.h:23
Engine_ Engine
Definition: HashContext.h:26
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:31
HashContext()
Definition: HashContext.h:34
Hash calculate(Ts &&... args)
Calculate hash on some data.
Definition: HashContext.h:53
Wraps a pointer to some data with size.
Definition: Blob.h:20
Definition: Blob.h:15
const uint8_t * data() const
Definition: Blob.h:31
Hash getHash()
Finalise and return the final hash value.
Definition: HashContext.h:96
Definition: HashContext.h:29