Debugging Digest object. Now it seems to work almost perfectly

This commit is contained in:
Alexander Afanasyev committed 2012-03-05 16:57:39 -08:00
1 parent 22f4c81b9a
commit 2fc2d6792d
6 files changed
+283 -32

No files matched your search

+96 -16
View File
@@ -27,15 +27,79 @@
#include <boost/exception/errinfo_at_line.hpp>
// for printing, may be disabled in optimized build
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
// #ifdef DIGEST_BASE64
// #include <boost/archive/iterators/base64_from_binary.hpp>
// #include <boost/archive/iterators/binary_from_base64.hpp>
// #endif
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/archive/iterators/dataflow_exception.hpp>
using namespace boost;
using namespace boost::archive::iterators;
using namespace std;
using namespace boost::archive::iterators;
typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > base64_t;
// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
#define HASH_FUNCTION EVP_sha1
// #ifndef DIGEST_BASE64
template<class CharType>
struct hex_from_4_bit
{
typedef CharType result_type;
CharType operator () (CharType ch) const
{
const char *lookup_table = "0123456789abcdef";
// cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
BOOST_ASSERT (ch < 16);
return lookup_table[static_cast<size_t>(ch)];
}
};
typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>,
transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary;
template<class CharType>
struct hex_to_4_bit
{
typedef CharType result_type;
CharType operator () (CharType ch) const
{
const signed char lookup_table [] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
};
// cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n";
signed char value = -1;
if ((unsigned)ch < 128)
value = lookup_table [(unsigned)ch];
if (value == -1)
throw Sync::DigestCalculationError () << errinfo_at_line (__LINE__);
return value;
}
};
typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary;
// #else
// typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > string_from_binary;
// typedef binary_from_base64<transform_width<string::const_iterator, 8, 6> > string_to_binary;
// #endif
namespace Sync {
@@ -65,7 +129,7 @@ Digest::reset ()
m_buffer = 0;
}
int ok = EVP_DigestInit_ex (m_context, EVP_sha1 (), 0);
int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
if (!ok)
throw DigestCalculationError () << errinfo_at_line (__LINE__);
}
@@ -76,7 +140,7 @@ Digest::finalize ()
{
if (m_buffer != 0) return;
m_buffer = new uint8_t [HASH_SIZE];
m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
int ok = EVP_DigestFinal_ex (m_context,
m_buffer, &m_hashLength);
@@ -115,6 +179,8 @@ Digest::operator == (Digest &digest)
void
Digest::update (const uint8_t *buffer, size_t size)
{
// cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
// cannot update Digest when it has been finalized
if (m_buffer != 0)
throw DigestCalculationError () << errinfo_at_line (__LINE__);
@@ -136,26 +202,40 @@ Digest::operator << (const Digest &src)
return *this;
}
std::ostream&
Digest::print (std::ostream &os) const
std::ostream &
operator << (std::ostream &os, const Digest &digest)
{
BOOST_ASSERT (m_hashLength != 0);
BOOST_ASSERT (digest.m_hashLength != 0);
ostreambuf_iterator<char> out_it (os); // ostream iterator
// need to encode to base64
copy (base64_t (reinterpret_cast<const char*> (m_buffer)),
base64_t (reinterpret_cast<const char*> (m_buffer+m_hashLength)),
copy (string_from_binary (reinterpret_cast<const char*> (digest.m_buffer)),
string_from_binary (reinterpret_cast<const char*> (digest.m_buffer+digest.m_hashLength)),
out_it);
return os;
}
std::ostream &
operator << (std::ostream &os, const Digest &digest)
std::istream &
operator >> (std::istream &is, Digest &digest)
{
digest.print (os);
return os;
string str;
is >> str; // read string first
// uint8_t padding = (3 - str.size () % 3) % 3;
// for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
// only empty digest object can be used for reading
if (digest.m_buffer != 0)
throw DigestCalculationError () << errinfo_at_line (__LINE__);
digest.m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
uint8_t *end = copy (string_to_binary (str.begin ()),
string_to_binary (str.end ()),
digest.m_buffer);
digest.m_hashLength = end - digest.m_buffer;
return is;
}
+9 -5
View File
@@ -29,8 +29,6 @@
namespace Sync {
const std::size_t HASH_SIZE = 160;
/**
* @ingroup sync
* @brief A simple wrapper for libcrypto hash functions
@@ -97,9 +95,6 @@ public:
// inline Digest &
// operator << (INT value);
std::ostream&
print (std::ostream &os) const;
private:
/**
* @brief Disabled copy operator
@@ -118,6 +113,12 @@ private:
*/
void
update (const uint8_t *buffer, size_t size);
friend std::ostream &
operator << (std::ostream &os, const Digest &digest);
friend std::istream &
operator >> (std::istream &is, Digest &digest);
private:
EVP_MD_CTX *m_context;
@@ -147,6 +148,9 @@ Digest::operator << (uint32_t value)
std::ostream &
operator << (std::ostream &os, const Digest &digest);
std::istream &
operator >> (std::istream &is, Digest &digest);
// template<class INT>
// Digest &
// Digest::operator << (INT value)
+68
View File
@@ -0,0 +1,68 @@
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2012 University of California, Los Angeles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
* 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
* Alexander Afanasyev <alexander.afanasyev@ucla.edu>
*/
#include "sync-std-name-info.h"
using namespace std;
namespace Sync {
NameInfoConstPtr
StdNameInfo::FindOrCreate (const std::string &key)
{
NameInfoPtr value = NameInfoPtr (new StdNameInfo (key));
pair<NameMap::iterator,bool> item =
m_names.insert (make_pair (key, value));
return item.first->second;
}
StdNameInfo::StdNameInfo (const std::string &name)
: m_name (name)
{
m_id = m_ids ++; // set ID for a newly inserted element
m_digest << name;
m_digest.getHash (); // finalize digest
}
string
StdNameInfo::toString () const
{
return m_name;
}
bool
StdNameInfo::operator == (const NameInfo &info) const
{
try
{
return m_name == dynamic_cast<const StdNameInfo&> (info).m_name;
}
catch (...)
{
return false;
}
}
} // Sync
+68
View File
@@ -0,0 +1,68 @@
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2012 University of California, Los Angeles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
* 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
* Alexander Afanasyev <alexander.afanasyev@ucla.edu>
*/
#ifndef SYNC_STD_NAME_INFO_H
#define SYNC_STD_NAME_INFO_H
#include "sync-name-info.h"
#include <string>
namespace Sync {
class StdNameInfo : public NameInfo
{
public:
/**
* @brief Lookup existing or create new NameInfo object
* @param name routable prefix
*/
static NameInfoConstPtr
FindOrCreate (const std::string &name);
virtual ~StdNameInfo () { };
// from NameInfo
virtual bool
operator == (const NameInfo &info) const;
virtual std::string
toString () const;
private:
// implementing a singleton pattern.
/**
* @brief Disabled default constructor. NameInfo object should be created through FindOrCreate static call.
*/
/**
* @brief Disabled default
*/
StdNameInfo () {}
StdNameInfo& operator = (const StdNameInfo &info) { return *this; }
StdNameInfo (const std::string &name);
std::string m_name;
};
} // Sync
#endif // SYNC_CCNX_NAME_INFO_H
+41 -11
View File
@@ -21,27 +21,30 @@
*/
#include "../model/sync-digest.h"
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <sstream>
using namespace Sync;
using namespace std;
using namespace boost;
int
main (int argc, char **argv)
{
Digest test;
test << "1";
test << "1\n";
try
{
cout << "Trying to print without explicit getHash() call: ";
cout << test;
cout << "Failed (should be asserted)\n";
}
catch (...)
{
cout << "OK (exception)\n";
}
// try
// {
// cout << "Trying to print without explicit getHash() call: ";
// cout << test;
// cout << "Failed (should be asserted)\n";
// }
// catch (...)
// {
// cout << "OK (exception)\n";
// }
// without explicit finalizing, Digest will not be complete and printing out will cause assert
test.getHash ();
@@ -65,5 +68,32 @@ main (int argc, char **argv)
{
cout << "OK (exception)\n";
}
Digest test2;
// #ifdef DIGEST_BASE64
// string testString = "sCYyTGkEsqnLS4jW1hyB0Q==";
// #else
string testString = lexical_cast<string> (test); //"b026324c6904b2a9cb4b88d6d61c81d1";
// #endif
cout << "Hash from string: " << testString << "\n";
istringstream is (testString);
is >> test2;
cout << "Result from hash: " << test2 << "\n";
cout << "Compare two hashes: " << (test == test2) << "\n";
Digest test3;
if (testString[0] != '1')
testString[0] = '1';
else
testString[0] = '2';
istringstream is2(testString);
is2 >> test3;
cout << "Hash from string: " << test3 << "\n";
cout << "Compare two hashes: " << (test == test3) << "\n";
return 0;
}
+1
View File
@@ -10,6 +10,7 @@ def configure(conf):
conf.check_cfg(atleast_pkgconfig_version='0.20')
conf.check_cfg(package='openssl', args=['--cflags', '--libs'], uselib_store='SSL')
conf.define ('STANDALONE', 1)
# conf.define ('DIGEST_BASE64', 1) # base64 is not working and probably will not work at all
conf.check_tool('boost')
conf.check_boost(lib='signals filesystem iostreams regex')