blob: cfd94e4705726e872cb5a985789fc928bcd52f09 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080019 */
20
Alexander Afanasyeva199f972013-01-02 19:37:26 -080021#include "hash-helper.h"
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080022
23#include <boost/assert.hpp>
24#include <boost/throw_exception.hpp>
25#include <boost/make_shared.hpp>
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -080026#include <boost/lexical_cast.hpp>
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080027#include <openssl/evp.h>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080028#include <fstream>
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080029
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -080030typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
31typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080032
33#include <boost/archive/iterators/transform_width.hpp>
34#include <boost/iterator/transform_iterator.hpp>
35#include <boost/archive/iterators/dataflow_exception.hpp>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080036#include <boost/filesystem/fstream.hpp>
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080037
38using namespace boost;
39using namespace boost::archive::iterators;
40using namespace std;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080041namespace fs = boost::filesystem;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080042
43template<class CharType>
44struct hex_from_4_bit
45{
46 typedef CharType result_type;
47 CharType operator () (CharType ch) const
48 {
49 const char *lookup_table = "0123456789abcdef";
50 // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
51 BOOST_ASSERT (ch < 16);
52 return lookup_table[static_cast<size_t>(ch)];
53 }
54};
55
56typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>,
57 transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary;
58
59
60template<class CharType>
61struct hex_to_4_bit
62{
63 typedef CharType result_type;
64 CharType operator () (CharType ch) const
65 {
66 const signed char lookup_table [] = {
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 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
70 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
71 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
72 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
73 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
74 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
75 };
76
77 // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n";
78 signed char value = -1;
79 if ((unsigned)ch < 128)
80 value = lookup_table [(unsigned)ch];
81 if (value == -1)
82 BOOST_THROW_EXCEPTION (Error::HashConversion () << errmsg_info_int ((int)ch));
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -080083
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080084 return value;
85 }
86};
87
88typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary;
89
90
91std::ostream &
92operator << (std::ostream &os, const Hash &hash)
93{
94 if (hash.m_length == 0)
95 return os;
96
97 ostreambuf_iterator<char> out_it (os); // ostream iterator
98 // need to encode to base64
99 copy (string_from_binary (reinterpret_cast<const char*> (hash.m_buf)),
100 string_from_binary (reinterpret_cast<const char*> (hash.m_buf+hash.m_length)),
101 out_it);
102
103 return os;
104}
105
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800106std::string
107Hash::shortHash () const
108{
109 return lexical_cast<string> (*this).substr (0, 10);
110}
111
112
Zhenkai Zhue851b952013-01-13 22:29:57 -0800113unsigned char Hash::_origin = 0;
114HashPtr Hash::Origin(new Hash(&Hash::_origin, sizeof(unsigned char)));
115
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800116HashPtr
117Hash::FromString (const std::string &hashInTextEncoding)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800118{
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800119 HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800120
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800121 if (hashInTextEncoding.size () == 0)
122 {
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800123 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800124 }
125
126 if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2)
127 {
128 cerr << "Input hash is too long. Returning an empty hash" << endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800129 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800130 }
131
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800132 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800133
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800134 unsigned char *end = copy (string_to_binary (hashInTextEncoding.begin ()),
135 string_to_binary (hashInTextEncoding.end ()),
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800136 retval->m_buf);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800137
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800138 retval->m_length = end - retval->m_buf;
139
140 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800141}
142
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800143HashPtr
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800144Hash::FromFileContent (const fs::path &filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800145{
146 HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
147 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
148
149 EVP_MD_CTX *hash_context = EVP_MD_CTX_create ();
150 EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0);
151
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800152 fs::ifstream iff (filename, std::ios::in | std::ios::binary);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800153 while (iff.good ())
154 {
155 char buf[1024];
156 iff.read (buf, 1024);
157 EVP_DigestUpdate (hash_context, buf, iff.gcount ());
158 }
159
160 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
161
162 EVP_DigestFinal_ex (hash_context,
163 retval->m_buf, &retval->m_length);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800164
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800165 EVP_MD_CTX_destroy (hash_context);
166
167 return retval;
168}
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700169
170HashPtr
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700171Hash::FromBytes (const Ndnx::Bytes &bytes)
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700172{
173 HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
174 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
175
176 EVP_MD_CTX *hash_context = EVP_MD_CTX_create ();
177 EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0);
178
179 // not sure whether it's bad to do so if bytes.size is huge
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700180 EVP_DigestUpdate(hash_context, Ndnx::head(bytes), bytes.size());
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700181
182 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
183
184 EVP_DigestFinal_ex (hash_context,
185 retval->m_buf, &retval->m_length);
186
187 EVP_MD_CTX_destroy (hash_context);
188
189 return retval;
190}