NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
digest.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "digest.hpp"
23 #include "string-helper.hpp"
24 #include <sstream>
25 
26 namespace ndn {
27 namespace util {
28 
29 template<typename Hash>
31 {
32  reset();
33 }
34 
35 template<typename Hash>
36 Digest<Hash>::Digest(std::istream& is)
37  : m_isInProcess(false)
38  , m_isFinalized(true)
39 {
40  using namespace CryptoPP;
41 
42  m_buffer = make_shared<Buffer>(m_hash.DigestSize());
43  FileSource(is, true,
44  new HashFilter(m_hash,
45  new ArraySink(m_buffer->get(), m_buffer->size())));
46 }
47 
48 template<typename Hash>
49 void
51 {
52  m_hash.Restart();
53  m_buffer = make_shared<Buffer>(m_hash.DigestSize());
54  m_isInProcess = false;
55  m_isFinalized = false;
56 }
57 
58 template<typename Hash>
59 void
61 {
62  // return immediately if Digest is finalized already.
63  if (m_isFinalized)
64  return;
65 
66  m_hash.Final(m_buffer->get());
67 
68  m_isFinalized = true;
69 }
70 
71 template<typename Hash>
74 {
75  finalize();
76  return m_buffer;
77 }
78 
79 template<typename Hash>
80 bool
82 {
83  return *computeDigest() == *digest.computeDigest();
84 }
85 
86 template<typename Hash>
89 {
90  ConstBufferPtr buffer = src.computeDigest();
91  update(buffer->get(), buffer->size());
92 
93  return *this;
94 }
95 
96 template<typename Hash>
98 Digest<Hash>::operator<<(const std::string& str)
99 {
100  update(reinterpret_cast<const uint8_t*>(str.c_str()), str.size());
101 
102  return *this;
103 }
104 
105 template<typename Hash>
108 {
109  update(block.wire(), block.size());
110 
111  return *this;
112 }
113 
114 template<typename Hash>
117 {
118  update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t));
119 
120  return *this;
121 }
122 
123 template<typename Hash>
124 void
125 Digest<Hash>::update(const uint8_t* buffer, size_t size)
126 {
127  // cannot update Digest when it has been finalized
128  if (m_isFinalized)
129  BOOST_THROW_EXCEPTION(Error("Digest has been already finalized"));
130 
131  m_hash.Update(buffer, size);
132 
133  m_isInProcess = true;
134 }
135 
136 template<typename Hash>
138 Digest<Hash>::computeDigest(const uint8_t* buffer, size_t size)
139 {
140  Hash hash;
141  BufferPtr result = make_shared<Buffer>(hash.DigestSize());
142  hash.Update(buffer, size);
143  hash.Final(result->get());
144 
145  return result;
146 }
147 
148 template<typename Hash>
149 std::string
151 {
152  std::ostringstream os;
153  os << *this;
154 
155  return os.str();
156 }
157 
158 template<typename Hash>
159 std::ostream&
160 operator<<(std::ostream& os, Digest<Hash>& digest)
161 {
162  ConstBufferPtr buffer = digest.computeDigest();
163  printHex(os, buffer->buf(), buffer->size());
164 
165  return os;
166 }
167 
168 template
170 
171 template
172 std::ostream&
173 operator<<(std::ostream& os, Digest<CryptoPP::SHA256>& digest);
174 
175 
176 } // namespace util
177 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
void reset()
Discard the current state and start a new digest.
Definition: digest.cpp:50
Copyright (c) 2013-2014 Regents of the University of California.
Definition: oid.hpp:29
shared_ptr< Buffer > BufferPtr
Definition: buffer.hpp:35
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
void printHex(std::ostream &os, const uint8_t *buffer, size_t length, bool isUpperCase)
Output the hex representation of the bytes in array to the output stream os.
provides a digest calculation
Definition: digest.hpp:47
size_t size() const
Definition: block.cpp:504
Digest< Hash > & operator<<(Digest< Hash > &src)
Add existing digest to digest calculation.
Definition: digest.cpp:88
void update(const uint8_t *buffer, size_t size)
Add a buffer to digest calculation.
Definition: digest.cpp:125
ConstBufferPtr computeDigest()
Obtain the digest.
Definition: digest.cpp:73
const uint8_t * wire() const
Definition: block.cpp:495
bool operator==(Digest< Hash > &digest)
Check if supplied digest equal to this digest.
Definition: digest.cpp:81
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:33
std::string toString()
Convert digest to std::string.
Definition: digest.cpp:150