blob: 91b331e0c417852fd438615967431d07ca489dbb [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
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080032/**
33 * @ingroup sync
34 * @brief A simple wrapper for libcrypto hash functions
35 */
36class Digest
37{
38public:
39 /**
40 * @brief Default constructor. Will initialize internal libssl structures
41 */
42 Digest ();
43
44 /**
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080045 * @brief Check if digest is empty
46 */
47 bool
48 empty () const;
49
50 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080051 * @brief Reset digest to the initial state
52 */
53 void
54 reset ();
55
56 /**
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080057 * @brief Destructor
58 */
59 ~Digest ();
60
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080061 /**
62 * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes
63 *
64 * Side effect: Finalize will be called on `this'
65 */
66 std::size_t
67 getHash ();
68
69 /**
70 * @brief Compare two full digests
71 *
72 * Side effect: Finalize will be called on `this' and `digest'
73 */
74 bool
Alexander Afanasyevb71beab2012-03-05 21:13:49 -080075 operator == (const Digest &digest) const;
76
77 bool
78 operator != (const Digest &digest) const
79 { return ! (*this == digest); }
80
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080081
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080082 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080083 * @brief Add existing digest to digest calculation
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080084 * @param src digest to combine with
85 *
86 * The result of this combination is hash (hash (...))
87 */
88 Digest &
89 operator << (const Digest &src);
90
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080091 /**
92 * @brief Add string to digest calculation
93 * @param str string to put into digest
94 */
95 inline Digest &
96 operator << (const std::string &str);
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080097
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080098 inline Digest &
99 operator << (uint32_t value);
100
101 // /**
102 // * @brief Add integer to digest calculation
103 // * @param value the value to add to the digest
104 // */
105 // template<class INT>
106 // inline Digest &
107 // operator << (INT value);
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800108
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800109private:
110 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800111 * @brief Disabled copy operator
112 */
113 Digest &
114 operator = (Digest &digest) { return *this; }
115
116 /**
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800117 * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception
118 */
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800119 void
120 finalize ();
121
122 /**
123 * @brief Add size bytes of buffer to the hash
124 */
125 void
126 update (const uint8_t *buffer, size_t size);
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800127
128 friend std::ostream &
129 operator << (std::ostream &os, const Digest &digest);
130
131 friend std::istream &
132 operator >> (std::istream &is, Digest &digest);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800133
134private:
135 EVP_MD_CTX *m_context;
136 uint8_t *m_buffer;
137 uint32_t m_hashLength;
138};
139
140struct DigestCalculationError : virtual boost::exception { };
141
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800142typedef boost::shared_ptr<Digest> DigestPtr;
143typedef boost::shared_ptr<const Digest> DigestConstPtr;
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800144
145Digest &
146Digest::operator << (const std::string &str)
147{
148 update (reinterpret_cast<const uint8_t*> (str.c_str ()), str.size ());
149 return *this;
150}
151
152inline Digest &
153Digest::operator << (uint32_t value)
154{
155 update (reinterpret_cast<const uint8_t*> (&value), sizeof (uint32_t));
156 return *this;
157}
158
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800159std::ostream &
160operator << (std::ostream &os, const Digest &digest);
161
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800162std::istream &
163operator >> (std::istream &is, Digest &digest);
164
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800165// template<class INT>
166// Digest &
167// Digest::operator << (INT value)
168// {
169// update (&value, sizeof (INT));
170// return *this;
171// }
172
173
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800174} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800175
176#endif // SYNC_DIGEST_H