blob: b7dbe41e54a26116ad85fee112ad9b5e3b9caa18 [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- 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>
akmhoquefdbddb12014-05-02 18:35:19 -050020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
akmhoque66e66182014-02-21 17:56:03 -060021 */
22
23#ifndef SYNC_DIGEST_H
24#define SYNC_DIGEST_H
25
Vince Lehman0a7da612014-10-29 14:39:29 -050026#include "sync-common.h"
27
akmhoque66e66182014-02-21 17:56:03 -060028#include <vector>
akmhoquefdbddb12014-05-02 18:35:19 -050029#include "openssl.h"
akmhoque66e66182014-02-21 17:56:03 -060030
31namespace Sync {
32
33/**
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 /**
46 * @brief Check if digest is empty
47 */
48 bool
49 empty () const;
akmhoquefdbddb12014-05-02 18:35:19 -050050
akmhoque66e66182014-02-21 17:56:03 -060051 /**
52 * @brief Reset digest to the initial state
53 */
54 void
55 reset ();
56
57 /**
58 * @brief Destructor
59 */
60 ~Digest ();
61
62 /**
63 * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes
64 *
65 * Side effect: finalize() will be called on `this'
66 */
67 std::size_t
68 getHash () const;
69
70 /**
71 * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception
72 */
73 void
74 finalize ();
75
76 /**
77 * @brief Compare two full digests
78 *
79 * Side effect: Finalize will be called on `this' and `digest'
80 */
81 bool
82 operator == (const Digest &digest) const;
83
84 bool
85 operator != (const Digest &digest) const
86 { return ! (*this == digest); }
akmhoquefdbddb12014-05-02 18:35:19 -050087
akmhoque66e66182014-02-21 17:56:03 -060088
89 /**
90 * @brief Add existing digest to digest calculation
91 * @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
98 /**
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);
104
105 /**
106 * @brief Add uint64_t value to digest calculation
107 * @param value uint64_t value to put into digest
108 */
109 inline Digest &
110 operator << (uint64_t value);
111
112 /**
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
118 isZero () const;
akmhoquefdbddb12014-05-02 18:35:19 -0500119
akmhoque66e66182014-02-21 17:56:03 -0600120private:
121 Digest &
122 operator = (Digest &digest) { (void)digest; return *this; }
akmhoquefdbddb12014-05-02 18:35:19 -0500123
akmhoque66e66182014-02-21 17:56:03 -0600124 /**
125 * @brief Add size bytes of buffer to the hash
126 */
127 void
128 update (const uint8_t *buffer, size_t size);
129
130 friend std::ostream &
131 operator << (std::ostream &os, const Digest &digest);
132
133 friend std::istream &
134 operator >> (std::istream &is, Digest &digest);
akmhoquefdbddb12014-05-02 18:35:19 -0500135
akmhoque66e66182014-02-21 17:56:03 -0600136private:
137 EVP_MD_CTX *m_context;
138 std::vector<uint8_t> m_buffer;
139};
140
141namespace Error {
142struct DigestCalculationError : virtual boost::exception, virtual std::exception { };
143}
144
Vince Lehman0a7da612014-10-29 14:39:29 -0500145typedef shared_ptr<Digest> DigestPtr;
146typedef shared_ptr<const Digest> DigestConstPtr;
akmhoque66e66182014-02-21 17:56:03 -0600147
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 << (uint64_t value)
157{
158 update (reinterpret_cast<const uint8_t*> (&value), sizeof (uint64_t));
159 return *this;
160}
161
162std::ostream &
163operator << (std::ostream &os, const Digest &digest);
164
165std::istream &
166operator >> (std::istream &is, Digest &digest);
167
168// template<class INT>
169// Digest &
170// Digest::operator << (INT value)
171// {
172// update (&value, sizeof (INT));
173// return *this;
174// }
175
176struct 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
196
197} // Sync
198
199#endif // SYNC_DIGEST_H