blob: 638a93a3fa3a77e99bb9637df10215b00ce4bfdd [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 Afanasyeva199f972013-01-02 19:37:26 -080042 Hash (const void *buf, unsigned int length)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080043 : m_length (length)
44 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080045 if (m_length != 0)
46 {
47 m_buf = new unsigned char [length];
48 memcpy (m_buf, buf, length);
49 }
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080050 }
51
Alexander Afanasyeva199f972013-01-02 19:37:26 -080052 static HashPtr
53 FromString (const std::string &hashInTextEncoding);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080054
Alexander Afanasyeva199f972013-01-02 19:37:26 -080055 static HashPtr
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080056 FromFileContent (const boost::filesystem::path &fileName);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080057
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080058 ~Hash ()
59 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080060 if (m_length != 0)
61 delete [] m_buf;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080062 }
63
64 bool
65 IsZero () const
66 {
67 return m_length == 0 ||
68 (m_length == 1 && m_buf[0] == 0);
69 }
70
71 bool
72 operator == (const Hash &otherHash) const
73 {
74 if (m_length != otherHash.m_length)
75 return false;
76
77 return memcmp (m_buf, otherHash.m_buf, m_length) == 0;
78 }
79
80 const void *
81 GetHash () const
82 {
83 return m_buf;
84 }
85
Alexander Afanasyeva199f972013-01-02 19:37:26 -080086 unsigned int
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080087 GetHashBytes () const
88 {
89 return m_length;
90 }
91
92private:
93 unsigned char *m_buf;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080094 unsigned int m_length;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080095
Alexander Afanasyevae43c502012-12-29 17:26:37 -080096 Hash (const Hash &) { }
97 Hash & operator = (const Hash &) { return *this; }
98
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080099 friend std::ostream &
100 operator << (std::ostream &os, const Hash &digest);
101};
102
103namespace Error {
104struct HashConversion : virtual boost::exception, virtual std::exception { };
105}
106
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800107
108std::ostream &
109operator << (std::ostream &os, const Hash &digest);
110
111#endif // HASH_STRING_CONVERTER_H