blob: fca979868edddb5320a60d8503e04d04bbadcc14 [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
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080030namespace Sync {
31
32const std::size_t HASH_SIZE = 160;
33
34/**
35 * @ingroup sync
36 * @brief A simple wrapper for libcrypto hash functions
37 */
38class Digest
39{
40public:
41 /**
42 * @brief Default constructor. Will initialize internal libssl structures
43 */
44 Digest ();
45
46 /**
47 * @brief Destructor
48 */
49 ~Digest ();
50
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080051 /**
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 Afanasyevdf718f52012-03-02 00:23:04 -080067 /**
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 Afanasyev8f25cbb2012-03-01 23:53:40 -080079private:
80 /**
81 * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception
82 */
83 void Finalize ();
84
85private:
86 EVP_MD_CTX *m_context;
87 uint8_t *m_buffer;
88 uint32_t m_hashLength;
89};
90
91struct DigestCalculationError : virtual boost::exception { };
92
93} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080094
95#endif // SYNC_DIGEST_H