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 | /** |
| 47 | * @brief Destructor |
| 48 | */ |
| 49 | ~Digest (); |
| 50 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 51 | /** |
| 52 | * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes |
| 53 | * |
| 54 | * Side effect: Finalize will be called on `this' |
| 55 | */ |
| 56 | std::size_t |
| 57 | getHash (); |
| 58 | |
| 59 | /** |
| 60 | * @brief Compare two full digests |
| 61 | * |
| 62 | * Side effect: Finalize will be called on `this' and `digest' |
| 63 | */ |
| 64 | bool |
| 65 | operator == (Digest &digest); |
| 66 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 67 | /** |
| 68 | * @brief Combine digests |
| 69 | * @param src digest to combine with |
| 70 | * |
| 71 | * The result of this combination is hash (hash (...)) |
| 72 | */ |
| 73 | Digest & |
| 74 | operator << (const Digest &src); |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 79 | private: |
| 80 | /** |
| 81 | * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception |
| 82 | */ |
| 83 | void Finalize (); |
| 84 | |
| 85 | private: |
| 86 | EVP_MD_CTX *m_context; |
| 87 | uint8_t *m_buffer; |
| 88 | uint32_t m_hashLength; |
| 89 | }; |
| 90 | |
| 91 | struct DigestCalculationError : virtual boost::exception { }; |
| 92 | |
| 93 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 94 | |
| 95 | #endif // SYNC_DIGEST_H |