db-helper: Switch code to use ndn-cxx

Changes include:
- move to ndn::chronoshare namespace
- switch to ndn::util::Digest for hash computation
diff --git a/src/db-helper.cpp b/src/db-helper.cpp
index e677a75..8adf5a0 100644
--- a/src/db-helper.cpp
+++ b/src/db-helper.cpp
@@ -18,18 +18,20 @@
  * See AUTHORS.md for complete list of ChronoShare authors and contributors.
  */
 
-#include "db-helper.h"
-#include "logging.h"
+#include "db-helper.hpp"
+#include "core/logging.hpp"
 
-#include <boost/make_shared.hpp>
-#include <boost/ref.hpp>
-#include <boost/throw_exception.hpp>
+#include <ndn-cxx/util/digest.hpp>
+
+namespace ndn {
+namespace chronoshare {
 
 INIT_LOGGER("DbHelper");
 
-using namespace boost;
 namespace fs = boost::filesystem;
 
+using util::Sha256;
+
 const std::string INIT_DATABASE = "\
     PRAGMA foreign_keys = ON;      \
 ";
@@ -40,32 +42,30 @@
 
   int res = sqlite3_open((path / dbname).c_str(), &m_db);
   if (res != SQLITE_OK) {
-    BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot open/create dabatabase: [" +
-                                                         (path / dbname).string() + "]"));
+    BOOST_THROW_EXCEPTION(Error("Cannot open/create database: [" + (path / dbname).string() + "]"));
   }
 
   res = sqlite3_create_function(m_db, "hash", 2, SQLITE_ANY, 0, 0, DbHelper::hash_xStep,
                                 DbHelper::hash_xFinal);
   if (res != SQLITE_OK) {
-    BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot create function ``hash''"));
+    BOOST_THROW_EXCEPTION(Error("Cannot create function ``hash''"));
   }
 
   res = sqlite3_create_function(m_db, "is_prefix", 2, SQLITE_ANY, 0, DbHelper::is_prefix_xFun, 0, 0);
   if (res != SQLITE_OK) {
-    BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot create function ``is_prefix''"));
+    BOOST_THROW_EXCEPTION(Error("Cannot create function ``is_prefix''"));
   }
 
   res = sqlite3_create_function(m_db, "directory_name", -1, SQLITE_ANY, 0,
                                 DbHelper::directory_name_xFun, 0, 0);
   if (res != SQLITE_OK) {
-    BOOST_THROW_EXCEPTION(Error::Db()
-                          << errmsg_info_str("Cannot create function ``directory_name''"));
+    BOOST_THROW_EXCEPTION(Error("Cannot create function ``directory_name''"));
   }
 
   res = sqlite3_create_function(m_db, "is_dir_prefix", 2, SQLITE_ANY, 0,
                                 DbHelper::is_dir_prefix_xFun, 0, 0);
   if (res != SQLITE_OK) {
-    BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot create function ``is_dir_prefix''"));
+    BOOST_THROW_EXCEPTION(Error("Cannot create function ``is_dir_prefix''"));
   }
 
   sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, NULL);
