blob: f5c83f60ed248696cc7753521b45b3491a749991 [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 Afanasyeve00ffbe2012-03-05 00:01:36 -080045 * @brief Reset digest to the initial state
46 */
47 void
48 reset ();
49
50 /**
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080051 * @brief Destructor
52 */
53 ~Digest ();
54
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080055 /**
56 * @brief Obtain a short version of the hash (just first sizeof(size_t) bytes
57 *
58 * Side effect: Finalize will be called on `this'
59 */
60 std::size_t
61 getHash ();
62
63 /**
64 * @brief Compare two full digests
65 *
66 * Side effect: Finalize will be called on `this' and `digest'
67 */
68 bool
69 operator == (Digest &digest);
70
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080071 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080072 * @brief Add existing digest to digest calculation
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080073 * @param src digest to combine with
74 *
75 * The result of this combination is hash (hash (...))
76 */
77 Digest &
78 operator << (const Digest &src);
79
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080080 /**
81 * @brief Add string to digest calculation
82 * @param str string to put into digest
83 */
84 inline Digest &
85 operator << (const std::string &str);
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080086
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080087 inline Digest &
88 operator << (uint32_t value);
89
90 // /**
91 // * @brief Add integer to digest calculation
92 // * @param value the value to add to the digest
93 // */
94 // template<class INT>
95 // inline Digest &
96 // operator << (INT value);
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080097
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080098private:
99 /**
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800100 * @brief Disabled copy operator
101 */
102 Digest &
103 operator = (Digest &digest) { return *this; }
104
105 /**
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800106 * @brief Finalize digest. All subsequent calls to "operator <<" will fire an exception
107 */
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800108 void
109 finalize ();
110
111 /**
112 * @brief Add size bytes of buffer to the hash
113 */
114 void
115 update (const uint8_t *buffer, size_t size);
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800116
117 friend std::ostream &
118 operator << (std::ostream &os, const Digest &digest);
119
120 friend std::istream &
121 operator >> (std::istream &is, Digest &digest);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800122
123private:
124 EVP_MD_CTX *m_context;
125 uint8_t *m_buffer;
126 uint32_t m_hashLength;
127};
128
129struct DigestCalculationError : virtual boost::exception { };
130
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800131typedef boost::shared_ptr<Digest> DigestPtr;
132typedef boost::shared_ptr<const Digest> DigestConstPtr;
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800133
134Digest &
135Digest::operator << (const std::string &str)
136{
137 update (reinterpret_cast<const uint8_t*> (str.c_str ()), str.size ());
138 return *this;
139}
140
141inline Digest &
142Digest::operator << (uint32_t value)
143{
144 update (reinterpret_cast<const uint8_t*> (&value), sizeof (uint32_t));
145 return *this;
146}
147
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800148std::ostream &
149operator << (std::ostream &os, const Digest &digest);
150
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800151std::istream &
152operator >> (std::istream &is, Digest &digest);
153
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800154// template<class INT>
155// Digest &
156// Digest::operator << (INT value)
157// {
158// update (&value, sizeof (INT));
159// return *this;
160// }
161
162
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800163} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800164
165#endif // SYNC_DIGEST_H