Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 7 | * 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 Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 11 | * 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 Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 15 | * 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 Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 21 | #ifndef HASH_HELPER_H |
| 22 | #define HASH_HELPER_H |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 23 | |
| 24 | #include <string.h> |
| 25 | #include <iostream> |
| 26 | #include <boost/shared_ptr.hpp> |
| 27 | #include <boost/exception/all.hpp> |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 28 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 29 | #include "ccnx-common.hpp" |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 31 | // 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 | |
| 34 | class Hash; |
| 35 | typedef boost::shared_ptr<Hash> HashPtr; |
| 36 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 37 | class Hash |
| 38 | { |
| 39 | public: |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 40 | static unsigned char _origin; |
| 41 | static HashPtr Origin; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 42 | |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 43 | Hash () |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame] | 44 | : m_buf(0) |
| 45 | , m_length(0) |
Zhenkai Zhu | 3457ed4 | 2013-03-12 15:15:21 -0700 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 49 | Hash (const void *buf, unsigned int length) |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 50 | : m_length (length) |
| 51 | { |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 52 | if (m_length != 0) |
| 53 | { |
| 54 | m_buf = new unsigned char [length]; |
| 55 | memcpy (m_buf, buf, length); |
| 56 | } |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 59 | 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 Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 69 | static HashPtr |
| 70 | FromString (const std::string &hashInTextEncoding); |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 72 | static HashPtr |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 73 | FromFileContent (const boost::filesystem::path &fileName); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 74 | |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 75 | static HashPtr |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 76 | FromBytes (const Ndnx::Bytes &bytes); |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 77 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 78 | ~Hash () |
| 79 | { |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 80 | if (m_length != 0) |
| 81 | delete [] m_buf; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 82 | } |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 83 | |
| 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 Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 99 | 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 Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 111 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 112 | return memcmp (m_buf, otherHash.m_buf, m_length) == 0; |
| 113 | } |
| 114 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 115 | 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 Afanasyev | d724581 | 2013-02-13 21:06:57 -0800 | [diff] [blame] | 123 | for (unsigned int i = 0; i < m_length; i++) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 124 | { |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 125 | if (m_buf [i] < otherHash.m_buf [i]) |
| 126 | return true; |
| 127 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 128 | if (m_buf [i] > otherHash.m_buf [i]) |
| 129 | return false; |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 130 | |
| 131 | // if equal, continue |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 134 | return false; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 137 | const void * |
| 138 | GetHash () const |
| 139 | { |
| 140 | return m_buf; |
| 141 | } |
| 142 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 143 | unsigned int |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 144 | GetHashBytes () const |
| 145 | { |
| 146 | return m_length; |
| 147 | } |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 149 | std::string |
| 150 | shortHash () const; |
| 151 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 152 | private: |
| 153 | unsigned char *m_buf; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 154 | unsigned int m_length; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 155 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 156 | friend std::ostream & |
| 157 | operator << (std::ostream &os, const Hash &digest); |
| 158 | }; |
| 159 | |
| 160 | namespace Error { |
| 161 | struct HashConversion : virtual boost::exception, virtual std::exception { }; |
| 162 | } |
| 163 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 164 | |
| 165 | std::ostream & |
| 166 | operator << (std::ostream &os, const Hash &digest); |
| 167 | |
| 168 | #endif // HASH_STRING_CONVERTER_H |