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 | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 42 | Hash (const void *buf, unsigned int length) |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 43 | : m_length (length) |
| 44 | { |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 45 | if (m_length != 0) |
| 46 | { |
| 47 | m_buf = new unsigned char [length]; |
| 48 | memcpy (m_buf, buf, length); |
| 49 | } |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 52 | static HashPtr |
| 53 | FromString (const std::string &hashInTextEncoding); |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 55 | static HashPtr |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 56 | FromFileContent (const boost::filesystem::path &fileName); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 58 | ~Hash () |
| 59 | { |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 60 | if (m_length != 0) |
| 61 | delete [] m_buf; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool |
| 65 | IsZero () const |
| 66 | { |
| 67 | return m_length == 0 || |
| 68 | (m_length == 1 && m_buf[0] == 0); |
| 69 | } |
| 70 | |
| 71 | bool |
| 72 | operator == (const Hash &otherHash) const |
| 73 | { |
| 74 | if (m_length != otherHash.m_length) |
| 75 | return false; |
| 76 | |
| 77 | return memcmp (m_buf, otherHash.m_buf, m_length) == 0; |
| 78 | } |
| 79 | |
| 80 | const void * |
| 81 | GetHash () const |
| 82 | { |
| 83 | return m_buf; |
| 84 | } |
| 85 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 86 | unsigned int |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 87 | GetHashBytes () const |
| 88 | { |
| 89 | return m_length; |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | unsigned char *m_buf; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 94 | unsigned int m_length; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 96 | Hash (const Hash &) { } |
| 97 | Hash & operator = (const Hash &) { return *this; } |
| 98 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 99 | friend std::ostream & |
| 100 | operator << (std::ostream &os, const Hash &digest); |
| 101 | }; |
| 102 | |
| 103 | namespace Error { |
| 104 | struct HashConversion : virtual boost::exception, virtual std::exception { }; |
| 105 | } |
| 106 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 107 | |
| 108 | std::ostream & |
| 109 | operator << (std::ostream &os, const Hash &digest); |
| 110 | |
| 111 | #endif // HASH_STRING_CONVERTER_H |