blob: a6f91f792e47b4c8ce14ab7fd47e4691e88d10ef [file] [log] [blame]
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -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: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include "sync-digest.h"
24#include <string.h>
25
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080026#include <boost/assert.hpp>
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080027#include <boost/exception/errinfo_at_line.hpp>
28
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080029// for printing, may be disabled in optimized build
30#include <boost/archive/iterators/base64_from_binary.hpp>
31#include <boost/archive/iterators/base64_from_binary.hpp>
32#include <boost/archive/iterators/transform_width.hpp>
33
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080034using namespace boost;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080035using namespace std;
36
37using namespace boost::archive::iterators;
38typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > base64_t;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080039
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080040namespace Sync {
41
42Digest::Digest ()
43 : m_buffer (0)
44 , m_hashLength (0)
45{
46 m_context = EVP_MD_CTX_create ();
47
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080048 reset ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080049}
50
51Digest::~Digest ()
52{
53 if (m_buffer != 0)
54 delete [] m_buffer;
55
56 EVP_MD_CTX_destroy (m_context);
57}
58
59void
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080060Digest::reset ()
61{
62 if (m_buffer != 0)
63 {
64 delete [] m_buffer;
65 m_buffer = 0;
66 }
67
68 int ok = EVP_DigestInit_ex (m_context, EVP_sha1 (), 0);
69 if (!ok)
70 throw DigestCalculationError () << errinfo_at_line (__LINE__);
71}
72
73
74void
75Digest::finalize ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080076{
77 if (m_buffer != 0) return;
78
79 m_buffer = new uint8_t [HASH_SIZE];
80
81 int ok = EVP_DigestFinal_ex (m_context,
82 m_buffer, &m_hashLength);
83 if (!ok)
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080084 throw DigestCalculationError () << errinfo_at_line (__LINE__);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080085}
86
87std::size_t
88Digest::getHash ()
89{
90 if (m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080091 finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080092
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080093 BOOST_ASSERT (sizeof (std::size_t) <= m_hashLength);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080094
95 // just getting first sizeof(std::size_t) bytes
96 // not ideal, but should work pretty well
97 return reinterpret_cast<std::size_t> (m_buffer);
98}
99
100bool
101Digest::operator == (Digest &digest)
102{
103 if (m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800104 finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800105
106 if (digest.m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800107 digest.finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800108
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800109 BOOST_ASSERT (m_hashLength == digest.m_hashLength);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800110
111 return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0;
112}
113
114
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800115void
116Digest::update (const uint8_t *buffer, size_t size)
117{
118 // cannot update Digest when it has been finalized
119 if (m_buffer != 0)
120 throw DigestCalculationError () << errinfo_at_line (__LINE__);
121
122 bool ok = EVP_DigestUpdate (m_context, buffer, size);
123 if (!ok)
124 throw DigestCalculationError () << errinfo_at_line (__LINE__);
125}
126
127
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800128Digest &
129Digest::operator << (const Digest &src)
130{
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800131 if (src.m_buffer == 0)
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800132 throw DigestCalculationError () << errinfo_at_line (__LINE__);
133
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800134 update (src.m_buffer, src.m_hashLength);
135
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800136 return *this;
137}
138
139
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800140std::ostream&
141Digest::print (std::ostream &os) const
142{
143 BOOST_ASSERT (m_hashLength != 0);
144
145 ostreambuf_iterator<char> out_it (os); // ostream iterator
146 // need to encode to base64
147 copy (base64_t (reinterpret_cast<const char*> (m_buffer)),
148 base64_t (reinterpret_cast<const char*> (m_buffer+m_hashLength)),
149 out_it);
150
151 return os;
152}
153
154std::ostream &
155operator << (std::ostream &os, const Digest &digest)
156{
157 digest.print (os);
158 return os;
159}
160
161
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800162} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800163