Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 26 | #include <boost/assert.hpp> |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 27 | #include <boost/exception/errinfo_at_line.hpp> |
| 28 | |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame^] | 29 | // 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 Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 34 | using namespace boost; |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame^] | 35 | using namespace std; |
| 36 | |
| 37 | using namespace boost::archive::iterators; |
| 38 | typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > base64_t; |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 40 | namespace Sync { |
| 41 | |
| 42 | Digest::Digest () |
| 43 | : m_buffer (0) |
| 44 | , m_hashLength (0) |
| 45 | { |
| 46 | m_context = EVP_MD_CTX_create (); |
| 47 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 48 | reset (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | Digest::~Digest () |
| 52 | { |
| 53 | if (m_buffer != 0) |
| 54 | delete [] m_buffer; |
| 55 | |
| 56 | EVP_MD_CTX_destroy (m_context); |
| 57 | } |
| 58 | |
| 59 | void |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 60 | Digest::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 | |
| 74 | void |
| 75 | Digest::finalize () |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 76 | { |
| 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 Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 84 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | std::size_t |
| 88 | Digest::getHash () |
| 89 | { |
| 90 | if (m_buffer == 0) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 91 | finalize (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 93 | BOOST_ASSERT (sizeof (std::size_t) <= m_hashLength); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 94 | |
| 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 | |
| 100 | bool |
| 101 | Digest::operator == (Digest &digest) |
| 102 | { |
| 103 | if (m_buffer == 0) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 104 | finalize (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 105 | |
| 106 | if (digest.m_buffer == 0) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 107 | digest.finalize (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 109 | BOOST_ASSERT (m_hashLength == digest.m_hashLength); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 110 | |
| 111 | return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0; |
| 112 | } |
| 113 | |
| 114 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 115 | void |
| 116 | Digest::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 Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 128 | Digest & |
| 129 | Digest::operator << (const Digest &src) |
| 130 | { |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 131 | if (src.m_buffer == 0) |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 132 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 133 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 134 | update (src.m_buffer, src.m_hashLength); |
| 135 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 136 | return *this; |
| 137 | } |
| 138 | |
| 139 | |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame^] | 140 | std::ostream& |
| 141 | Digest::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 | |
| 154 | std::ostream & |
| 155 | operator << (std::ostream &os, const Digest &digest) |
| 156 | { |
| 157 | digest.print (os); |
| 158 | return os; |
| 159 | } |
| 160 | |
| 161 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 162 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 163 | |