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> |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 28 | #include <openssl/evp.h> |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 29 | #include <fstream> |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 31 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 32 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 33 | |
| 34 | #include <boost/archive/iterators/transform_width.hpp> |
| 35 | #include <boost/iterator/transform_iterator.hpp> |
| 36 | #include <boost/archive/iterators/dataflow_exception.hpp> |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 37 | #include <boost/filesystem/fstream.hpp> |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 38 | |
| 39 | using namespace boost; |
| 40 | using namespace boost::archive::iterators; |
| 41 | using namespace std; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 42 | namespace fs = boost::filesystem; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 43 | |
| 44 | template<class CharType> |
| 45 | struct hex_from_4_bit |
| 46 | { |
| 47 | typedef CharType result_type; |
| 48 | CharType operator () (CharType ch) const |
| 49 | { |
| 50 | const char *lookup_table = "0123456789abcdef"; |
| 51 | // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n"; |
| 52 | BOOST_ASSERT (ch < 16); |
| 53 | return lookup_table[static_cast<size_t>(ch)]; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>, |
| 58 | transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary; |
| 59 | |
| 60 | |
| 61 | template<class CharType> |
| 62 | struct hex_to_4_bit |
| 63 | { |
| 64 | typedef CharType result_type; |
| 65 | CharType operator () (CharType ch) const |
| 66 | { |
| 67 | const signed char lookup_table [] = { |
| 68 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 69 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 70 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 71 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-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 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 75 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
| 76 | }; |
| 77 | |
| 78 | // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n"; |
| 79 | signed char value = -1; |
| 80 | if ((unsigned)ch < 128) |
| 81 | value = lookup_table [(unsigned)ch]; |
| 82 | if (value == -1) |
| 83 | BOOST_THROW_EXCEPTION (Error::HashConversion () << errmsg_info_int ((int)ch)); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 85 | return value; |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary; |
| 90 | |
| 91 | |
| 92 | std::ostream & |
| 93 | operator << (std::ostream &os, const Hash &hash) |
| 94 | { |
| 95 | if (hash.m_length == 0) |
| 96 | return os; |
| 97 | |
| 98 | ostreambuf_iterator<char> out_it (os); // ostream iterator |
| 99 | // need to encode to base64 |
| 100 | copy (string_from_binary (reinterpret_cast<const char*> (hash.m_buf)), |
| 101 | string_from_binary (reinterpret_cast<const char*> (hash.m_buf+hash.m_length)), |
| 102 | out_it); |
| 103 | |
| 104 | return os; |
| 105 | } |
| 106 | |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 107 | std::string |
| 108 | Hash::shortHash () const |
| 109 | { |
| 110 | return lexical_cast<string> (*this).substr (0, 10); |
| 111 | } |
| 112 | |
| 113 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 114 | unsigned char Hash::_origin = 0; |
| 115 | HashPtr Hash::Origin(new Hash(&Hash::_origin, sizeof(unsigned char))); |
| 116 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 117 | HashPtr |
| 118 | Hash::FromString (const std::string &hashInTextEncoding) |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 119 | { |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 120 | HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 122 | if (hashInTextEncoding.size () == 0) |
| 123 | { |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 124 | return retval; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2) |
| 128 | { |
| 129 | cerr << "Input hash is too long. Returning an empty hash" << endl; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 130 | return retval; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 133 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 135 | unsigned char *end = copy (string_to_binary (hashInTextEncoding.begin ()), |
| 136 | string_to_binary (hashInTextEncoding.end ()), |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 137 | retval->m_buf); |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 139 | retval->m_length = end - retval->m_buf; |
| 140 | |
| 141 | return retval; |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 144 | HashPtr |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 145 | Hash::FromFileContent (const fs::path &filename) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 146 | { |
| 147 | HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0); |
| 148 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 149 | |
| 150 | EVP_MD_CTX *hash_context = EVP_MD_CTX_create (); |
| 151 | EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0); |
| 152 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 153 | fs::ifstream iff (filename, std::ios::in | std::ios::binary); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 154 | while (iff.good ()) |
| 155 | { |
| 156 | char buf[1024]; |
| 157 | iff.read (buf, 1024); |
| 158 | EVP_DigestUpdate (hash_context, buf, iff.gcount ()); |
| 159 | } |
| 160 | |
| 161 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 162 | |
| 163 | EVP_DigestFinal_ex (hash_context, |
| 164 | retval->m_buf, &retval->m_length); |
Alexander Afanasyev | fcf81dc | 2013-01-25 20:36:58 -0800 | [diff] [blame] | 165 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 166 | EVP_MD_CTX_destroy (hash_context); |
| 167 | |
| 168 | return retval; |
| 169 | } |
Zhenkai Zhu | 9dd9adc | 2013-03-13 16:12:09 -0700 | [diff] [blame] | 170 | |
| 171 | HashPtr |
| 172 | Hash::FromBytes (const Ccnx::Bytes &bytes) |
| 173 | { |
| 174 | HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0); |
| 175 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 176 | |
| 177 | EVP_MD_CTX *hash_context = EVP_MD_CTX_create (); |
| 178 | EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0); |
| 179 | |
| 180 | // not sure whether it's bad to do so if bytes.size is huge |
| 181 | EVP_DigestUpdate(hash_context, Ccnx::head(bytes), bytes.size()); |
| 182 | |
| 183 | retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE]; |
| 184 | |
| 185 | EVP_DigestFinal_ex (hash_context, |
| 186 | retval->m_buf, &retval->m_length); |
| 187 | |
| 188 | EVP_MD_CTX_destroy (hash_context); |
| 189 | |
| 190 | return retval; |
| 191 | } |