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