blob: 33c0e775d40d899a9e6377479a188c39daff2242 [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>
36
37using namespace boost;
38using namespace boost::archive::iterators;
39using namespace std;
40
41
42template<class CharType>
43struct hex_from_4_bit
44{
45 typedef CharType result_type;
46 CharType operator () (CharType ch) const
47 {
48 const char *lookup_table = "0123456789abcdef";
49 // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
50 BOOST_ASSERT (ch < 16);
51 return lookup_table[static_cast<size_t>(ch)];
52 }
53};
54
55typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>,
56 transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary;
57
58
59template<class CharType>
60struct hex_to_4_bit
61{
62 typedef CharType result_type;
63 CharType operator () (CharType ch) const
64 {
65 const signed char lookup_table [] = {
66 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
70 -1,10,11,12,13,14,15,-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,-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 };
75
76 // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n";
77 signed char value = -1;
78 if ((unsigned)ch < 128)
79 value = lookup_table [(unsigned)ch];
80 if (value == -1)
81 BOOST_THROW_EXCEPTION (Error::HashConversion () << errmsg_info_int ((int)ch));
82
83 return value;
84 }
85};
86
87typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary;
88
89
90std::ostream &
91operator << (std::ostream &os, const Hash &hash)
92{
93 if (hash.m_length == 0)
94 return os;
95
96 ostreambuf_iterator<char> out_it (os); // ostream iterator
97 // need to encode to base64
98 copy (string_from_binary (reinterpret_cast<const char*> (hash.m_buf)),
99 string_from_binary (reinterpret_cast<const char*> (hash.m_buf+hash.m_length)),
100 out_it);
101
102 return os;
103}
104
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800105HashPtr
106Hash::FromString (const std::string &hashInTextEncoding)
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800107{
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800108 HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
109
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800110 if (hashInTextEncoding.size () == 0)
111 {
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800112 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800113 }
114
115 if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2)
116 {
117 cerr << "Input hash is too long. Returning an empty hash" << endl;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800118 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800119 }
120
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800121 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800122
123 unsigned char *end = copy (string_to_binary (hashInTextEncoding.begin ()),
124 string_to_binary (hashInTextEncoding.end ()),
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800125 retval->m_buf);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800126
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800127 retval->m_length = end - retval->m_buf;
128
129 return retval;
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800130}
131
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800132HashPtr
133Hash::FromFileContent (const std::string &filename)
134{
135 HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
136 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
137
138 EVP_MD_CTX *hash_context = EVP_MD_CTX_create ();
139 EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0);
140
141 ifstream iff (filename.c_str (), std::ios::in | std::ios::binary);
142 while (iff.good ())
143 {
144 char buf[1024];
145 iff.read (buf, 1024);
146 EVP_DigestUpdate (hash_context, buf, iff.gcount ());
147 }
148
149 retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
150
151 EVP_DigestFinal_ex (hash_context,
152 retval->m_buf, &retval->m_length);
153
154 EVP_MD_CTX_destroy (hash_context);
155
156 return retval;
157}