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