blob: 400461419407f249e69e71665bd2551239e347d0 [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
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080039 reset ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080040}
41
42Digest::~Digest ()
43{
44 if (m_buffer != 0)
45 delete [] m_buffer;
46
47 EVP_MD_CTX_destroy (m_context);
48}
49
50void
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080051Digest::reset ()
52{
53 if (m_buffer != 0)
54 {
55 delete [] m_buffer;
56 m_buffer = 0;
57 }
58
59 int ok = EVP_DigestInit_ex (m_context, EVP_sha1 (), 0);
60 if (!ok)
61 throw DigestCalculationError () << errinfo_at_line (__LINE__);
62}
63
64
65void
66Digest::finalize ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080067{
68 if (m_buffer != 0) return;
69
70 m_buffer = new uint8_t [HASH_SIZE];
71
72 int ok = EVP_DigestFinal_ex (m_context,
73 m_buffer, &m_hashLength);
74 if (!ok)
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080075 throw DigestCalculationError () << errinfo_at_line (__LINE__);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080076}
77
78std::size_t
79Digest::getHash ()
80{
81 if (m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080082 finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080083
84 NS_ASSERT (sizeof (std::size_t) <= m_hashLength);
85
86 // just getting first sizeof(std::size_t) bytes
87 // not ideal, but should work pretty well
88 return reinterpret_cast<std::size_t> (m_buffer);
89}
90
91bool
92Digest::operator == (Digest &digest)
93{
94 if (m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080095 finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080096
97 if (digest.m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -080098 digest.finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080099
100 NS_ASSERT (m_hashLength == digest.m_hashLength);
101
102 return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0;
103}
104
105
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800106void
107Digest::update (const uint8_t *buffer, size_t size)
108{
109 // cannot update Digest when it has been finalized
110 if (m_buffer != 0)
111 throw DigestCalculationError () << errinfo_at_line (__LINE__);
112
113 bool ok = EVP_DigestUpdate (m_context, buffer, size);
114 if (!ok)
115 throw DigestCalculationError () << errinfo_at_line (__LINE__);
116}
117
118
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800119Digest &
120Digest::operator << (const Digest &src)
121{
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800122 if (src.m_buffer == 0)
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800123 throw DigestCalculationError () << errinfo_at_line (__LINE__);
124
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800125 update (src.m_buffer, src.m_hashLength);
126
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800127 return *this;
128}
129
130
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800131} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800132