blob: 7cef28f71a17353aec01fc18926ea4be5f966459 [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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080020 * 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>
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080029#include <vector>
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080030
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080031namespace Sync {
32
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080033/**
34 * @ingroup sync
35 * @brief A simple wrapper for libcrypto hash functions
36 */
37class Digest
38{
39public:
40 /**
41 * @brief Default constructor. Will initialize internal libssl structures
42 */
43 Digest ();
44
45 /**
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080046 * @brief Check if digest is empty
47 */
48 bool
49 empty () const;
50
51 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080052 * @brief Reset digest to the initial state
53 */
54 void
55 reset ();
56
57 /**
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080058 * @brief Destructor
59 */
60 ~Digest ();
61
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080062 /**
63 * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes
64 *
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080065 * Side effect: finalize() will be called on `this'
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080066 */
67 std::size_t
Alexander Afanasyeva5858032012-03-09 15:55:10 -080068 getHash () const;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080069
70 /**
Alexander Afanasyev1fbdcdd2012-03-05 23:14:33 -080071 * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception
72 */
73 void
74 finalize ();
75
76 /**
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080077 * @brief Compare two full digests
78 *
79 * Side effect: Finalize will be called on `this' and `digest'
80 */
81 bool
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080082 operator == (const Digest &digest) const;
83
84 bool
85 operator != (const Digest &digest) const
86 { return ! (*this == digest); }
87
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080088
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080089 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080090 * @brief Add existing digest to digest calculation
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080091 * @param src digest to combine with
92 *
93 * The result of this combination is hash (hash (...))
94 */
95 Digest &
96 operator << (const Digest &src);
97
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080098 /**
99 * @brief Add string to digest calculation
100 * @param str string to put into digest
101 */
102 inline Digest &
103 operator << (const std::string &str);
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800104
Alexander Afanasyev58c77b02012-03-05 21:52:25 -0800105 /**
106 * @brief Add uint32_t value to digest calculation
107 * @param value uint32_t value to put into digest
108 */
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800109 inline Digest &
110 operator << (uint32_t value);
111
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700112 /**
113 * @brief Checks if the stored hash is zero-root hash
114 *
115 * Zero-root hash is a valid hash that optimally represents an empty state
116 */
117 bool
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700118 isZero () const;
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700119
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800120private:
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800121 Digest &
Chaoyi Bianb1190282012-06-13 17:15:07 -0700122 operator = (Digest &digest) { (void)digest; return *this; }
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800123
124 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800125 * @brief Add size bytes of buffer to the hash
126 */
127 void
128 update (const uint8_t *buffer, size_t size);
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800129
130 friend std::ostream &
131 operator << (std::ostream &os, const Digest &digest);
132
133 friend std::istream &
134 operator >> (std::istream &is, Digest &digest);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800135
136private:
137 EVP_MD_CTX *m_context;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800138 std::vector<uint8_t> m_buffer;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800139};
140
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800141namespace Error {
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800142struct DigestCalculationError : virtual boost::exception, virtual std::exception { };
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800143}
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800144
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800145typedef boost::shared_ptr<Digest> DigestPtr;
146typedef boost::shared_ptr<const Digest> DigestConstPtr;
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800147
148Digest &
149Digest::operator << (const std::string &str)
150{
151 update (reinterpret_cast<const uint8_t*> (str.c_str ()), str.size ());
152 return *this;
153}
154
155inline Digest &
156Digest::operator << (uint32_t value)
157{
158 update (reinterpret_cast<const uint8_t*> (&value), sizeof (uint32_t));
159 return *this;
160}
161
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800162std::ostream &
163operator << (std::ostream &os, const Digest &digest);
164
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800165std::istream &
166operator >> (std::istream &is, Digest &digest);
167
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800168// template<class INT>
169// Digest &
170// Digest::operator << (INT value)
171// {
172// update (&value, sizeof (INT));
173// return *this;
174// }
175
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700176struct DigestPtrHash : public std::unary_function<Digest, std::size_t>
177{
178 std::size_t
179 operator() (DigestConstPtr digest) const
180 {
181 // std::cout << "digest->getHash: " << digest->getHash () << " (" << *digest << ")" << std::endl;
182 return digest->getHash ();
183 }
184};
185
186struct DigestPtrEqual : public std::unary_function<Digest, std::size_t>
187{
188 bool
189 operator() (DigestConstPtr digest1, DigestConstPtr digest2) const
190 {
191 // std::cout << boost::cref(*digest1) << " == " << boost::cref(*digest2) << " : " << (*digest1 == *digest2) << std::endl;
192 return *digest1 == *digest2;
193 }
194};
195
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800196
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800197} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800198
199#endif // SYNC_DIGEST_H