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 | 6800e1f | 2014-09-30 13:20:12 -0700 | [diff] [blame] | 23 | #include <sstream> |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | namespace util { |
| 27 | |
| 28 | template<typename Hash> |
| 29 | Digest<Hash>::Digest() |
| 30 | { |
| 31 | reset(); |
| 32 | } |
| 33 | |
| 34 | template<typename Hash> |
| 35 | void |
| 36 | Digest<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 | |
| 44 | template<typename Hash> |
| 45 | void |
| 46 | Digest<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 | |
| 57 | template<typename Hash> |
| 58 | ConstBufferPtr |
| 59 | Digest<Hash>::computeDigest() |
| 60 | { |
| 61 | finalize(); |
| 62 | return m_buffer; |
| 63 | } |
| 64 | |
| 65 | template<typename Hash> |
| 66 | bool |
| 67 | Digest<Hash>::operator==(Digest<Hash>& digest) |
| 68 | { |
| 69 | return *computeDigest() == *digest.computeDigest(); |
| 70 | } |
| 71 | |
| 72 | template<typename Hash> |
| 73 | Digest<Hash>& |
| 74 | Digest<Hash>::operator<<(Digest<Hash>& src) |
| 75 | { |
| 76 | ConstBufferPtr buffer = src.computeDigest(); |
| 77 | update(buffer->get(), buffer->size()); |
| 78 | |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | template<typename Hash> |
| 83 | Digest<Hash>& |
| 84 | Digest<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 | |
| 91 | template<typename Hash> |
| 92 | Digest<Hash>& |
| 93 | Digest<Hash>::operator<<(const Block& block) |
| 94 | { |
| 95 | update(block.wire(), block.size()); |
| 96 | |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | template<typename Hash> |
| 101 | Digest<Hash>& |
| 102 | Digest<Hash>::operator<<(uint64_t value) |
| 103 | { |
| 104 | update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t)); |
| 105 | |
| 106 | return *this; |
| 107 | } |
| 108 | |
| 109 | template<typename Hash> |
| 110 | void |
| 111 | Digest<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 | |
| 122 | template<typename Hash> |
| 123 | ConstBufferPtr |
| 124 | Digest<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 Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 134 | template<typename Hash> |
| 135 | std::string |
| 136 | Digest<Hash>::toString() |
| 137 | { |
| 138 | std::ostringstream os; |
| 139 | os << *this; |
| 140 | |
| 141 | return os.str(); |
| 142 | } |
| 143 | |
| 144 | template<typename Hash> |
| 145 | std::ostream& |
| 146 | operator<<(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 | |
| 157 | template |
| 158 | class Digest<CryptoPP::SHA256>; |
| 159 | |
| 160 | template |
| 161 | std::ostream& |
| 162 | operator<<(std::ostream& os, Digest<CryptoPP::SHA256>& digest); |
| 163 | |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 164 | |
| 165 | } // namespace util |
| 166 | } // namespace ndn |