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