blob: d289bb1e364f540ee6dd8347b51c047fc2423366 [file] [log] [blame]
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyeva199f972013-01-02 19:37:26 -08003 * Copyright (c) 2012-2013 University of California, Los Angeles
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -08004 *
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
20 */
21
Alexander Afanasyeva199f972013-01-02 19:37:26 -080022#ifndef HASH_HELPER_H
23#define HASH_HELPER_H
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080024
25#include <string.h>
26#include <iostream>
27#include <boost/shared_ptr.hpp>
28#include <boost/exception/all.hpp>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080029#include <boost/filesystem.hpp>
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080030
Alexander Afanasyeva199f972013-01-02 19:37:26 -080031// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
32#define HASH_FUNCTION EVP_sha256
33
34class Hash;
35typedef boost::shared_ptr<Hash> HashPtr;
36
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080037class Hash
38{
39public:
Zhenkai Zhue851b952013-01-13 22:29:57 -080040 static unsigned char _origin;
41 static HashPtr Origin;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080042
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070043 Hash ()
44 : m_length(0)
45 , m_buf(0)
46 {
47 }
48
Alexander Afanasyeva199f972013-01-02 19:37:26 -080049 Hash (const void *buf, unsigned int length)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080050 : m_length (length)
51 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080052 if (m_length != 0)
53 {
54 m_buf = new unsigned char [length];
55 memcpy (m_buf, buf, length);
56 }
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080057 }
58
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080059 Hash (const Hash &otherHash)
60 : m_length (otherHash.m_length)
61 {
62 if (m_length != 0)
63 {
64 m_buf = new unsigned char [m_length];
65 memcpy (m_buf, otherHash.m_buf, otherHash.m_length);
66 }
67 }
68
Alexander Afanasyeva199f972013-01-02 19:37:26 -080069 static HashPtr
70 FromString (const std::string &hashInTextEncoding);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080071
Alexander Afanasyeva199f972013-01-02 19:37:26 -080072 static HashPtr
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080073 FromFileContent (const boost::filesystem::path &fileName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080074
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080075 ~Hash ()
76 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080077 if (m_length != 0)
78 delete [] m_buf;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080079 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080080
81 Hash &
82 operator = (const Hash &otherHash)
83 {
84 if (m_length != 0)
85 delete [] m_buf;
86
87 m_length = otherHash.m_length;
88 if (m_length != 0)
89 {
90 m_buf = new unsigned char [m_length];
91 memcpy (m_buf, otherHash.m_buf, otherHash.m_length);
92 }
93 return *this;
94 }
95
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080096 bool
97 IsZero () const
98 {
99 return m_length == 0 ||
100 (m_length == 1 && m_buf[0] == 0);
101 }
102
103 bool
104 operator == (const Hash &otherHash) const
105 {
106 if (m_length != otherHash.m_length)
107 return false;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800108
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800109 return memcmp (m_buf, otherHash.m_buf, m_length) == 0;
110 }
111
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800112 bool operator < (const Hash &otherHash) const
113 {
114 if (m_length < otherHash.m_length)
115 return true;
116
117 if (m_length > otherHash.m_length)
118 return false;
119
Alexander Afanasyevd7245812013-02-13 21:06:57 -0800120 for (unsigned int i = 0; i < m_length; i++)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800121 {
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800122 if (m_buf [i] < otherHash.m_buf [i])
123 return true;
124
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800125 if (m_buf [i] > otherHash.m_buf [i])
126 return false;
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800127
128 // if equal, continue
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800129 }
130
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800131 return false;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800132 }
133
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800134 const void *
135 GetHash () const
136 {
137 return m_buf;
138 }
139
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800140 unsigned int
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800141 GetHashBytes () const
142 {
143 return m_length;
144 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800145
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800146 std::string
147 shortHash () const;
148
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800149private:
150 unsigned char *m_buf;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800151 unsigned int m_length;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800152
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800153 friend std::ostream &
154 operator << (std::ostream &os, const Hash &digest);
155};
156
157namespace Error {
158struct HashConversion : virtual boost::exception, virtual std::exception { };
159}
160
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800161
162std::ostream &
163operator << (std::ostream &os, const Hash &digest);
164
165#endif // HASH_STRING_CONVERTER_H