ccnx: Rename Key to Cert; fix bug with the usage of Hash

Change-Id: Ic7656e6d8d46729d663ccfa5412e78443aff76bc
diff --git a/src/hash-helper.cc b/src/hash-helper.cc
index 6fe0ca9..11b29e2 100644
--- a/src/hash-helper.cc
+++ b/src/hash-helper.cc
@@ -167,3 +167,25 @@
 
   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, Ccnx::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.h b/src/hash-helper.h
index 64a625e..9050573 100644
--- a/src/hash-helper.h
+++ b/src/hash-helper.h
@@ -27,6 +27,7 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/exception/all.hpp>
 #include <boost/filesystem.hpp>
+#include "ccnx-common.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
@@ -72,6 +73,9 @@
   static HashPtr
   FromFileContent (const boost::filesystem::path &fileName);
 
+  static HashPtr
+  FromBytes (const Ccnx::Bytes &bytes);
+
   ~Hash ()
   {
     if (m_length != 0)