blob: 2816b3e3c630c49b3e5bc3b4870212395e4a187e [file] [log] [blame]
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -08001/* -*- 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
30namespace ns3 {
31namespace Sync {
32
33const std::size_t HASH_SIZE = 160;
34
35/**
36 * @ingroup sync
37 * @brief A simple wrapper for libcrypto hash functions
38 */
39class Digest
40{
41public:
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
71private:
72 /**
73 * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception
74 */
75 void Finalize ();
76
77private:
78 EVP_MD_CTX *m_context;
79 uint8_t *m_buffer;
80 uint32_t m_hashLength;
81};
82
83struct DigestCalculationError : virtual boost::exception { };
84
85} // Sync
86} // ns3
87
88#endif // SYNC_DIGEST_H