blob: ea12f30d79ef709b830834c7d38bc298aa5f3456 [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>
29
Alexander Afanasyeva199f972013-01-02 19:37:26 -080030// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
31#define HASH_FUNCTION EVP_sha256
32
33class Hash;
34typedef boost::shared_ptr<Hash> HashPtr;
35
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080036class Hash
37{
38public:
Alexander Afanasyeva199f972013-01-02 19:37:26 -080039 Hash (const void *buf, unsigned int length)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080040 : m_length (length)
41 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080042 if (m_length != 0)
43 {
44 m_buf = new unsigned char [length];
45 memcpy (m_buf, buf, length);
46 }
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080047 }
48
Alexander Afanasyeva199f972013-01-02 19:37:26 -080049 static HashPtr
50 FromString (const std::string &hashInTextEncoding);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080051
Alexander Afanasyeva199f972013-01-02 19:37:26 -080052 static HashPtr
53 FromFileContent (const std::string &hashInTextEncoding);
54
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080055 ~Hash ()
56 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080057 if (m_length != 0)
58 delete [] m_buf;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080059 }
60
61 bool
62 IsZero () const
63 {
64 return m_length == 0 ||
65 (m_length == 1 && m_buf[0] == 0);
66 }
67
68 bool
69 operator == (const Hash &otherHash) const
70 {
71 if (m_length != otherHash.m_length)
72 return false;
73
74 return memcmp (m_buf, otherHash.m_buf, m_length) == 0;
75 }
76
77 const void *
78 GetHash () const
79 {
80 return m_buf;
81 }
82
Alexander Afanasyeva199f972013-01-02 19:37:26 -080083 unsigned int
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080084 GetHashBytes () const
85 {
86 return m_length;
87 }
88
89private:
90 unsigned char *m_buf;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080091 unsigned int m_length;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080092
Alexander Afanasyevae43c502012-12-29 17:26:37 -080093 Hash (const Hash &) { }
94 Hash & operator = (const Hash &) { return *this; }
95
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080096 friend std::ostream &
97 operator << (std::ostream &os, const Hash &digest);
98};
99
100namespace Error {
101struct HashConversion : virtual boost::exception, virtual std::exception { };
102}
103
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800104
105std::ostream &
106operator << (std::ostream &os, const Hash &digest);
107
108#endif // HASH_STRING_CONVERTER_H