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> |
| 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 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 30 | namespace Sync { |
| 31 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 32 | /** |
| 33 | * @ingroup sync |
| 34 | * @brief A simple wrapper for libcrypto hash functions |
| 35 | */ |
| 36 | class Digest |
| 37 | { |
| 38 | public: |
| 39 | /** |
| 40 | * @brief Default constructor. Will initialize internal libssl structures |
| 41 | */ |
| 42 | Digest (); |
| 43 | |
| 44 | /** |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 45 | * @brief Check if digest is empty |
| 46 | */ |
| 47 | bool |
| 48 | empty () const; |
| 49 | |
| 50 | /** |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 51 | * @brief Reset digest to the initial state |
| 52 | */ |
| 53 | void |
| 54 | reset (); |
| 55 | |
| 56 | /** |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 57 | * @brief Destructor |
| 58 | */ |
| 59 | ~Digest (); |
| 60 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 61 | /** |
| 62 | * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes |
| 63 | * |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 64 | * Side effect: finalize() will be called on `this' |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 65 | */ |
| 66 | std::size_t |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 67 | getHash () const; |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
Alexander Afanasyev | 1fbdcdd | 2012-03-05 23:14:33 -0800 | [diff] [blame] | 70 | * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception |
| 71 | */ |
| 72 | void |
| 73 | finalize (); |
| 74 | |
| 75 | /** |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 76 | * @brief Compare two full digests |
| 77 | * |
| 78 | * Side effect: Finalize will be called on `this' and `digest' |
| 79 | */ |
| 80 | bool |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 81 | operator == (const Digest &digest) const; |
| 82 | |
| 83 | bool |
| 84 | operator != (const Digest &digest) const |
| 85 | { return ! (*this == digest); } |
| 86 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 88 | /** |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 89 | * @brief Add existing digest to digest calculation |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 90 | * @param src digest to combine with |
| 91 | * |
| 92 | * The result of this combination is hash (hash (...)) |
| 93 | */ |
| 94 | Digest & |
| 95 | operator << (const Digest &src); |
| 96 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 97 | /** |
| 98 | * @brief Add string to digest calculation |
| 99 | * @param str string to put into digest |
| 100 | */ |
| 101 | inline Digest & |
| 102 | operator << (const std::string &str); |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 58c77b0 | 2012-03-05 21:52:25 -0800 | [diff] [blame] | 104 | /** |
| 105 | * @brief Add uint32_t value to digest calculation |
| 106 | * @param value uint32_t value to put into digest |
| 107 | */ |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 108 | inline Digest & |
| 109 | operator << (uint32_t value); |
| 110 | |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 111 | /** |
| 112 | * @brief Checks if the stored hash is zero-root hash |
| 113 | * |
| 114 | * Zero-root hash is a valid hash that optimally represents an empty state |
| 115 | */ |
| 116 | bool |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 117 | isZero () const; |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 118 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 119 | private: |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 120 | Digest & |
| 121 | operator = (Digest &digest) { return *this; } |
| 122 | |
| 123 | /** |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 124 | * @brief Add size bytes of buffer to the hash |
| 125 | */ |
| 126 | void |
| 127 | update (const uint8_t *buffer, size_t size); |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 128 | |
| 129 | friend std::ostream & |
| 130 | operator << (std::ostream &os, const Digest &digest); |
| 131 | |
| 132 | friend std::istream & |
| 133 | operator >> (std::istream &is, Digest &digest); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 134 | |
| 135 | private: |
| 136 | EVP_MD_CTX *m_context; |
| 137 | uint8_t *m_buffer; |
| 138 | uint32_t m_hashLength; |
| 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 | |
| 176 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 177 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 178 | |
| 179 | #endif // SYNC_DIGEST_H |