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> |
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 () |
| 44 | : m_length(0) |
| 45 | , m_buf(0) |
| 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 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 75 | ~Hash () |
| 76 | { |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 77 | if (m_length != 0) |
| 78 | delete [] m_buf; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 79 | } |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 80 | |
| 81 | Hash & |
| 82 | operator = (const Hash &otherHash) |
| 83 | { |
| 84 | if (m_length != 0) |
| 85 | delete [] m_buf; |
| 86 | |
| 87 | m_length = otherHash.m_length; |
| 88 | if (m_length != 0) |
| 89 | { |
| 90 | m_buf = new unsigned char [m_length]; |
| 91 | memcpy (m_buf, otherHash.m_buf, otherHash.m_length); |
| 92 | } |
| 93 | return *this; |
| 94 | } |
| 95 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 96 | bool |
| 97 | IsZero () const |
| 98 | { |
| 99 | return m_length == 0 || |
| 100 | (m_length == 1 && m_buf[0] == 0); |
| 101 | } |
| 102 | |
| 103 | bool |
| 104 | operator == (const Hash &otherHash) const |
| 105 | { |
| 106 | if (m_length != otherHash.m_length) |
| 107 | return false; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 109 | return memcmp (m_buf, otherHash.m_buf, m_length) == 0; |
| 110 | } |
| 111 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 112 | bool operator < (const Hash &otherHash) const |
| 113 | { |
| 114 | if (m_length < otherHash.m_length) |
| 115 | return true; |
| 116 | |
| 117 | if (m_length > otherHash.m_length) |
| 118 | return false; |
| 119 | |
Alexander Afanasyev | d724581 | 2013-02-13 21:06:57 -0800 | [diff] [blame] | 120 | for (unsigned int i = 0; i < m_length; i++) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 121 | { |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 122 | if (m_buf [i] < otherHash.m_buf [i]) |
| 123 | return true; |
| 124 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 125 | if (m_buf [i] > otherHash.m_buf [i]) |
| 126 | return false; |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 127 | |
| 128 | // if equal, continue |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 131 | return false; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 134 | const void * |
| 135 | GetHash () const |
| 136 | { |
| 137 | return m_buf; |
| 138 | } |
| 139 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 140 | unsigned int |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 141 | GetHashBytes () const |
| 142 | { |
| 143 | return m_length; |
| 144 | } |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 146 | std::string |
| 147 | shortHash () const; |
| 148 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 149 | private: |
| 150 | unsigned char *m_buf; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 151 | unsigned int m_length; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 152 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 153 | friend std::ostream & |
| 154 | operator << (std::ostream &os, const Hash &digest); |
| 155 | }; |
| 156 | |
| 157 | namespace Error { |
| 158 | struct HashConversion : virtual boost::exception, virtual std::exception { }; |
| 159 | } |
| 160 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 161 | |
| 162 | std::ostream & |
| 163 | operator << (std::ostream &os, const Hash &digest); |
| 164 | |
| 165 | #endif // HASH_STRING_CONVERTER_H |