blob: 93768716e19a735bf7a8d3add305233a1de5ae18 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080019 */
20
Alexander Afanasyeva199f972013-01-02 19:37:26 -080021#ifndef HASH_HELPER_H
22#define HASH_HELPER_H
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080023
24#include <string.h>
25#include <iostream>
26#include <boost/shared_ptr.hpp>
27#include <boost/exception/all.hpp>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080028#include <boost/filesystem.hpp>
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080029#include "ccnx-common.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
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070043 Hash ()
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -070044 : m_buf(0)
45 , m_length(0)
Zhenkai Zhu3457ed42013-03-12 15:15:21 -070046 {
47 }
48
Alexander Afanasyeva199f972013-01-02 19:37:26 -080049 Hash (const void *buf, unsigned int length)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080050 : m_length (length)
51 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080052 if (m_length != 0)
53 {
54 m_buf = new unsigned char [length];
55 memcpy (m_buf, buf, length);
56 }
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080057 }
58
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080059 Hash (const Hash &otherHash)
60 : m_length (otherHash.m_length)
61 {
62 if (m_length != 0)
63 {
64 m_buf = new unsigned char [m_length];
65 memcpy (m_buf, otherHash.m_buf, otherHash.m_length);
66 }
67 }
68
Alexander Afanasyeva199f972013-01-02 19:37:26 -080069 static HashPtr
70 FromString (const std::string &hashInTextEncoding);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080071
Alexander Afanasyeva199f972013-01-02 19:37:26 -080072 static HashPtr
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080073 FromFileContent (const boost::filesystem::path &fileName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080074
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070075 static HashPtr
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070076 FromBytes (const Ndnx::Bytes &bytes);
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070077
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080078 ~Hash ()
79 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080080 if (m_length != 0)
81 delete [] m_buf;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080082 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080083
84 Hash &
85 operator = (const Hash &otherHash)
86 {
87 if (m_length != 0)
88 delete [] m_buf;
89
90 m_length = otherHash.m_length;
91 if (m_length != 0)
92 {
93 m_buf = new unsigned char [m_length];
94 memcpy (m_buf, otherHash.m_buf, otherHash.m_length);
95 }
96 return *this;
97 }
98
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080099 bool
100 IsZero () const
101 {
102 return m_length == 0 ||
103 (m_length == 1 && m_buf[0] == 0);
104 }
105
106 bool
107 operator == (const Hash &otherHash) const
108 {
109 if (m_length != otherHash.m_length)
110 return false;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800111
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800112 return memcmp (m_buf, otherHash.m_buf, m_length) == 0;
113 }
114
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800115 bool operator < (const Hash &otherHash) const
116 {
117 if (m_length < otherHash.m_length)
118 return true;
119
120 if (m_length > otherHash.m_length)
121 return false;
122
Alexander Afanasyevd7245812013-02-13 21:06:57 -0800123 for (unsigned int i = 0; i < m_length; i++)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800124 {
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800125 if (m_buf [i] < otherHash.m_buf [i])
126 return true;
127
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800128 if (m_buf [i] > otherHash.m_buf [i])
129 return false;
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800130
131 // if equal, continue
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800132 }
133
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800134 return false;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800135 }
136
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800137 const void *
138 GetHash () const
139 {
140 return m_buf;
141 }
142
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800143 unsigned int
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800144 GetHashBytes () const
145 {
146 return m_length;
147 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800148
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800149 std::string
150 shortHash () const;
151
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800152private:
153 unsigned char *m_buf;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800154 unsigned int m_length;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800155
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800156 friend std::ostream &
157 operator << (std::ostream &os, const Hash &digest);
158};
159
160namespace Error {
161struct HashConversion : virtual boost::exception, virtual std::exception { };
162}
163
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800164
165std::ostream &
166operator << (std::ostream &os, const Hash &digest);
167
168#endif // HASH_STRING_CONVERTER_H