blob: 422486052802f8b2fe197d9b34c5e30a5b3bd089 [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
Alexander Afanasyeva199f972013-01-02 19:37:26 -080043 Hash (const void *buf, unsigned int length)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080044 : m_length (length)
45 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080046 if (m_length != 0)
47 {
48 m_buf = new unsigned char [length];
49 memcpy (m_buf, buf, length);
50 }
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080051 }
52
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080053 Hash (const Hash &otherHash)
54 : m_length (otherHash.m_length)
55 {
56 if (m_length != 0)
57 {
58 m_buf = new unsigned char [m_length];
59 memcpy (m_buf, otherHash.m_buf, otherHash.m_length);
60 }
61 }
62
Alexander Afanasyeva199f972013-01-02 19:37:26 -080063 static HashPtr
64 FromString (const std::string &hashInTextEncoding);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080065
Alexander Afanasyeva199f972013-01-02 19:37:26 -080066 static HashPtr
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080067 FromFileContent (const boost::filesystem::path &fileName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080068
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080069 ~Hash ()
70 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080071 if (m_length != 0)
72 delete [] m_buf;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080073 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080074
75 Hash &
76 operator = (const Hash &otherHash)
77 {
78 if (m_length != 0)
79 delete [] m_buf;
80
81 m_length = otherHash.m_length;
82 if (m_length != 0)
83 {
84 m_buf = new unsigned char [m_length];
85 memcpy (m_buf, otherHash.m_buf, otherHash.m_length);
86 }
87 return *this;
88 }
89
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080090 bool
91 IsZero () const
92 {
93 return m_length == 0 ||
94 (m_length == 1 && m_buf[0] == 0);
95 }
96
97 bool
98 operator == (const Hash &otherHash) const
99 {
100 if (m_length != otherHash.m_length)
101 return false;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800102
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800103 return memcmp (m_buf, otherHash.m_buf, m_length) == 0;
104 }
105
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800106 bool operator < (const Hash &otherHash) const
107 {
108 if (m_length < otherHash.m_length)
109 return true;
110
111 if (m_length > otherHash.m_length)
112 return false;
113
114 for (int i = 0; i < m_length; i++)
115 {
116 if (m_buf [i] > otherHash.m_buf [i])
117 return false;
118 }
119
120 return true;
121 }
122
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800123 const void *
124 GetHash () const
125 {
126 return m_buf;
127 }
128
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800129 unsigned int
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800130 GetHashBytes () const
131 {
132 return m_length;
133 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800134
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800135private:
136 unsigned char *m_buf;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800137 unsigned int m_length;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800138
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800139 friend std::ostream &
140 operator << (std::ostream &os, const Hash &digest);
141};
142
143namespace Error {
144struct HashConversion : virtual boost::exception, virtual std::exception { };
145}
146
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800147
148std::ostream &
149operator << (std::ostream &os, const Hash &digest);
150
151#endif // HASH_STRING_CONVERTER_H