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 | |
| 32 | const std::size_t HASH_SIZE = 160; |
| 33 | |
| 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 | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 47 | * @brief Reset digest to the initial state |
| 48 | */ |
| 49 | void |
| 50 | reset (); |
| 51 | |
| 52 | /** |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 53 | * @brief Destructor |
| 54 | */ |
| 55 | ~Digest (); |
| 56 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 57 | /** |
| 58 | * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes |
| 59 | * |
| 60 | * Side effect: Finalize will be called on `this' |
| 61 | */ |
| 62 | std::size_t |
| 63 | getHash (); |
| 64 | |
| 65 | /** |
| 66 | * @brief Compare two full digests |
| 67 | * |
| 68 | * Side effect: Finalize will be called on `this' and `digest' |
| 69 | */ |
| 70 | bool |
| 71 | operator == (Digest &digest); |
| 72 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 73 | /** |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 74 | * @brief Add existing digest to digest calculation |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 75 | * @param src digest to combine with |
| 76 | * |
| 77 | * The result of this combination is hash (hash (...)) |
| 78 | */ |
| 79 | Digest & |
| 80 | operator << (const Digest &src); |
| 81 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 82 | /** |
| 83 | * @brief Add string to digest calculation |
| 84 | * @param str string to put into digest |
| 85 | */ |
| 86 | inline Digest & |
| 87 | operator << (const std::string &str); |
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 | inline Digest & |
| 90 | operator << (uint32_t value); |
| 91 | |
| 92 | // /** |
| 93 | // * @brief Add integer to digest calculation |
| 94 | // * @param value the value to add to the digest |
| 95 | // */ |
| 96 | // template<class INT> |
| 97 | // inline Digest & |
| 98 | // operator << (INT value); |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame^] | 99 | |
| 100 | std::ostream& |
| 101 | print (std::ostream &os) const; |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 103 | private: |
| 104 | /** |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 105 | * @brief Disabled copy operator |
| 106 | */ |
| 107 | Digest & |
| 108 | operator = (Digest &digest) { return *this; } |
| 109 | |
| 110 | /** |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 111 | * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception |
| 112 | */ |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 113 | void |
| 114 | finalize (); |
| 115 | |
| 116 | /** |
| 117 | * @brief Add size bytes of buffer to the hash |
| 118 | */ |
| 119 | void |
| 120 | update (const uint8_t *buffer, size_t size); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | EVP_MD_CTX *m_context; |
| 124 | uint8_t *m_buffer; |
| 125 | uint32_t m_hashLength; |
| 126 | }; |
| 127 | |
| 128 | struct DigestCalculationError : virtual boost::exception { }; |
| 129 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 130 | typedef boost::shared_ptr<Digest> DigestPtr; |
| 131 | typedef boost::shared_ptr<const Digest> DigestConstPtr; |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 132 | |
| 133 | Digest & |
| 134 | Digest::operator << (const std::string &str) |
| 135 | { |
| 136 | update (reinterpret_cast<const uint8_t*> (str.c_str ()), str.size ()); |
| 137 | return *this; |
| 138 | } |
| 139 | |
| 140 | inline Digest & |
| 141 | Digest::operator << (uint32_t value) |
| 142 | { |
| 143 | update (reinterpret_cast<const uint8_t*> (&value), sizeof (uint32_t)); |
| 144 | return *this; |
| 145 | } |
| 146 | |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame^] | 147 | std::ostream & |
| 148 | operator << (std::ostream &os, const Digest &digest); |
| 149 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 150 | // template<class INT> |
| 151 | // Digest & |
| 152 | // Digest::operator << (INT value) |
| 153 | // { |
| 154 | // update (&value, sizeof (INT)); |
| 155 | // return *this; |
| 156 | // } |
| 157 | |
| 158 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 159 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 160 | |
| 161 | #endif // SYNC_DIGEST_H |