blob: 6ffdbcd343884b9cdadb504e273c70848ebb6a83 [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>
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -080024#include <boost/lexical_cast.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080025#include <boost/make_shared.hpp>
26#include <boost/throw_exception.hpp>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080027#include <fstream>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080028#include <openssl/evp.h>
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
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080033#include <boost/archive/iterators/dataflow_exception.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080034#include <boost/archive/iterators/transform_width.hpp>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080035#include <boost/filesystem/fstream.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080036#include <boost/iterator/transform_iterator.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
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080043template <class CharType>
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080044struct hex_from_4_bit
45{
46 typedef CharType result_type;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047 CharType
48 operator()(CharType ch) const
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080049 {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080050 const char* lookup_table = "0123456789abcdef";
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080051 // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080052 BOOST_ASSERT(ch < 16);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080053 return lookup_table[static_cast<size_t>(ch)];
54 }
55};
56
57typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>,
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type>>
59 string_from_binary;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080060
61
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080062template <class CharType>
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080063struct hex_to_4_bit
64{
65 typedef CharType result_type;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080066 CharType
67 operator()(CharType ch) const
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080068 {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080069 const signed char lookup_table[] = {-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,
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1,
73 -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1,
76 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
77 -1, -1, -1, -1, -1, -1, -1, -1};
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080078
79 // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n";
80 signed char value = -1;
81 if ((unsigned)ch < 128)
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080082 value = lookup_table[(unsigned)ch];
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080083 if (value == -1)
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080084 BOOST_THROW_EXCEPTION(Error::HashConversion() << errmsg_info_int((int)ch));
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -080085
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080086 return value;
87 }
88};
89
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080090typedef transform_width<
91 transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>,
92 8,
93 4>
94 string_to_binary;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080095
96
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097std::ostream&
98operator<<(std::ostream& os, const Hash& hash)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080099{
100 if (hash.m_length == 0)
101 return os;
102
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 ostreambuf_iterator<char> out_it(os); // ostream iterator
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800104 // need to encode to base64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800105 copy(string_from_binary(reinterpret_cast<const char*>(hash.m_buf)),
106 string_from_binary(reinterpret_cast<const char*>(hash.m_buf + hash.m_length)),
107 out_it);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800108
109 return os;
110}
111
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800112std::string
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800113Hash::shortHash() const
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800114{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800115 return lexical_cast<string>(*this).substr(0, 10);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800116}
117
118
Zhenkai Zhue851b952013-01-13 22:29:57 -0800119unsigned char Hash::_origin = 0;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800120HashPtr
121Hash::Origin(new Hash(&Hash::_origin, sizeof(unsigned char)));
Zhenkai Zhue851b952013-01-13 22:29:57 -0800122
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800123HashPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800124Hash::FromString(const std::string& hashInTextEncoding)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800125{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800126 HashPtr retval = make_shared<Hash>(reinterpret_cast<void*>(0), 0);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800127
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800128 if (hashInTextEncoding.size() == 0) {
129 return retval;
130 }
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800131
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800132 if (hashInTextEncoding.size() > EVP_MAX_MD_SIZE * 2) {
133 cerr << "Input hash is too long. Returning an empty hash" << endl;
134 return retval;
135 }
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800136
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800137 retval->m_buf = new unsigned char[EVP_MAX_MD_SIZE];
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800138
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800139 unsigned char* end = copy(string_to_binary(hashInTextEncoding.begin()),
140 string_to_binary(hashInTextEncoding.end()),
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800141 retval->m_buf);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800142
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800143 retval->m_length = end - retval->m_buf;
144
145 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800146}
147
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800148HashPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800149Hash::FromFileContent(const fs::path& filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800150{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800151 HashPtr retval = make_shared<Hash>(reinterpret_cast<void*>(0), 0);
152 retval->m_buf = new unsigned char[EVP_MAX_MD_SIZE];
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800153
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800154 EVP_MD_CTX* hash_context = EVP_MD_CTX_create();
155 EVP_DigestInit_ex(hash_context, HASH_FUNCTION(), 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800156
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800157 fs::ifstream iff(filename, std::ios::in | std::ios::binary);
158 while (iff.good()) {
159 char buf[1024];
160 iff.read(buf, 1024);
161 EVP_DigestUpdate(hash_context, buf, iff.gcount());
162 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800163
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800164 retval->m_buf = new unsigned char[EVP_MAX_MD_SIZE];
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800165
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800166 EVP_DigestFinal_ex(hash_context, retval->m_buf, &retval->m_length);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800167
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800168 EVP_MD_CTX_destroy(hash_context);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800169
170 return retval;
171}
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700172
173HashPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800174Hash::FromBytes(const Ccnx::Bytes& bytes)
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700175{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800176 HashPtr retval = make_shared<Hash>(reinterpret_cast<void*>(0), 0);
177 retval->m_buf = new unsigned char[EVP_MAX_MD_SIZE];
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700178
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800179 EVP_MD_CTX* hash_context = EVP_MD_CTX_create();
180 EVP_DigestInit_ex(hash_context, HASH_FUNCTION(), 0);
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700181
182 // not sure whether it's bad to do so if bytes.size is huge
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700183 EVP_DigestUpdate(hash_context, Ndnx::head(bytes), bytes.size());
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700184
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800185 retval->m_buf = new unsigned char[EVP_MAX_MD_SIZE];
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700186
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800187 EVP_DigestFinal_ex(hash_context, retval->m_buf, &retval->m_length);
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700188
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800189 EVP_MD_CTX_destroy(hash_context);
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700190
191 return retval;
192}