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