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 | |
| 52 | // Digest & |
| 53 | // operator << ( |
| 54 | |
| 55 | /** |
| 56 | * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes |
| 57 | * |
| 58 | * Side effect: Finalize will be called on `this' |
| 59 | */ |
| 60 | std::size_t |
| 61 | getHash (); |
| 62 | |
| 63 | /** |
| 64 | * @brief Compare two full digests |
| 65 | * |
| 66 | * Side effect: Finalize will be called on `this' and `digest' |
| 67 | */ |
| 68 | bool |
| 69 | operator == (Digest &digest); |
| 70 | |
| 71 | private: |
| 72 | /** |
| 73 | * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception |
| 74 | */ |
| 75 | void Finalize (); |
| 76 | |
| 77 | private: |
| 78 | EVP_MD_CTX *m_context; |
| 79 | uint8_t *m_buffer; |
| 80 | uint32_t m_hashLength; |
| 81 | }; |
| 82 | |
| 83 | struct DigestCalculationError : virtual boost::exception { }; |
| 84 | |
| 85 | } // Sync |
| 86 | } // ns3 |
| 87 | |
| 88 | #endif // SYNC_DIGEST_H |