blob: a49965934f658320f5ef222cbd7bf2137255d415 [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
26#include "ns3/assert.h"
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080027#include <boost/exception/errinfo_at_line.hpp>
28
29using namespace boost;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080030
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080031namespace Sync {
32
33Digest::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 Afanasyevdf718f52012-03-02 00:23:04 -080041 throw DigestCalculationError () << errinfo_at_line (__LINE__);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080042}
43
44Digest::~Digest ()
45{
46 if (m_buffer != 0)
47 delete [] m_buffer;
48
49 EVP_MD_CTX_destroy (m_context);
50}
51
52void
53Digest::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 Afanasyevdf718f52012-03-02 00:23:04 -080062 throw DigestCalculationError () << errinfo_at_line (__LINE__);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080063}
64
65std::size_t
66Digest::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
78bool
79Digest::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 Afanasyevdf718f52012-03-02 00:23:04 -080093Digest &
94Digest::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 Afanasyev8f25cbb2012-03-01 23:53:40 -0800107} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800108