Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 20 | * @author Chaoyi Bian <bcy@pku.edu.cn> |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #ifndef SYNC_DIGEST_H |
| 25 | #define SYNC_DIGEST_H |
| 26 | |
| 27 | #include <boost/exception/all.hpp> |
| 28 | #include <openssl/evp.h> |
| 29 | #include <boost/cstdint.hpp> |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 30 | #include <vector> |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 32 | namespace Sync { |
| 33 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 34 | /** |
| 35 | * @ingroup sync |
| 36 | * @brief A simple wrapper for libcrypto hash functions |
| 37 | */ |
| 38 | class Digest |
| 39 | { |
| 40 | public: |
| 41 | /** |
| 42 | * @brief Default constructor. Will initialize internal libssl structures |
| 43 | */ |
| 44 | Digest (); |
| 45 | |
| 46 | /** |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 47 | * @brief Check if digest is empty |
| 48 | */ |
| 49 | bool |
| 50 | empty () const; |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 52 | /** |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 53 | * @brief Reset digest to the initial state |
| 54 | */ |
| 55 | void |
| 56 | reset (); |
| 57 | |
| 58 | /** |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 59 | * @brief Destructor |
| 60 | */ |
| 61 | ~Digest (); |
| 62 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 63 | /** |
| 64 | * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes |
| 65 | * |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 66 | * Side effect: finalize() will be called on `this' |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 67 | */ |
| 68 | std::size_t |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 69 | getHash () const; |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 72 | * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception |
| 73 | */ |
| 74 | void |
| 75 | finalize (); |
| 76 | |
| 77 | /** |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 78 | * @brief Compare two full digests |
| 79 | * |
| 80 | * Side effect: Finalize will be called on `this' and `digest' |
| 81 | */ |
| 82 | bool |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 83 | operator == (const Digest &digest) const; |
| 84 | |
| 85 | bool |
| 86 | operator != (const Digest &digest) const |
| 87 | { return ! (*this == digest); } |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 90 | /** |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 91 | * @brief Add existing digest to digest calculation |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 92 | * @param src digest to combine with |
| 93 | * |
| 94 | * The result of this combination is hash (hash (...)) |
| 95 | */ |
| 96 | Digest & |
| 97 | operator << (const Digest &src); |
| 98 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 99 | /** |
| 100 | * @brief Add string to digest calculation |
| 101 | * @param str string to put into digest |
| 102 | */ |
| 103 | inline Digest & |
| 104 | operator << (const std::string &str); |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | 58c77b0 | 2012-03-05 21:52:25 -0800 | [diff] [blame] | 106 | /** |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 107 | * @brief Add uint64_t value to digest calculation |
| 108 | * @param value uint64_t value to put into digest |
Alexander Afanasyev | 58c77b0 | 2012-03-05 21:52:25 -0800 | [diff] [blame] | 109 | */ |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 110 | inline Digest & |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 111 | operator << (uint64_t value); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 113 | /** |
| 114 | * @brief Checks if the stored hash is zero-root hash |
| 115 | * |
| 116 | * Zero-root hash is a valid hash that optimally represents an empty state |
| 117 | */ |
| 118 | bool |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 119 | isZero () const; |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 120 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 121 | private: |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 122 | Digest & |
Chaoyi Bian | b119028 | 2012-06-13 17:15:07 -0700 | [diff] [blame] | 123 | operator = (Digest &digest) { (void)digest; return *this; } |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 125 | /** |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 126 | * @brief Add size bytes of buffer to the hash |
| 127 | */ |
| 128 | void |
| 129 | update (const uint8_t *buffer, size_t size); |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 130 | |
| 131 | friend std::ostream & |
| 132 | operator << (std::ostream &os, const Digest &digest); |
| 133 | |
| 134 | friend std::istream & |
| 135 | operator >> (std::istream &is, Digest &digest); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 136 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 137 | private: |
| 138 | EVP_MD_CTX *m_context; |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 139 | std::vector<uint8_t> m_buffer; |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 142 | namespace Error { |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 143 | struct DigestCalculationError : virtual boost::exception, virtual std::exception { }; |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 144 | } |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 146 | typedef boost::shared_ptr<Digest> DigestPtr; |
| 147 | typedef boost::shared_ptr<const Digest> DigestConstPtr; |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 148 | |
| 149 | Digest & |
| 150 | Digest::operator << (const std::string &str) |
| 151 | { |
| 152 | update (reinterpret_cast<const uint8_t*> (str.c_str ()), str.size ()); |
| 153 | return *this; |
| 154 | } |
| 155 | |
| 156 | inline Digest & |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 157 | Digest::operator << (uint64_t value) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 158 | { |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 159 | update (reinterpret_cast<const uint8_t*> (&value), sizeof (uint64_t)); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 160 | return *this; |
| 161 | } |
| 162 | |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 163 | std::ostream & |
| 164 | operator << (std::ostream &os, const Digest &digest); |
| 165 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 166 | std::istream & |
| 167 | operator >> (std::istream &is, Digest &digest); |
| 168 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 169 | // template<class INT> |
| 170 | // Digest & |
| 171 | // Digest::operator << (INT value) |
| 172 | // { |
| 173 | // update (&value, sizeof (INT)); |
| 174 | // return *this; |
| 175 | // } |
| 176 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 177 | struct DigestPtrHash : public std::unary_function<Digest, std::size_t> |
| 178 | { |
| 179 | std::size_t |
| 180 | operator() (DigestConstPtr digest) const |
| 181 | { |
| 182 | // std::cout << "digest->getHash: " << digest->getHash () << " (" << *digest << ")" << std::endl; |
| 183 | return digest->getHash (); |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | struct DigestPtrEqual : public std::unary_function<Digest, std::size_t> |
| 188 | { |
| 189 | bool |
| 190 | operator() (DigestConstPtr digest1, DigestConstPtr digest2) const |
| 191 | { |
| 192 | // std::cout << boost::cref(*digest1) << " == " << boost::cref(*digest2) << " : " << (*digest1 == *digest2) << std::endl; |
| 193 | return *digest1 == *digest2; |
| 194 | } |
| 195 | }; |
| 196 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 197 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 198 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 199 | |
| 200 | #endif // SYNC_DIGEST_H |