util: Add toString and operator<< for util::Digest
Change-Id: Id3695ad1d66dcc93f08085ee42556c71e3eb419c
diff --git a/src/util/digest.cpp b/src/util/digest.cpp
index bb2201d..ec27804 100644
--- a/src/util/digest.cpp
+++ b/src/util/digest.cpp
@@ -130,7 +130,36 @@
return result;
}
-template class Digest<CryptoPP::SHA256>;
+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)
+{
+ using namespace CryptoPP;
+
+ std::string output;
+ ConstBufferPtr buffer = digest.computeDigest();
+ StringSource(buffer->buf(), buffer->size(), true, new HexEncoder(new FileSink(os)));
+
+ return os;
+}
+
+template
+class Digest<CryptoPP::SHA256>;
+
+template
+std::ostream&
+operator<<(std::ostream& os, Digest<CryptoPP::SHA256>& digest);
+
} // namespace util
} // namespace ndn
diff --git a/src/util/digest.hpp b/src/util/digest.hpp
index 39ff811..db9ccc1 100644
--- a/src/util/digest.hpp
+++ b/src/util/digest.hpp
@@ -163,6 +163,14 @@
static ConstBufferPtr
computeDigest(const uint8_t* buffer, size_t size);
+ /**
+ * @brief Convert digest to std::string
+ *
+ * Note that this method will invoke computeDigest().
+ * Once this method is invoked, the digest is finalized.
+ */
+ std::string
+ toString();
private:
/**
@@ -180,6 +188,10 @@
bool m_isFinalized;
};
+template<typename Hash>
+std::ostream&
+operator<<(std::ostream& os, Digest<Hash>& digest);
+
/**
* @brief A digest using SHA256 as the hash function.
*/