blob: 7346e23ec78c3b032e0c96ed161edd76f01b4a66 [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
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080024#include "ccnx-common.hpp"
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080025#include <boost/exception/all.hpp>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080026#include <boost/filesystem.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080027#include <boost/shared_ptr.hpp>
28#include <iostream>
29#include <string.h>
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
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080043 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080049 Hash(const void* buf, unsigned int length)
50 : m_length(length)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080051 {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080052 if (m_length != 0) {
53 m_buf = new unsigned char[length];
54 memcpy(m_buf, buf, length);
55 }
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080056 }
57
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 Hash(const Hash& otherHash)
59 : m_length(otherHash.m_length)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080060 {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080061 if (m_length != 0) {
62 m_buf = new unsigned char[m_length];
63 memcpy(m_buf, otherHash.m_buf, otherHash.m_length);
64 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080065 }
66
Alexander Afanasyeva199f972013-01-02 19:37:26 -080067 static HashPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080068 FromString(const std::string& hashInTextEncoding);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080069
Alexander Afanasyeva199f972013-01-02 19:37:26 -080070 static HashPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080071 FromFileContent(const boost::filesystem::path& fileName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080072
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070073 static HashPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080074 FromBytes(const Ccnx::Bytes& bytes);
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -070075
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080076 ~Hash()
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080077 {
Alexander Afanasyevae43c502012-12-29 17:26:37 -080078 if (m_length != 0)
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080079 delete[] m_buf;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080080 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080081
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080082 Hash&
83 operator=(const Hash& otherHash)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080084 {
85 if (m_length != 0)
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080086 delete[] m_buf;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080087
88 m_length = otherHash.m_length;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080089 if (m_length != 0) {
90 m_buf = new unsigned char[m_length];
91 memcpy(m_buf, otherHash.m_buf, otherHash.m_length);
92 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080093 return *this;
94 }
95
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080096 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097 IsZero() const
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080098 {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080099 return m_length == 0 || (m_length == 1 && m_buf[0] == 0);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800100 }
101
102 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 operator==(const Hash& otherHash) const
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800104 {
105 if (m_length != otherHash.m_length)
106 return false;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800107
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800108 return memcmp(m_buf, otherHash.m_buf, m_length) == 0;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800109 }
110
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800111 bool
112 operator<(const Hash& otherHash) const
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800113 {
114 if (m_length < otherHash.m_length)
115 return true;
116
117 if (m_length > otherHash.m_length)
118 return false;
119
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800120 for (unsigned int i = 0; i < m_length; i++) {
121 if (m_buf[i] < otherHash.m_buf[i])
122 return true;
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800123
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800124 if (m_buf[i] > otherHash.m_buf[i])
125 return false;
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800126
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800127 // if equal, continue
128 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800129
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800130 return false;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800131 }
132
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800133 const void*
134 GetHash() const
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800135 {
136 return m_buf;
137 }
138
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800139 unsigned int
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800140 GetHashBytes() const
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800141 {
142 return m_length;
143 }
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800144
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800145 std::string
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800146 shortHash() const;
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800147
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800148private:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800149 unsigned char* m_buf;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800150 unsigned int m_length;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800151
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800152 friend std::ostream&
153 operator<<(std::ostream& os, const Hash& digest);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800154};
155
156namespace Error {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800157struct HashConversion : virtual boost::exception, virtual std::exception
158{
159};
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800160}
161
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800162
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800163std::ostream&
164operator<<(std::ostream& os, const Hash& digest);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800165
166#endif // HASH_STRING_CONVERTER_H