Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 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 | #include "hash-helper.h" |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 23 | |
| 24 | #include <boost/assert.hpp> |
| 25 | #include <boost/throw_exception.hpp> |
| 26 | #include <boost/make_shared.hpp> |
| 27 | #include <openssl/evp.h> |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 28 | #include <fstream> |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 29 | |
| 30 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 31 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
| 32 | |
| 33 | #include <boost/archive/iterators/transform_width.hpp> |
| 34 | #include <boost/iterator/transform_iterator.hpp> |
| 35 | #include <boost/archive/iterators/dataflow_exception.hpp> |
| 36 | |
| 37 | using namespace boost; |
| 38 | using namespace boost::archive::iterators; |
| 39 | using namespace std; |
| 40 | |
| 41 | |
| 42 | template<class CharType> |
| 43 | struct hex_from_4_bit |
| 44 | { |
| 45 | typedef CharType result_type; |
| 46 | CharType operator () (CharType ch) const |
| 47 | { |
| 48 | const char *lookup_table = "0123456789abcdef"; |
| 49 | // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n"; |
| 50 | BOOST_ASSERT (ch < 16); |
| 51 | return lookup_table[static_cast<size_t>(ch)]; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>, |
| 56 | transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary; |
| 57 | |
| 58 | |
| 59 | template<class CharType> |
| 60 | struct hex_to_4_bit |
| 61 | { |
| 62 | typedef CharType result_type; |
| 63 | CharType operator () (CharType ch) const |
| 64 | { |
| 65 | const signed char lookup_table [] = { |
| 66 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 67 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 68 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 69 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, |
| 70 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 71 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 72 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 73 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
| 74 | }; |
| 75 | |
| 76 | // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n"; |
| 77 | signed char value = -1; |
| 78 | if ((unsigned)ch < 128) |
| 79 | value = lookup_table [(unsigned)ch]; |
| 80 | if (value == -1) |
| 81 | BOOST_THROW_EXCEPTION (Error::HashConversion () << errmsg_info_int ((int)ch)); |
| 82 | |
| 83 | return value; |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary; |
| 88 | |
| 89 | |
| 90 | std::ostream & |
| 91 | operator << (std::ostream &os, const Hash &hash) |
| 92 | { |
| 93 | if (hash.m_length == 0) |
| 94 | return os; |
| 95 | |
| 96 | ostreambuf_iterator<char> out_it (os); // ostream iterator |
| 97 | // need to encode to base64 |
| 98 | copy (string_from_binary (reinterpret_cast<const char*> (hash.m_buf)), |
| 99 | string_from_binary (reinterpret_cast<const char*> (hash.m_buf+hash.m_length)), |
| 100 | out_it); |
| 101 | |
| 102 | return os; |
| 103 | } |
| 104 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 105 | HashPtr |
| 106 | Hash::FromString (const std::string &hashInTextEncoding) |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 107 | { |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 108 | HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0); |
| 109 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 110 | if (hashInTextEncoding.size () == 0) |
| 111 | { |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 112 | return retval; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2) |
| 116 | { |
| 117 | cerr << "Input hash is too long. Returning an empty hash" << endl; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 118 | return retval; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame^] | 121 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 122 | |
| 123 | unsigned char *end = copy (string_to_binary (hashInTextEncoding.begin ()), |
| 124 | string_to_binary (hashInTextEncoding.end ()), |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 125 | retval->m_buf); |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 127 | retval->m_length = end - retval->m_buf; |
| 128 | |
| 129 | return retval; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 132 | HashPtr |
| 133 | Hash::FromFileContent (const std::string &filename) |
| 134 | { |
| 135 | HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0); |
| 136 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 137 | |
| 138 | EVP_MD_CTX *hash_context = EVP_MD_CTX_create (); |
| 139 | EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0); |
| 140 | |
| 141 | ifstream iff (filename.c_str (), std::ios::in | std::ios::binary); |
| 142 | while (iff.good ()) |
| 143 | { |
| 144 | char buf[1024]; |
| 145 | iff.read (buf, 1024); |
| 146 | EVP_DigestUpdate (hash_context, buf, iff.gcount ()); |
| 147 | } |
| 148 | |
| 149 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 150 | |
| 151 | EVP_DigestFinal_ex (hash_context, |
| 152 | retval->m_buf, &retval->m_length); |
| 153 | |
| 154 | EVP_MD_CTX_destroy (hash_context); |
| 155 | |
| 156 | return retval; |
| 157 | } |