blob: 9050573e20f42a67ae7ad236001c5846c936d10c [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>
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070030#include "ccnx-common.h"
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080031
Alexander Afanasyeva199f972013-01-02 19:37:26 -080032// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
33#define HASH_FUNCTION EVP_sha256
34
35class Hash;
36typedef boost::shared_ptr<Hash> HashPtr;
37
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080038class Hash
39{
40public:
Zhenkai Zhue851b952013-01-13 22:29:57 -080041 static unsigned char _origin;
42 static HashPtr Origin;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080043
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070044 Hash ()
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -070045 : m_buf(0)
46 , m_length(0)
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070047 {
48 }
49
Alexander Afanasyeva199f972013-01-02 19:37:26 -080050 Hash (const void *buf, unsigned int length)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080051 : m_length (length)
52 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080053 if (m_length != 0)
54 {
55 m_buf = new unsigned char [length];
56 memcpy (m_buf, buf, length);
57 }
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080058 }
59
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080060 Hash (const Hash &otherHash)
61 : m_length (otherHash.m_length)
62 {
63 if (m_length != 0)
64 {
65 m_buf = new unsigned char [m_length];
66 memcpy (m_buf, otherHash.m_buf, otherHash.m_length);
67 }
68 }
69
Alexander Afanasyeva199f972013-01-02 19:37:26 -080070 static HashPtr
71 FromString (const std::string &hashInTextEncoding);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080072
Alexander Afanasyeva199f972013-01-02 19:37:26 -080073 static HashPtr
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080074 FromFileContent (const boost::filesystem::path &fileName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080075
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070076 static HashPtr
77 FromBytes (const Ccnx::Bytes &bytes);
78
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080079 ~Hash ()
80 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080081 if (m_length != 0)
82 delete [] m_buf;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080083 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080084
85 Hash &
86 operator = (const Hash &otherHash)
87 {
88 if (m_length != 0)
89 delete [] m_buf;
90
91 m_length = otherHash.m_length;
92 if (m_length != 0)
93 {
94 m_buf = new unsigned char [m_length];
95 memcpy (m_buf, otherHash.m_buf, otherHash.m_length);
96 }
97 return *this;
98 }
99
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800100 bool
101 IsZero () const
102 {
103 return m_length == 0 ||
104 (m_length == 1 && m_buf[0] == 0);
105 }
106
107 bool
108 operator == (const Hash &otherHash) const
109 {
110 if (m_length != otherHash.m_length)
111 return false;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800112
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800113 return memcmp (m_buf, otherHash.m_buf, m_length) == 0;
114 }
115
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800116 bool operator < (const Hash &otherHash) const
117 {
118 if (m_length < otherHash.m_length)
119 return true;
120
121 if (m_length > otherHash.m_length)
122 return false;
123
Alexander Afanasyevd7245812013-02-13 21:06:57 -0800124 for (unsigned int i = 0; i < m_length; i++)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800125 {
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800126 if (m_buf [i] < otherHash.m_buf [i])
127 return true;
128
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800129 if (m_buf [i] > otherHash.m_buf [i])
130 return false;
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800131
132 // if equal, continue
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800133 }
134
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800135 return false;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800136 }
137
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800138 const void *
139 GetHash () const
140 {
141 return m_buf;
142 }
143
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800144 unsigned int
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800145 GetHashBytes () const
146 {
147 return m_length;
148 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800149
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800150 std::string
151 shortHash () const;
152
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800153private:
154 unsigned char *m_buf;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800155 unsigned int m_length;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800156
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800157 friend std::ostream &
158 operator << (std::ostream &os, const Hash &digest);
159};
160
161namespace Error {
162struct HashConversion : virtual boost::exception, virtual std::exception { };
163}
164
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800165
166std::ostream &
167operator << (std::ostream &os, const Hash &digest);
168
169#endif // HASH_STRING_CONVERTER_H