blob: d0c76f5e804457bd657971cdbe6d8496abffb44c [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 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080047 * @brief Reset digest to the initial state
48 */
49 void
50 reset ();
51
52 /**
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080053 * @brief Destructor
54 */
55 ~Digest ();
56
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080057 /**
58 * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes
59 *
60 * Side effect: Finalize will be called on `this'
61 */
62 std::size_t
63 getHash ();
64
65 /**
66 * @brief Compare two full digests
67 *
68 * Side effect: Finalize will be called on `this' and `digest'
69 */
70 bool
71 operator == (Digest &digest);
72
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080073 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080074 * @brief Add existing digest to digest calculation
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080075 * @param src digest to combine with
76 *
77 * The result of this combination is hash (hash (...))
78 */
79 Digest &
80 operator << (const Digest &src);
81
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080082 /**
83 * @brief Add string to digest calculation
84 * @param str string to put into digest
85 */
86 inline Digest &
87 operator << (const std::string &str);
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080088
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080089 inline Digest &
90 operator << (uint32_t value);
91
92 // /**
93 // * @brief Add integer to digest calculation
94 // * @param value the value to add to the digest
95 // */
96 // template<class INT>
97 // inline Digest &
98 // operator << (INT value);
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080099
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800100private:
101 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800102 * @brief Disabled copy operator
103 */
104 Digest &
105 operator = (Digest &digest) { return *this; }
106
107 /**
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800108 * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception
109 */
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800110 void
111 finalize ();
112
113 /**
114 * @brief Add size bytes of buffer to the hash
115 */
116 void
117 update (const uint8_t *buffer, size_t size);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800118
119private:
120 EVP_MD_CTX *m_context;
121 uint8_t *m_buffer;
122 uint32_t m_hashLength;
123};
124
125struct DigestCalculationError : virtual boost::exception { };
126
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800127
128Digest &
129Digest::operator << (const std::string &str)
130{
131 update (reinterpret_cast<const uint8_t*> (str.c_str ()), str.size ());
132 return *this;
133}
134
135inline Digest &
136Digest::operator << (uint32_t value)
137{
138 update (reinterpret_cast<const uint8_t*> (&value), sizeof (uint32_t));
139 return *this;
140}
141
142// template<class INT>
143// Digest &
144// Digest::operator << (INT value)
145// {
146// update (&value, sizeof (INT));
147// return *this;
148// }
149
150
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800151} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800152
153#endif // SYNC_DIGEST_H