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 | |
| 26 | #include "ns3/assert.h" |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 27 | #include <boost/exception/errinfo_at_line.hpp> |
| 28 | |
| 29 | using namespace boost; |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 31 | namespace Sync { |
| 32 | |
| 33 | Digest::Digest () |
| 34 | : m_buffer (0) |
| 35 | , m_hashLength (0) |
| 36 | { |
| 37 | m_context = EVP_MD_CTX_create (); |
| 38 | |
| 39 | int ok = EVP_DigestInit_ex (m_context, EVP_sha1 (), 0); |
| 40 | if (!ok) |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 41 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | Digest::~Digest () |
| 45 | { |
| 46 | if (m_buffer != 0) |
| 47 | delete [] m_buffer; |
| 48 | |
| 49 | EVP_MD_CTX_destroy (m_context); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | Digest::Finalize () |
| 54 | { |
| 55 | if (m_buffer != 0) return; |
| 56 | |
| 57 | m_buffer = new uint8_t [HASH_SIZE]; |
| 58 | |
| 59 | int ok = EVP_DigestFinal_ex (m_context, |
| 60 | m_buffer, &m_hashLength); |
| 61 | if (!ok) |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 62 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | std::size_t |
| 66 | Digest::getHash () |
| 67 | { |
| 68 | if (m_buffer == 0) |
| 69 | Finalize (); |
| 70 | |
| 71 | NS_ASSERT (sizeof (std::size_t) <= m_hashLength); |
| 72 | |
| 73 | // just getting first sizeof(std::size_t) bytes |
| 74 | // not ideal, but should work pretty well |
| 75 | return reinterpret_cast<std::size_t> (m_buffer); |
| 76 | } |
| 77 | |
| 78 | bool |
| 79 | Digest::operator == (Digest &digest) |
| 80 | { |
| 81 | if (m_buffer == 0) |
| 82 | Finalize (); |
| 83 | |
| 84 | if (digest.m_buffer == 0) |
| 85 | digest.Finalize (); |
| 86 | |
| 87 | NS_ASSERT (m_hashLength == digest.m_hashLength); |
| 88 | |
| 89 | return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0; |
| 90 | } |
| 91 | |
| 92 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 93 | Digest & |
| 94 | Digest::operator << (const Digest &src) |
| 95 | { |
| 96 | if (src.m_buffer == 0) |
| 97 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 98 | |
| 99 | bool ok = EVP_DigestUpdate (m_context, src.m_buffer, src.m_hashLength); |
| 100 | if (!ok) |
| 101 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 102 | |
| 103 | return *this; |
| 104 | } |
| 105 | |
| 106 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 107 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 108 | |