security: get KeyDigest from PublicKey.

Change-Id: Ibb7cf0aba262dbb80d5ea1d2da02318963c0f417
Refs: #1964
diff --git a/src/security/public-key.cpp b/src/security/public-key.cpp
index 801fe6c..07b8137 100644
--- a/src/security/public-key.cpp
+++ b/src/security/public-key.cpp
@@ -25,6 +25,7 @@
 #include "public-key.hpp"
 
 #include "../encoding/oid.hpp"
+#include "../util/crypto.hpp"
 #include "cryptopp.hpp"
 
 namespace ndn {
@@ -41,6 +42,22 @@
   decode(src);
 }
 
+const Block&
+PublicKey::computeDigest() const
+{
+  if (m_key.empty())
+    throw Error("Public key is empty");
+
+  if (m_digest.hasWire())
+    return m_digest;
+  else {
+    m_digest = Block(tlv::KeyDigest, crypto::sha256(m_key.buf(), m_key.size()));
+    m_digest.encode();
+    return m_digest;
+  }
+}
+
+
 void
 PublicKey::encode(CryptoPP::BufferedTransformation& out) const
 {
@@ -105,6 +122,8 @@
       m_type = KEY_TYPE_NULL;
       throw Error("PublicKey decoding error");
     }
+
+  m_digest.reset();
 }
 
 // Blob