@@ -84,66 +84,56 @@
 DbHelper::hash_xStep(sqlite3_context* context, int argc, sqlite3_value** argv)
 {
   if (argc != 2) {
-    // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function");
+    // _LOG_ERROR("Wrong arguments are supplied for ``hash'' function");
     sqlite3_result_error(context, "Wrong arguments are supplied for ``hash'' function", -1);
     return;
   }
+
   if (sqlite3_value_type(argv[0]) != SQLITE_BLOB || sqlite3_value_type(argv[1]) != SQLITE_INTEGER) {
-    // _LOG_ERROR ("Hash expects (blob,integer) parameters");
-    sqlite3_result_error(context, "Hash expects (blob,integer) parameters", -1);
+    // _LOG_ERROR("Hash expects(blob,integer) parameters");
+    sqlite3_result_error(context, "Hash expects(blob,integer) parameters", -1);
     return;
   }
 
-  EVP_MD_CTX** hash_context =
-    reinterpret_cast<EVP_MD_CTX**>(sqlite3_aggregate_context(context, sizeof(EVP_MD_CTX*)));
+  Sha256** digest = reinterpret_cast<Sha256**>(sqlite3_aggregate_context(context, sizeof(Sha256*)));
 
-  if (hash_context == 0) {
+  if (digest == nullptr) {
     sqlite3_result_error_nomem(context);
     return;
   }
 
-  if (*hash_context == 0) {
-    *hash_context = EVP_MD_CTX_create();
-    EVP_DigestInit_ex(*hash_context, HASH_FUNCTION(), 0);
+  if (*digest == nullptr) {
+    *digest = new Sha256();
   }
 
   int nameBytes = sqlite3_value_bytes(argv[0]);
   const void* name = sqlite3_value_blob(argv[0]);
   sqlite3_int64 seqno = sqlite3_value_int64(argv[1]);
 
-  EVP_DigestUpdate(*hash_context, name, nameBytes);
-  EVP_DigestUpdate(*hash_context, &seqno, sizeof(sqlite3_int64));
+  (*digest)->update(reinterpret_cast<const uint8_t*>(name), nameBytes);
+  (*digest)->update(reinterpret_cast<const uint8_t*>(&seqno), sizeof(sqlite3_int64));
 }
 
 void
 DbHelper::hash_xFinal(sqlite3_context* context)
 {
-  EVP_MD_CTX** hash_context =
-    reinterpret_cast<EVP_MD_CTX**>(sqlite3_aggregate_context(context, sizeof(EVP_MD_CTX*)));
+  Sha256** digest = reinterpret_cast<Sha256**>(sqlite3_aggregate_context(context, sizeof(Sha256*)));
 
-  if (hash_context == 0) {
+  if (digest == nullptr) {
     sqlite3_result_error_nomem(context);
     return;
   }
 
-  if (*hash_context == 0) // no rows
-  {
+  if (*digest == nullptr) {
     char charNullResult = 0;
-    sqlite3_result_blob(context, &charNullResult, 1,
-                        SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
+    sqlite3_result_blob(context, &charNullResult, 1, SQLITE_TRANSIENT);
     return;
   }
 
-  unsigned char* hash = new unsigned char[EVP_MAX_MD_SIZE];
-  unsigned int hashLength = 0;
+  shared_ptr<const Buffer> hash = (*digest)->computeDigest();
+  sqlite3_result_blob(context, hash->buf(), hash->size(), SQLITE_TRANSIENT);
 
-  int ok = EVP_DigestFinal_ex(*hash_context, hash, &hashLength);
-
-  sqlite3_result_blob(context, hash, hashLength,
-                      SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
-  delete[] hash;
-
-  EVP_MD_CTX_destroy(*hash_context);
+  delete *digest;
 }
 
 void
@@ -232,3 +222,6 @@
     sqlite3_result_int(context, 0);
   }
 }
+
+} // chronoshare
+} // ndn
diff --git a/src/db-helper.hpp b/src/db-helper.hpp
index 7e75892..a6604d2 100644
--- a/src/db-helper.hpp
+++ b/src/db-helper.hpp
@@ -18,22 +18,30 @@
  * See AUTHORS.md for complete list of ChronoShare authors and contributors.
  */
 
-#ifndef DB_HELPER_H
-#define DB_HELPER_H
+#ifndef CHRONOSHARE_SRC_DB_HELPER_HPP
+#define CHRONOSHARE_SRC_DB_HELPER_HPP
 
-#include "hash-helper.hpp"
-#include <boost/exception/all.hpp>
+#include "core/chronoshare-common.hpp"
+
 #include <boost/filesystem.hpp>
-#include <openssl/evp.h>
 #include <sqlite3.h>
-#include <stdint.h>
-#include <string>
 
-typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
+namespace ndn {
+namespace chronoshare {
 
 class DbHelper
 {
 public:
+  class Error : public std::runtime_error
+  {
+  public:
+    explicit Error(const std::string& what)
+      : std::runtime_error(what)
+    {
+    }
+  };
+
+public:
   DbHelper(const boost::filesystem::path& path, const std::string& dbname);
   virtual ~DbHelper();
 
@@ -57,13 +65,9 @@
   sqlite3* m_db;
 };
 
-namespace Error {
-struct Db : virtual boost::exception, virtual std::exception
-{
-};
-}
+typedef shared_ptr<DbHelper> DbHelperPtr;
 
-typedef boost::shared_ptr<DbHelper> DbHelperPtr;
+} // chronoshare
+} // ndn
 
-
-#endif // DB_HELPER_H
+#endif // CHRONOSHARE_SRC_DB_HELPER_HPP
diff --git a/src/hash-helper.cpp b/src/hash-helper.cpp
deleted file mode 100644
index 6ffdbcd..0000000
--- a/src/hash-helper.cpp
+++ /dev/null
@@ -1,192 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016, Regents of the University of California.
- *
- * This file is part of ChronoShare, a decentralized file sharing application over NDN.
- *
- * ChronoShare is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * ChronoShare 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 copies of the GNU General Public License along with
- * ChronoShare, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- *
- * See AUTHORS.md for complete list of ChronoShare authors and contributors.
- */
-
-#include "hash-helper.h"
-
-#include <boost/assert.hpp>
-#include <boost/lexical_cast.hpp>
-#include <boost/make_shared.hpp>
-#include <boost/throw_exception.hpp>
-#include <fstream>
-#include <openssl/evp.h>
-
-typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
-typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
-
-#include <boost/archive/iterators/dataflow_exception.hpp>
-#include <boost/archive/iterators/transform_width.hpp>
-#include <boost/filesystem/fstream.hpp>
-#include <boost/iterator/transform_iterator.hpp>
-
-using namespace boost;
-using namespace boost::archive::iterators;
-using namespace std;
-namespace fs = boost::filesystem;
-
-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)
-      BOOST_THROW_EXCEPTION(Error::HashConversion() << errmsg_info_int((int)ch));
-
-    return value;
-  }
-};
-
-typedef transform_width<
-  transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>,
-  8,
-  4>
-  string_to_binary;
-
-
-std::ostream&
-operator<<(std::ostream& os, const Hash& hash)
-{
-  if (hash.m_length == 0)
-    return os;
-
-  ostreambuf_iterator<char> out_it(os); // ostream iterator
-  // need to encode to base64
-  copy(string_from_binary(reinterpret_cast<const char*>(hash.m_buf)),
-       string_from_binary(reinterpret_cast<const char*>(hash.m_buf + hash.m_length)),
-       out_it);
-
-  return os;
-}
-
-std::string
-Hash::shortHash() const
-{
-  return lexical_cast<string>(*this).substr(0, 10);
-}
-
-
-unsigned char Hash::_origin = 0;
-HashPtr
-Hash::Origin(new Hash(&Hash::_origin, sizeof(unsigned char)));
-
-HashPtr
-Hash::FromString(const std::string& hashInTextEncoding)
-{
-  HashPtr retval = make_shared<Hash>(reinterpret_cast<void*>(0), 0);
-
-  if (hashInTextEncoding.size() == 0) {
-    return retval;
-  }
-
-  if (hashInTextEncoding.size() > EVP_MAX_MD_SIZE * 2) {
-    cerr << "Input hash is too long. Returning an empty hash" << endl;
-    return retval;
-  }
-
-  retval->m_buf = new unsigned char[EVP_MAX_MD_SIZE];
-
-  unsigned char* end = copy(string_to_binary(hashInTextEncoding.begin()),
-                            string_to_binary(hashInTextEncoding.end()),
-                            retval->m_buf);
-
-  retval->m_length = end - retval->m_buf;
-
-  return retval;
-}
-
-HashPtr
-Hash::FromFileContent(const fs::path& filename)
-{
-  HashPtr retval = make_shared<Hash>(reinterpret_cast<void*>(0), 0);
-  retval->m_buf = new unsigned char[EVP_MAX_MD_SIZE];
-
-  EVP_MD_CTX* hash_context = EVP_MD_CTX_create();
-  EVP_DigestInit_ex(hash_context, HASH_FUNCTION(), 0);
-
-  fs::ifstream iff(filename, std::ios::in | std::ios::binary);
-  while (iff.good()) {
-    char buf[1024];
-    iff.read(buf, 1024);
-    EVP_DigestUpdate(hash_context, buf, iff.gcount());
-  }
-
-  retval->m_buf = new unsigned char[EVP_MAX_MD_SIZE];
-
-  EVP_DigestFinal_ex(hash_context, retval->m_buf, &retval->m_length);
-
-  EVP_MD_CTX_destroy(hash_context);
-
-  return retval;
-}
-
-HashPtr
-Hash::FromBytes(const Ccnx::Bytes& bytes)
-{
-  HashPtr retval = make_shared<Hash>(reinterpret_cast<void*>(0), 0);
-  retval->m_buf = new unsigned char[EVP_MAX_MD_SIZE];
-
-  EVP_MD_CTX* hash_context = EVP_MD_CTX_create();
-  EVP_DigestInit_ex(hash_context, HASH_FUNCTION(), 0);
-
-  // not sure whether it's bad to do so if bytes.size is huge
-  EVP_DigestUpdate(hash_context, Ndnx::head(bytes), bytes.size());
-
-  retval->m_buf = new unsigned char[EVP_MAX_MD_SIZE];
-
-  EVP_DigestFinal_ex(hash_context, retval->m_buf, &retval->m_length);
-
-  EVP_MD_CTX_destroy(hash_context);
-
-  return retval;
-}
diff --git a/src/hash-helper.hpp b/src/hash-helper.hpp
deleted file mode 100644
index 7346e23..0000000
--- a/src/hash-helper.hpp
+++ /dev/null
@@ -1,166 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016, Regents of the University of California.
- *
- * This file is part of ChronoShare, a decentralized file sharing application over NDN.
- *
- * ChronoShare is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * ChronoShare 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 copies of the GNU General Public License along with
- * ChronoShare, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- *
- * See AUTHORS.md for complete list of ChronoShare authors and contributors.
- */
-
-#ifndef HASH_HELPER_H
-#define HASH_HELPER_H
-
-#include "ccnx-common.hpp"
-#include <boost/exception/all.hpp>
-#include <boost/filesystem.hpp>
-#include <boost/shared_ptr.hpp>
-#include <iostream>
-#include <string.h>
-
-// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
-#define HASH_FUNCTION EVP_sha256
-
-class Hash;
-typedef boost::shared_ptr<Hash> HashPtr;
-
-class Hash
-{
-public:
-  static unsigned char _origin;
-  static HashPtr Origin;
-
-  Hash()
-    : m_buf(0)
-    , m_length(0)
-  {
-  }
-
-  Hash(const void* buf, unsigned int length)
-    : m_length(length)
-  {
-    if (m_length != 0) {
-      m_buf = new unsigned char[length];
-      memcpy(m_buf, buf, length);
-    }
-  }
-
-  Hash(const Hash& otherHash)
-    : m_length(otherHash.m_length)
-  {
-    if (m_length != 0) {
-      m_buf = new unsigned char[m_length];
-      memcpy(m_buf, otherHash.m_buf, otherHash.m_length);
-    }
-  }
-
-  static HashPtr
-  FromString(const std::string& hashInTextEncoding);
-
-  static HashPtr
-  FromFileContent(const boost::filesystem::path& fileName);
-
-  static HashPtr
-  FromBytes(const Ccnx::Bytes& bytes);
-
-  ~Hash()
-  {
-    if (m_length != 0)
-      delete[] m_buf;
-  }
-
-  Hash&
-  operator=(const Hash& otherHash)
-  {
-    if (m_length != 0)
-      delete[] m_buf;
-
-    m_length = otherHash.m_length;
-    if (m_length != 0) {
-      m_buf = new unsigned char[m_length];
-      memcpy(m_buf, otherHash.m_buf, otherHash.m_length);
-    }
-    return *this;
-  }
-
-  bool
-  IsZero() const
-  {
-    return m_length == 0 || (m_length == 1 && m_buf[0] == 0);
-  }
-
-  bool
-  operator==(const Hash& otherHash) const
-  {
-    if (m_length != otherHash.m_length)
-      return false;
-
-    return memcmp(m_buf, otherHash.m_buf, m_length) == 0;
-  }
-
-  bool
-  operator<(const Hash& otherHash) const
-  {
-    if (m_length < otherHash.m_length)
-      return true;
-
-    if (m_length > otherHash.m_length)
-      return false;
-
-    for (unsigned int i = 0; i < m_length; i++) {
-      if (m_buf[i] < otherHash.m_buf[i])
-        return true;
-
-      if (m_buf[i] > otherHash.m_buf[i])
-        return false;
-
-      // if equal, continue
-    }
-
-    return false;
-  }
-
-  const void*
-  GetHash() const
-  {
-    return m_buf;
-  }
-
-  unsigned int
-  GetHashBytes() const
-  {
-    return m_length;
-  }
-
-  std::string
-  shortHash() const;
-
-private:
-  unsigned char* m_buf;
-  unsigned int m_length;
-
-  friend std::ostream&
-  operator<<(std::ostream& os, const Hash& digest);
-};
-
-namespace Error {
-struct HashConversion : virtual boost::exception, virtual std::exception
-{
-};
-}
-
-
-std::ostream&
-operator<<(std::ostream& os, const Hash& digest);
-
-#endif // HASH_STRING_CONVERTER_H