blob: 156d372da6781127f89ef1a61087b0f2e41248bc [file] [log] [blame]
Yingdi Yude222c72014-08-15 16:06:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "digest.hpp"
Alexander Afanasyev6800e1f2014-09-30 13:20:12 -070023#include <sstream>
Yingdi Yude222c72014-08-15 16:06:52 -070024
25namespace ndn {
26namespace util {
27
28template<typename Hash>
29Digest<Hash>::Digest()
30{
31 reset();
32}
33
34template<typename Hash>
35void
36Digest<Hash>::reset()
37{
38 m_hash.Restart();
39 m_buffer = make_shared<Buffer>(m_hash.DigestSize());
40 m_isInProcess = false;
41 m_isFinalized = false;
42}
43
44template<typename Hash>
45void
46Digest<Hash>::finalize()
47{
48 // return immediately if Digest is finalized already.
49 if (m_isFinalized)
50 return;
51
52 m_hash.Final(m_buffer->get());
53
54 m_isFinalized = true;
55}
56
57template<typename Hash>
58ConstBufferPtr
59Digest<Hash>::computeDigest()
60{
61 finalize();
62 return m_buffer;
63}
64
65template<typename Hash>
66bool
67Digest<Hash>::operator==(Digest<Hash>& digest)
68{
69 return *computeDigest() == *digest.computeDigest();
70}
71
72template<typename Hash>
73Digest<Hash>&
74Digest<Hash>::operator<<(Digest<Hash>& src)
75{
76 ConstBufferPtr buffer = src.computeDigest();
77 update(buffer->get(), buffer->size());
78
79 return *this;
80}
81
82template<typename Hash>
83Digest<Hash>&
84Digest<Hash>::operator<<(const std::string& str)
85{
86 update(reinterpret_cast<const uint8_t*>(str.c_str()), str.size());
87
88 return *this;
89}
90
91template<typename Hash>
92Digest<Hash>&
93Digest<Hash>::operator<<(const Block& block)
94{
95 update(block.wire(), block.size());
96
97 return *this;
98}
99
100template<typename Hash>
101Digest<Hash>&
102Digest<Hash>::operator<<(uint64_t value)
103{
104 update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t));
105
106 return *this;
107}
108
109template<typename Hash>
110void
111Digest<Hash>::update(const uint8_t* buffer, size_t size)
112{
113 // cannot update Digest when it has been finalized
114 if (m_isFinalized)
115 throw Error("Digest has been already finalized");
116
117 m_hash.Update(buffer, size);
118
119 m_isInProcess = true;
120}
121
122template<typename Hash>
123ConstBufferPtr
124Digest<Hash>::computeDigest(const uint8_t* buffer, size_t size)
125{
126 Hash hash;
127 BufferPtr result = make_shared<Buffer>(hash.DigestSize());
128 hash.Update(buffer, size);
129 hash.Final(result->get());
130
131 return result;
132}
133
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700134template<typename Hash>
135std::string
136Digest<Hash>::toString()
137{
138 std::ostringstream os;
139 os << *this;
140
141 return os.str();
142}
143
144template<typename Hash>
145std::ostream&
146operator<<(std::ostream& os, Digest<Hash>& digest)
147{
148 using namespace CryptoPP;
149
150 std::string output;
151 ConstBufferPtr buffer = digest.computeDigest();
152 StringSource(buffer->buf(), buffer->size(), true, new HexEncoder(new FileSink(os)));
153
154 return os;
155}
156
157template
158class Digest<CryptoPP::SHA256>;
159
160template
161std::ostream&
162operator<<(std::ostream& os, Digest<CryptoPP::SHA256>& digest);
163
Yingdi Yude222c72014-08-15 16:06:52 -0700164
165} // namespace util
166} // namespace ndn