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" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace util { |
| 26 | |
| 27 | template<typename Hash> |
| 28 | Digest<Hash>::Digest() |
| 29 | { |
| 30 | reset(); |
| 31 | } |
| 32 | |
| 33 | template<typename Hash> |
| 34 | void |
| 35 | Digest<Hash>::reset() |
| 36 | { |
| 37 | m_hash.Restart(); |
| 38 | m_buffer = make_shared<Buffer>(m_hash.DigestSize()); |
| 39 | m_isInProcess = false; |
| 40 | m_isFinalized = false; |
| 41 | } |
| 42 | |
| 43 | template<typename Hash> |
| 44 | void |
| 45 | Digest<Hash>::finalize() |
| 46 | { |
| 47 | // return immediately if Digest is finalized already. |
| 48 | if (m_isFinalized) |
| 49 | return; |
| 50 | |
| 51 | m_hash.Final(m_buffer->get()); |
| 52 | |
| 53 | m_isFinalized = true; |
| 54 | } |
| 55 | |
| 56 | template<typename Hash> |
| 57 | ConstBufferPtr |
| 58 | Digest<Hash>::computeDigest() |
| 59 | { |
| 60 | finalize(); |
| 61 | return m_buffer; |
| 62 | } |
| 63 | |
| 64 | template<typename Hash> |
| 65 | bool |
| 66 | Digest<Hash>::operator==(Digest<Hash>& digest) |
| 67 | { |
| 68 | return *computeDigest() == *digest.computeDigest(); |
| 69 | } |
| 70 | |
| 71 | template<typename Hash> |
| 72 | Digest<Hash>& |
| 73 | Digest<Hash>::operator<<(Digest<Hash>& src) |
| 74 | { |
| 75 | ConstBufferPtr buffer = src.computeDigest(); |
| 76 | update(buffer->get(), buffer->size()); |
| 77 | |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | template<typename Hash> |
| 82 | Digest<Hash>& |
| 83 | Digest<Hash>::operator<<(const std::string& str) |
| 84 | { |
| 85 | update(reinterpret_cast<const uint8_t*>(str.c_str()), str.size()); |
| 86 | |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | template<typename Hash> |
| 91 | Digest<Hash>& |
| 92 | Digest<Hash>::operator<<(const Block& block) |
| 93 | { |
| 94 | update(block.wire(), block.size()); |
| 95 | |
| 96 | return *this; |
| 97 | } |
| 98 | |
| 99 | template<typename Hash> |
| 100 | Digest<Hash>& |
| 101 | Digest<Hash>::operator<<(uint64_t value) |
| 102 | { |
| 103 | update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t)); |
| 104 | |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | template<typename Hash> |
| 109 | void |
| 110 | Digest<Hash>::update(const uint8_t* buffer, size_t size) |
| 111 | { |
| 112 | // cannot update Digest when it has been finalized |
| 113 | if (m_isFinalized) |
| 114 | throw Error("Digest has been already finalized"); |
| 115 | |
| 116 | m_hash.Update(buffer, size); |
| 117 | |
| 118 | m_isInProcess = true; |
| 119 | } |
| 120 | |
| 121 | template<typename Hash> |
| 122 | ConstBufferPtr |
| 123 | Digest<Hash>::computeDigest(const uint8_t* buffer, size_t size) |
| 124 | { |
| 125 | Hash hash; |
| 126 | BufferPtr result = make_shared<Buffer>(hash.DigestSize()); |
| 127 | hash.Update(buffer, size); |
| 128 | hash.Final(result->get()); |
| 129 | |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | template class Digest<CryptoPP::SHA256>; |
| 134 | |
| 135 | } // namespace util |
| 136 | } // namespace ndn |