blob: 863431e5bbde8c431ce35d7f7209688b7d7d6169 [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
31namespace ns3 {
32namespace Sync {
33
34Digest::Digest ()
35 : m_buffer (0)
36 , m_hashLength (0)
37{
38 m_context = EVP_MD_CTX_create ();
39
40 int ok = EVP_DigestInit_ex (m_context, EVP_sha1 (), 0);
41 if (!ok)
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080042 throw DigestCalculationError () << errinfo_at_line (__LINE__);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080043}
44
45Digest::~Digest ()
46{
47 if (m_buffer != 0)
48 delete [] m_buffer;
49
50 EVP_MD_CTX_destroy (m_context);
51}
52
53void
54Digest::Finalize ()
55{
56 if (m_buffer != 0) return;
57
58 m_buffer = new uint8_t [HASH_SIZE];
59
60 int ok = EVP_DigestFinal_ex (m_context,
61 m_buffer, &m_hashLength);
62 if (!ok)
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080063 throw DigestCalculationError () << errinfo_at_line (__LINE__);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080064}
65
66std::size_t
67Digest::getHash ()
68{
69 if (m_buffer == 0)
70 Finalize ();
71
72 NS_ASSERT (sizeof (std::size_t) <= m_hashLength);
73
74 // just getting first sizeof(std::size_t) bytes
75 // not ideal, but should work pretty well
76 return reinterpret_cast<std::size_t> (m_buffer);
77}
78
79bool
80Digest::operator == (Digest &digest)
81{
82 if (m_buffer == 0)
83 Finalize ();
84
85 if (digest.m_buffer == 0)
86 digest.Finalize ();
87
88 NS_ASSERT (m_hashLength == digest.m_hashLength);
89
90 return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0;
91}
92
93
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080094Digest &
95Digest::operator << (const Digest &src)
96{
97 if (src.m_buffer == 0)
98 throw DigestCalculationError () << errinfo_at_line (__LINE__);
99
100 bool ok = EVP_DigestUpdate (m_context, src.m_buffer, src.m_hashLength);
101 if (!ok)
102 throw DigestCalculationError () << errinfo_at_line (__LINE__);
103
104 return *this;
105}
106
107
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800108} // Sync
109} // ns3
110