digest.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_UTIL_DIGEST_HPP
23 #define NDN_UTIL_DIGEST_HPP
24 
25 #include "../encoding/buffer.hpp"
26 #include "../encoding/block.hpp"
27 #include "../security/v1/cryptopp.hpp"
28 #include "concepts.hpp"
29 
30 namespace ndn {
31 namespace util {
32 
44 template<typename Hash>
45 class Digest
46 {
47 public:
48  BOOST_CONCEPT_ASSERT((Hashable<Hash>));
49 
50  typedef Hash HashFunction;
51 
52  class Error : public std::runtime_error
53  {
54  public:
55  explicit
56  Error(const std::string& what)
57  : std::runtime_error(what)
58  {
59  }
60  };
61 
62  Digest();
63 
68  explicit
69  Digest(std::istream& is);
70 
74  void
75  reset();
76 
82  bool
83  empty() const
84  {
85  return !m_isInProcess;
86  }
87 
92  computeDigest();
93 
99  bool
100  operator==(Digest<Hash>& digest);
101 
107  bool
109  {
110  return !(*this == digest);
111  }
112 
121  Digest<Hash>&
122  operator<<(Digest<Hash>& src);
123 
128  Digest<Hash>&
129  operator<<(const std::string& str);
130 
136  Digest<Hash>&
137  operator<<(const Block& block);
138 
144  Digest<Hash>&
145  operator<<(uint64_t value);
146 
157  void
158  update(const uint8_t* buffer, size_t size);
159 
166  static ConstBufferPtr
167  computeDigest(const uint8_t* buffer, size_t size);
168 
174  std::string
175  toString();
176 
177 private:
183  void
184  finalize();
185 
186 private:
187  Hash m_hash;
188  BufferPtr m_buffer;
189  bool m_isInProcess;
190  bool m_isFinalized;
191 };
192 
193 template<typename Hash>
194 std::ostream&
195 operator<<(std::ostream& os, Digest<Hash>& digest);
196 
201 
202 } // namespace util
203 } // namespace ndn
204 
205 #endif // NDN_UTIL_DIGEST_HPP
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
void reset()
Discard the current state and start a new digest calculation.
Definition: digest.cpp:52
shared_ptr< Buffer > BufferPtr
Definition: buffer.hpp:35
STL namespace.
bool operator!=(Digest< Hash > &digest)
Check if the supplied digest is not equal to this digest.
Definition: digest.hpp:108
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
provides a stateful digest calculation
Definition: digest.hpp:45
bool empty() const
Check if digest is empty.
Definition: digest.hpp:83
Digest< Hash > & operator<<(Digest< Hash > &src)
Add existing digest to the digest calculation.
Definition: digest.cpp:98
Digest< CryptoPP::SHA256 > Sha256
A digest using SHA256 as the hash function.
Definition: digest.hpp:200
Error(const std::string &what)
Definition: digest.hpp:56
void update(const uint8_t *buffer, size_t size)
Add a buffer to the digest calculation.
Definition: digest.cpp:135
ConstBufferPtr computeDigest()
Finalize and return the digest based on all previously supplied inputs.
Definition: digest.cpp:75
bool operator==(Digest< Hash > &digest)
Check if the supplied digest equals to this digest.
Definition: digest.cpp:83
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:33
std::string toString()
Convert digest to std::string.
Definition: digest.cpp:160
a concept check for CryptoPP hash algorithm
Definition: concepts.hpp:90