util: reimplement Sha256 using security::transform

Change-Id: I4d01638a19867f39380dd0bee922c58148741506
Refs: #3924
diff --git a/src/util/digest.cpp b/src/util/digest.cpp
index 68c53ce..516255f 100644
--- a/src/util/digest.cpp
+++ b/src/util/digest.cpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -21,7 +21,11 @@
 
 #include "digest.hpp"
 #include "string-helper.hpp"
-#include "security/detail/openssl.hpp"
+#include "../security/detail/openssl.hpp"
+#include "../security/transform/digest-filter.hpp"
+#include "../security/transform/stream-sink.hpp"
+#include "../security/transform/stream-source.hpp"
+#include "../security/v1/cryptopp.hpp"
 
 #include <sstream>
 
@@ -175,13 +179,118 @@
   return os;
 }
 
-template
-class Digest<CryptoPP::SHA256>;
 
-template
+////////////////////////////////////////
+
+
+Sha256::Sha256()
+{
+  reset();
+}
+
+Sha256::Sha256(std::istream& is)
+  : m_output(make_unique<OBufferStream>())
+  , m_isEmpty(false)
+  , m_isFinalized(true)
+{
+  namespace tr = security::transform;
+
+  tr::streamSource(is) >> tr::digestFilter(DigestAlgorithm::SHA256) >> tr::streamSink(*m_output);
+}
+
+void
+Sha256::reset()
+{
+  namespace tr = security::transform;
+
+  m_input = make_unique<tr::StepSource>();
+  m_output = make_unique<OBufferStream>();
+  m_isEmpty = true;
+  m_isFinalized = false;
+
+  *m_input >> tr::digestFilter(DigestAlgorithm::SHA256) >> tr::streamSink(*m_output);
+}
+
+ConstBufferPtr
+Sha256::computeDigest()
+{
+  if (!m_isFinalized) {
+    BOOST_ASSERT(m_input != nullptr);
+    m_input->end();
+    m_isFinalized = true;
+  }
+
+  return m_output->buf();
+}
+
+bool
+Sha256::operator==(Sha256& digest)
+{
+  const Buffer& lhs = *computeDigest();
+  const Buffer& rhs = *digest.computeDigest();
+
+  if (lhs.size() != rhs.size()) {
+    return false;
+  }
+
+  // constant-time buffer comparison to mitigate timing attacks
+  return CRYPTO_memcmp(lhs.get(), rhs.get(), lhs.size()) == 0;
+}
+
+Sha256&
+Sha256::operator<<(Sha256& src)
+{
+  auto buf = src.computeDigest();
+  update(buf->get(), buf->size());
+  return *this;
+}
+
+Sha256&
+Sha256::operator<<(const std::string& str)
+{
+  update(reinterpret_cast<const uint8_t*>(str.data()), str.size());
+  return *this;
+}
+
+Sha256&
+Sha256::operator<<(const Block& block)
+{
+  update(block.wire(), block.size());
+  return *this;
+}
+
+Sha256&
+Sha256::operator<<(uint64_t value)
+{
+  update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t));
+  return *this;
+}
+
+void
+Sha256::update(const uint8_t* buffer, size_t size)
+{
+  if (m_isFinalized)
+    BOOST_THROW_EXCEPTION(Error("Digest has been already finalized"));
+
+  BOOST_ASSERT(m_input != nullptr);
+  m_input->write(buffer, size);
+  m_isEmpty = false;
+}
+
+std::string
+Sha256::toString()
+{
+  auto buf = computeDigest();
+  return toHex(*buf);
+}
+
 std::ostream&
-operator<<(std::ostream& os, Digest<CryptoPP::SHA256>& digest);
-
+operator<<(std::ostream& os, Sha256& digest)
+{
+  auto buf = digest.computeDigest();
+  printHex(os, *buf);
+  return os;
+}
 
 } // namespace util
 } // namespace ndn