util: remove Digest class template
Change-Id: I4e718fb7369df0963cc2e1e9b23b0c5e0c9d03f8
Refs: #3886
diff --git a/src/util/concepts.hpp b/src/util/concepts.hpp
index bb8c744..4b4d758 100644
--- a/src/util/concepts.hpp
+++ b/src/util/concepts.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2017 Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
@@ -88,24 +88,6 @@
}
};
-/** \brief a concept check for CryptoPP hash algorithm
- */
-template<class X>
-class Hashable
-{
-public:
- BOOST_CONCEPT_USAGE(Hashable)
- {
- X hash;
- uint8_t* buf = 0;
- size_t size = hash.DigestSize();
-
- hash.Update(buf, size);
- hash.Final(buf);
- hash.Restart();
- }
-};
-
namespace detail {
template<class X>
diff --git a/src/util/digest.cpp b/src/util/digest.cpp
index 516255f..55eb459 100644
--- a/src/util/digest.cpp
+++ b/src/util/digest.cpp
@@ -25,164 +25,10 @@
#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>
namespace ndn {
namespace util {
-template<typename Hash>
-Digest<Hash>::Digest()
-{
- reset();
-}
-
-template<typename Hash>
-Digest<Hash>::Digest(std::istream& is)
- : m_isInProcess(false)
- , m_isFinalized(true)
-{
- using namespace CryptoPP;
-
- m_buffer = make_shared<Buffer>(m_hash.DigestSize());
- FileSource(is, true,
- new HashFilter(m_hash,
- new ArraySink(m_buffer->get(), m_buffer->size())));
-}
-
-template<typename Hash>
-void
-Digest<Hash>::reset()
-{
- m_hash.Restart();
- m_buffer = make_shared<Buffer>(m_hash.DigestSize());
- m_isInProcess = false;
- m_isFinalized = false;
-}
-
-template<typename Hash>
-void
-Digest<Hash>::finalize()
-{
- // return immediately if Digest is finalized already.
- if (m_isFinalized)
- return;
-
- m_hash.Final(m_buffer->get());
-
- m_isFinalized = true;
-}
-
-template<typename Hash>
-ConstBufferPtr
-Digest<Hash>::computeDigest()
-{
- finalize();
- return m_buffer;
-}
-
-template<typename Hash>
-bool
-Digest<Hash>::operator==(Digest<Hash>& 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.buf(), rhs.buf(), lhs.size()) == 0;
-}
-
-template<typename Hash>
-Digest<Hash>&
-Digest<Hash>::operator<<(Digest<Hash>& src)
-{
- ConstBufferPtr buffer = src.computeDigest();
- update(buffer->get(), buffer->size());
-
- return *this;
-}
-
-template<typename Hash>
-Digest<Hash>&
-Digest<Hash>::operator<<(const std::string& str)
-{
- update(reinterpret_cast<const uint8_t*>(str.c_str()), str.size());
-
- return *this;
-}
-
-template<typename Hash>
-Digest<Hash>&
-Digest<Hash>::operator<<(const Block& block)
-{
- update(block.wire(), block.size());
-
- return *this;
-}
-
-template<typename Hash>
-Digest<Hash>&
-Digest<Hash>::operator<<(uint64_t value)
-{
- update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t));
-
- return *this;
-}
-
-template<typename Hash>
-void
-Digest<Hash>::update(const uint8_t* buffer, size_t size)
-{
- // cannot update Digest when it has been finalized
- if (m_isFinalized)
- BOOST_THROW_EXCEPTION(Error("Digest has been already finalized"));
-
- m_hash.Update(buffer, size);
-
- m_isInProcess = true;
-}
-
-template<typename Hash>
-ConstBufferPtr
-Digest<Hash>::computeDigest(const uint8_t* buffer, size_t size)
-{
- Hash hash;
- BufferPtr result = make_shared<Buffer>(hash.DigestSize());
- hash.Update(buffer, size);
- hash.Final(result->get());
-
- return result;
-}
-
-template<typename Hash>
-std::string
-Digest<Hash>::toString()
-{
- std::ostringstream os;
- os << *this;
-
- return os.str();
-}
-
-template<typename Hash>
-std::ostream&
-operator<<(std::ostream& os, Digest<Hash>& digest)
-{
- ConstBufferPtr buffer = digest.computeDigest();
- printHex(os, buffer->buf(), buffer->size());
-
- return os;
-}
-
-
-////////////////////////////////////////
-
-
Sha256::Sha256()
{
reset();
diff --git a/src/util/digest.hpp b/src/util/digest.hpp
index 40d0100..1c3c857 100644
--- a/src/util/digest.hpp
+++ b/src/util/digest.hpp
@@ -22,7 +22,6 @@
#ifndef NDN_UTIL_DIGEST_HPP
#define NDN_UTIL_DIGEST_HPP
-#include "concepts.hpp"
#include "crypto.hpp"
#include "../encoding/block.hpp"
#include "../encoding/buffer-stream.hpp"
@@ -32,171 +31,6 @@
namespace util {
/**
- * @brief provides a stateful digest calculation
- *
- * SHA256 example:
- *
- * Digest<CryptoPP::SHA256> digest;
- * digest.update(buf1, size1);
- * digest.update(buf2, size2);
- * ...
- * ConstBufferPtr result = digest.computeDigest();
- */
-template<typename Hash>
-class Digest
-{
-public:
- BOOST_CONCEPT_ASSERT((Hashable<Hash>));
-
- typedef Hash HashFunction;
-
- class Error : public std::runtime_error
- {
- public:
- explicit
- Error(const std::string& what)
- : std::runtime_error(what)
- {
- }
- };
-
- Digest();
-
- /**
- * @brief Calculate digest of the input stream @p is
- * @param is input stream
- */
- explicit
- Digest(std::istream& is);
-
- /**
- * @brief Discard the current state and start a new digest calculation.
- */
- void
- reset();
-
- /**
- * @brief Check if digest is empty.
- *
- * An empty digest means nothing has been taken into calculation.
- */
- bool
- empty() const
- {
- return !m_isInProcess;
- }
-
- /**
- * @brief Finalize and return the digest based on all previously supplied inputs.
- */
- ConstBufferPtr
- computeDigest();
-
- /**
- * @brief Check if the supplied digest equals to this digest
- *
- * @note This method will invoke computeDigest(), finalizing the digest.
- */
- bool
- operator==(Digest<Hash>& digest);
-
- /**
- * @brief Check if the supplied digest is not equal to this digest
- *
- * @note This method will invoke computeDigest(), finalizing the digest.
- */
- bool
- operator!=(Digest<Hash>& digest)
- {
- return !(*this == digest);
- }
-
- /**
- * @brief Add existing digest to the digest calculation
- * @param src digest to combine with
- *
- * The result of this combination is `digest(digest(...))`
- *
- * @note This method will invoke computeDigest(), finalizing the digest.
- */
- Digest<Hash>&
- operator<<(Digest<Hash>& src);
-
- /**
- * @brief Add string to the digest calculation
- * @param str string to put into digest
- */
- Digest<Hash>&
- operator<<(const std::string& str);
-
- /**
- * @brief Add block to the digest calculation
- * @param block data block to put into digest
- * @throw Error the digest has been finalized.
- */
- Digest<Hash>&
- operator<<(const Block& block);
-
- /**
- * @brief Add uint64_t value to the digest calculation
- * @param value the integer value to put into digest
- * @throw Error the digest has been finalized.
- */
- Digest<Hash>&
- operator<<(uint64_t value);
-
- /**
- * @brief Add a buffer to the digest calculation
- *
- * Update the state of the digest if it has not been finalized and mark the digest as
- * InProcess.
- *
- * @param buffer the input buffer
- * @param size the size of the input buffer.
- * @throw Error the digest has been finalized.
- */
- void
- update(const uint8_t* buffer, size_t size);
-
- /**
- * @brief Compute one-time digest
- * @param buffer the input buffer
- * @param size the size of the input buffer.
- * @return digest computed according to the `Hash` algorithm
- */
- static ConstBufferPtr
- computeDigest(const uint8_t* buffer, size_t size);
-
- /**
- * @brief Convert digest to std::string
- *
- * @note This method will invoke computeDigest(), finalizing the digest.
- */
- std::string
- toString();
-
-private:
- /**
- * @brief Finalize digest.
- *
- * All subsequent calls to "operator<<" will throw an exception
- */
- void
- finalize();
-
-private:
- Hash m_hash;
- BufferPtr m_buffer;
- bool m_isInProcess;
- bool m_isFinalized;
-};
-
-template<typename Hash>
-std::ostream&
-operator<<(std::ostream& os, Digest<Hash>& digest);
-
-
-/**
* @brief Provides stateful SHA-256 digest calculation.
*
* Example: