Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 23 | #include "string-helper.hpp" |
Alexander Afanasyev | 6800e1f | 2014-09-30 13:20:12 -0700 | [diff] [blame] | 24 | #include <sstream> |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace util { |
| 28 | |
| 29 | template<typename Hash> |
| 30 | Digest<Hash>::Digest() |
| 31 | { |
| 32 | reset(); |
| 33 | } |
| 34 | |
| 35 | template<typename Hash> |
Alexander Afanasyev | d27334f | 2015-07-01 21:44:36 -0700 | [diff] [blame] | 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> |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 49 | void |
| 50 | Digest<Hash>::reset() |
| 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 |
| 60 | Digest<Hash>::finalize() |
| 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> |
| 72 | ConstBufferPtr |
| 73 | Digest<Hash>::computeDigest() |
| 74 | { |
| 75 | finalize(); |
| 76 | return m_buffer; |
| 77 | } |
| 78 | |
| 79 | template<typename Hash> |
| 80 | bool |
| 81 | Digest<Hash>::operator==(Digest<Hash>& digest) |
| 82 | { |
| 83 | return *computeDigest() == *digest.computeDigest(); |
| 84 | } |
| 85 | |
| 86 | template<typename Hash> |
| 87 | Digest<Hash>& |
| 88 | Digest<Hash>::operator<<(Digest<Hash>& src) |
| 89 | { |
| 90 | ConstBufferPtr buffer = src.computeDigest(); |
| 91 | update(buffer->get(), buffer->size()); |
| 92 | |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | template<typename Hash> |
| 97 | Digest<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> |
| 106 | Digest<Hash>& |
| 107 | Digest<Hash>::operator<<(const Block& block) |
| 108 | { |
| 109 | update(block.wire(), block.size()); |
| 110 | |
| 111 | return *this; |
| 112 | } |
| 113 | |
| 114 | template<typename Hash> |
| 115 | Digest<Hash>& |
| 116 | Digest<Hash>::operator<<(uint64_t value) |
| 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 | throw Error("Digest has been already finalized"); |
| 130 | |
| 131 | m_hash.Update(buffer, size); |
| 132 | |
| 133 | m_isInProcess = true; |
| 134 | } |
| 135 | |
| 136 | template<typename Hash> |
| 137 | ConstBufferPtr |
| 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 | |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 148 | template<typename Hash> |
| 149 | std::string |
| 150 | Digest<Hash>::toString() |
| 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 | { |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 162 | ConstBufferPtr buffer = digest.computeDigest(); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 163 | printHex(os, buffer->buf(), buffer->size()); |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 164 | |
| 165 | return os; |
| 166 | } |
| 167 | |
| 168 | template |
| 169 | class Digest<CryptoPP::SHA256>; |
| 170 | |
| 171 | template |
| 172 | std::ostream& |
| 173 | operator<<(std::ostream& os, Digest<CryptoPP::SHA256>& digest); |
| 174 | |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 175 | |
| 176 | } // namespace util |
| 177 | } // namespace ndn |