blob: d92e6b0e3d5f6ac5f1e3622237a87c86a19d55a4 [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>
27#include <openssl/evp.h>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080028#include <fstream>
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080029
30typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
31typedef 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>
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));
83
84 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
Zhenkai Zhue851b952013-01-13 22:29:57 -0800106unsigned char Hash::_origin = 0;
107HashPtr Hash::Origin(new Hash(&Hash::_origin, sizeof(unsigned char)));
108
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800109HashPtr
110Hash::FromString (const std::string &hashInTextEncoding)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800111{
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800112 HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
113
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800114 if (hashInTextEncoding.size () == 0)
115 {
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800116 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800117 }
118
119 if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2)
120 {
121 cerr << "Input hash is too long. Returning an empty hash" << endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800122 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800123 }
124
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800125 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800126
127 unsigned char *end = copy (string_to_binary (hashInTextEncoding.begin ()),
128 string_to_binary (hashInTextEncoding.end ()),
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800129 retval->m_buf);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800130
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800131 retval->m_length = end - retval->m_buf;
132
133 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800134}
135
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800136HashPtr
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800137Hash::FromFileContent (const fs::path &filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800138{
139 HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
140 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
141
142 EVP_MD_CTX *hash_context = EVP_MD_CTX_create ();
143 EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0);
144
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800145 fs::ifstream iff (filename, std::ios::in | std::ios::binary);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800146 while (iff.good ())
147 {
148 char buf[1024];
149 iff.read (buf, 1024);
150 EVP_DigestUpdate (hash_context, buf, iff.gcount ());
151 }
152
153 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
154
155 EVP_DigestFinal_ex (hash_context,
156 retval->m_buf, &retval->m_length);
157
158 EVP_MD_CTX_destroy (hash_context);
159
160 return retval;
161}