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 | |
| 30 | namespace ns3 { |
| 31 | namespace Sync { |
| 32 | |
| 33 | const std::size_t HASH_SIZE = 160; |
| 34 | |
| 35 | /** |
| 36 | * @ingroup sync |
| 37 | * @brief A simple wrapper for libcrypto hash functions |
| 38 | */ |
| 39 | class Digest |
| 40 | { |
| 41 | public: |
| 42 | /** |
| 43 | * @brief Default constructor. Will initialize internal libssl structures |
| 44 | */ |
| 45 | Digest (); |
| 46 | |
| 47 | /** |
| 48 | * @brief Destructor |
| 49 | */ |
| 50 | ~Digest (); |
| 51 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 52 | /** |
| 53 | * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes |
| 54 | * |
| 55 | * Side effect: Finalize will be called on `this' |
| 56 | */ |
| 57 | std::size_t |
| 58 | getHash (); |
| 59 | |
| 60 | /** |
| 61 | * @brief Compare two full digests |
| 62 | * |
| 63 | * Side effect: Finalize will be called on `this' and `digest' |
| 64 | */ |
| 65 | bool |
| 66 | operator == (Digest &digest); |
| 67 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame^] | 68 | /** |
| 69 | * @brief Combine digests |
| 70 | * @param src digest to combine with |
| 71 | * |
| 72 | * The result of this combination is hash (hash (...)) |
| 73 | */ |
| 74 | Digest & |
| 75 | operator << (const Digest &src); |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 80 | private: |
| 81 | /** |
| 82 | * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception |
| 83 | */ |
| 84 | void Finalize (); |
| 85 | |
| 86 | private: |
| 87 | EVP_MD_CTX *m_context; |
| 88 | uint8_t *m_buffer; |
| 89 | uint32_t m_hashLength; |
| 90 | }; |
| 91 | |
| 92 | struct DigestCalculationError : virtual boost::exception { }; |
| 93 | |
| 94 | } // Sync |
| 95 | } // ns3 |
| 96 | |
| 97 | #endif // SYNC_DIGEST_H |