blob: d4f750d2193e5241535a6387309de283bfe313b9 [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:
Alexander Afanasyeva199f972013-01-02 19:37:26 -080040 Hash (const void *buf, unsigned int length)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080041 : m_length (length)
42 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080043 if (m_length != 0)
44 {
45 m_buf = new unsigned char [length];
46 memcpy (m_buf, buf, length);
47 }
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080048 }
49
Alexander Afanasyeva199f972013-01-02 19:37:26 -080050 static HashPtr
51 FromString (const std::string &hashInTextEncoding);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080052
Alexander Afanasyeva199f972013-01-02 19:37:26 -080053 static HashPtr
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080054 FromFileContent (const boost::filesystem::path &fileName);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080055
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080056 ~Hash ()
57 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080058 if (m_length != 0)
59 delete [] m_buf;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080060 }
61
62 bool
63 IsZero () const
64 {
65 return m_length == 0 ||
66 (m_length == 1 && m_buf[0] == 0);
67 }
68
69 bool
70 operator == (const Hash &otherHash) const
71 {
72 if (m_length != otherHash.m_length)
73 return false;
74
75 return memcmp (m_buf, otherHash.m_buf, m_length) == 0;
76 }
77
78 const void *
79 GetHash () const
80 {
81 return m_buf;
82 }
83
Alexander Afanasyeva199f972013-01-02 19:37:26 -080084 unsigned int
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080085 GetHashBytes () const
86 {
87 return m_length;
88 }
89
90private:
91 unsigned char *m_buf;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080092 unsigned int m_length;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080093
Alexander Afanasyevae43c502012-12-29 17:26:37 -080094 Hash (const Hash &) { }
95 Hash & operator = (const Hash &) { return *this; }
96
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080097 friend std::ostream &
98 operator << (std::ostream &os, const Hash &digest);
99};
100
101namespace Error {
102struct HashConversion : virtual boost::exception, virtual std::exception { };
103}
104
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800105
106std::ostream &
107operator << (std::ostream &os, const Hash &digest);
108
109#endif // HASH_STRING_CONVERTER_H