blob: 1ee08c1901fe1e56fac0a1e332cd2094486f47bc [file] [log] [blame]
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -08001/* -*- 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 Afanasyeva199f972013-01-02 19:37:26 -080022#include "hash-helper.h"
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080023
24#include <boost/assert.hpp>
25#include <boost/throw_exception.hpp>
26#include <boost/make_shared.hpp>
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -080027#include <boost/lexical_cast.hpp>
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080028#include <openssl/evp.h>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080029#include <fstream>
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080030
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -080031typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
32typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080033
34#include <boost/archive/iterators/transform_width.hpp>
35#include <boost/iterator/transform_iterator.hpp>
36#include <boost/archive/iterators/dataflow_exception.hpp>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080037#include <boost/filesystem/fstream.hpp>
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080038
39using namespace boost;
40using namespace boost::archive::iterators;
41using namespace std;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080042namespace fs = boost::filesystem;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080043
44template<class CharType>
45struct 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
57typedef 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
61template<class CharType>
62struct 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 Afanasyevfcf81dc2013-01-25 20:36:58 -080084
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080085 return value;
86 }
87};
88
89typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary;
90
91
92std::ostream &
93operator << (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 Afanasyevfcf81dc2013-01-25 20:36:58 -0800107std::string
108Hash::shortHash () const
109{
110 return lexical_cast<string> (*this).substr (0, 10);
111}
112
113
Zhenkai Zhue851b952013-01-13 22:29:57 -0800114unsigned char Hash::_origin = 0;
115HashPtr Hash::Origin(new Hash(&Hash::_origin, sizeof(unsigned char)));
116
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800117HashPtr
118Hash::FromString (const std::string &hashInTextEncoding)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800119{
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800120 HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800121
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800122 if (hashInTextEncoding.size () == 0)
123 {
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800124 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800125 }
126
127 if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2)
128 {
129 cerr << "Input hash is too long. Returning an empty hash" << endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800130 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800131 }
132
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800133 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
Alexander Afanasyevfcf81dc2013-01-25 20:36:58 -0800134
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800135 unsigned char *end = copy (string_to_binary (hashInTextEncoding.begin ()),
136 string_to_binary (hashInTextEncoding.end ()),
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800137 retval->m_buf);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800138
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800139 retval->m_length = end - retval->m_buf;
140
141 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800142}
143
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800144HashPtr
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800145Hash::FromFileContent (const fs::path &filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800146{
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 Afanasyev68f2a952013-01-08 14:34:16 -0800153 fs::ifstream iff (filename, std::ios::in | std::ios::binary);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800154 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 Afanasyevfcf81dc2013-01-25 20:36:58 -0800165
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800166 EVP_MD_CTX_destroy (hash_context);
167
168 return retval;
169}
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700170
171HashPtr
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700172Hash::FromBytes (const Ndnx::Bytes &bytes)
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700173{
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
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700181 EVP_DigestUpdate(hash_context, Ndnx::head(bytes), bytes.size());
Zhenkai Zhu9dd9adc2013-03-13 16:12:09 -0700182
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}