akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [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> |
| 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
| 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> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace Sync { |
| 32 | |
| 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 | /** |
| 46 | * @brief Check if digest is empty |
| 47 | */ |
| 48 | bool |
| 49 | empty () const; |
| 50 | |
| 51 | /** |
| 52 | * @brief Reset digest to the initial state |
| 53 | */ |
| 54 | void |
| 55 | reset (); |
| 56 | |
| 57 | /** |
| 58 | * @brief Destructor |
| 59 | */ |
| 60 | ~Digest (); |
| 61 | |
| 62 | /** |
| 63 | * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes |
| 64 | * |
| 65 | * Side effect: finalize() will be called on `this' |
| 66 | */ |
| 67 | std::size_t |
| 68 | getHash () const; |
| 69 | |
| 70 | /** |
| 71 | * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception |
| 72 | */ |
| 73 | void |
| 74 | finalize (); |
| 75 | |
| 76 | /** |
| 77 | * @brief Compare two full digests |
| 78 | * |
| 79 | * Side effect: Finalize will be called on `this' and `digest' |
| 80 | */ |
| 81 | bool |
| 82 | operator == (const Digest &digest) const; |
| 83 | |
| 84 | bool |
| 85 | operator != (const Digest &digest) const |
| 86 | { return ! (*this == digest); } |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * @brief Add existing digest to digest calculation |
| 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 | |
| 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); |
| 104 | |
| 105 | /** |
| 106 | * @brief Add uint64_t value to digest calculation |
| 107 | * @param value uint64_t value to put into digest |
| 108 | */ |
| 109 | inline Digest & |
| 110 | operator << (uint64_t value); |
| 111 | |
| 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 |
| 118 | isZero () const; |
| 119 | |
| 120 | private: |
| 121 | Digest & |
| 122 | operator = (Digest &digest) { (void)digest; return *this; } |
| 123 | |
| 124 | /** |
| 125 | * @brief Add size bytes of buffer to the hash |
| 126 | */ |
| 127 | void |
| 128 | update (const uint8_t *buffer, size_t size); |
| 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); |
| 135 | |
| 136 | private: |
| 137 | EVP_MD_CTX *m_context; |
| 138 | std::vector<uint8_t> m_buffer; |
| 139 | }; |
| 140 | |
| 141 | namespace Error { |
| 142 | struct DigestCalculationError : virtual boost::exception, virtual std::exception { }; |
| 143 | } |
| 144 | |
| 145 | typedef boost::shared_ptr<Digest> DigestPtr; |
| 146 | typedef boost::shared_ptr<const Digest> DigestConstPtr; |
| 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 << (uint64_t value) |
| 157 | { |
| 158 | update (reinterpret_cast<const uint8_t*> (&value), sizeof (uint64_t)); |
| 159 | return *this; |
| 160 | } |
| 161 | |
| 162 | std::ostream & |
| 163 | operator << (std::ostream &os, const Digest &digest); |
| 164 | |
| 165 | std::istream & |
| 166 | operator >> (std::istream &is, Digest &digest); |
| 167 | |
| 168 | // template<class INT> |
| 169 | // Digest & |
| 170 | // Digest::operator << (INT value) |
| 171 | // { |
| 172 | // update (&value, sizeof (INT)); |
| 173 | // return *this; |
| 174 | // } |
| 175 | |
| 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 | |
| 196 | |
| 197 | } // Sync |
| 198 | |
| 199 | #endif // SYNC_DIGEST_H |