security: Add SignatureSha256

Change-Id: Id9a61f898d12a6b289a24adb594471b26d3513d6
diff --git a/src/util/command-interest-validator.hpp b/src/util/command-interest-validator.hpp
index 8c5ecc3..af8c796 100644
--- a/src/util/command-interest-validator.hpp
+++ b/src/util/command-interest-validator.hpp
@@ -116,7 +116,7 @@
     }
   if(inScope == false)
     return onValidationFailed(interest.shared_from_this(), 
-                              "Signer cannot be authorized for the command: " + interest.getName().toUri());
+                              "Signer cannot be authorized for the command: " + keyName.toUri());
 
   //Check if timestamp is valid
   uint64_t timestamp = interestName.get(POS_TIMESTAMP).toNumber();
diff --git a/src/util/crypto.cpp b/src/util/crypto.cpp
index 8967523..b0767fa 100644
--- a/src/util/crypto.cpp
+++ b/src/util/crypto.cpp
@@ -1,21 +1,54 @@
 /**
  * Copyright (C) 2013 Regents of the University of California.
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  * See COPYING for copyright and distribution information.
  */
 
-#include "common.hpp"
+#include "../common.hpp"
 
 #include "crypto.hpp"
+#include <cryptopp/sha.h>
+#include <cryptopp/filters.h>
+#include <cryptopp/files.h>
 
 namespace ndn {
 
 void ndn_digestSha256(const uint8_t *data, size_t dataLength, uint8_t *digest)
 {
-  SHA256_CTX sha256;
-  SHA256_Init(&sha256);
-  SHA256_Update(&sha256, data, dataLength);
-  SHA256_Final(digest, &sha256);
+  try
+    {
+      using namespace CryptoPP;
+      
+      CryptoPP::SHA256 hash;
+      OBufferStream os;
+      StringSource(data, dataLength, true, new HashFilter(hash, new ArraySink(digest, crypto::SHA256_DIGEST_LENGTH)));
+    }
+  catch(CryptoPP::Exception& e)
+    {
+      return;
+    }
+
 }
 
+namespace crypto {
+
+ConstBufferPtr
+sha256(const uint8_t *data, size_t dataLength)
+{
+  try
+    {
+      using namespace CryptoPP;
+      
+      SHA256 hash;
+      OBufferStream os;
+      StringSource(data, dataLength, true, new HashFilter(hash, new FileSink(os)));
+      return os.buf();
+    }
+  catch(CryptoPP::Exception& e)
+    {
+      return ConstBufferPtr();
+    }
+}
+
+} // namespace crypto
+
 } // namespace ndn
diff --git a/src/util/crypto.hpp b/src/util/crypto.hpp
index 8d4aa0b..21e1e71 100644
--- a/src/util/crypto.hpp
+++ b/src/util/crypto.hpp
@@ -1,6 +1,5 @@
 /**
  * Copyright (C) 2013 Regents of the University of California.
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  * See COPYING for copyright and distribution information.
  */
 
@@ -8,9 +7,7 @@
 #define NDN_UTIL_CRYPTO_HPP
 
 #include "../common.hpp"
-
-#include <openssl/ssl.h>
-#include <openssl/rsa.h>
+#include "../encoding/buffer.hpp"
 
 namespace ndn {
 
@@ -22,6 +19,21 @@
  */
 void ndn_digestSha256(const uint8_t *data, size_t dataLength, uint8_t *digest);
 
+namespace crypto {
+
+static size_t SHA256_DIGEST_LENGTH = 32;
+
+/**
+ * Compute the sha-256 digest of data.
+ * @param data Pointer to the input byte array.
+ * @param dataLength The length of data.
+ * @return A pointer to a buffer of SHA256_DIGEST.
+ */
+ConstBufferPtr
+sha256(const uint8_t *data, size_t dataLength);
+
+} // namespace crypto
+
 } // namespace ndn
 
 #endif // NDN_UTIL_CRYPTO_HPP
diff --git a/src/util/regex/regex-top-matcher.cpp b/src/util/regex/regex-top-matcher.cpp
index 2c93f17..38f1a5a 100644
--- a/src/util/regex/regex-top-matcher.cpp
+++ b/src/util/regex/regex-top-matcher.cpp
@@ -10,10 +10,6 @@
 #include "regex-backref-manager.hpp"
 #include "regex-pattern-list-matcher.hpp"
 
-// #include "../logging.hpp"
-
-// INIT_LOGGER ("RegexTopMatcher");
-
 namespace ndn {
 
 RegexTopMatcher::RegexTopMatcher(const std::string& expr, const std::string& expand)
@@ -21,13 +17,9 @@
     m_expand(expand),
     m_secondaryUsed(false)
 {
-  // _LOG_TRACE ("Enter RegexTopMatcher Constructor");
-
   m_primaryBackRefManager = make_shared<RegexBackrefManager>();
   m_secondaryBackRefManager = make_shared<RegexBackrefManager>();
   compile();
-
-  // _LOG_TRACE ("Exit RegexTopMatcher Constructor");
 }
 
 RegexTopMatcher::~RegexTopMatcher()
@@ -38,8 +30,6 @@
 void 
 RegexTopMatcher::compile()
 {
-  // _LOG_TRACE ("Enter RegexTopMatcher::compile");
-
   std::string errMsg = "Error: RegexTopMatcher.Compile(): ";
 
   std::string expr = m_expr;
@@ -55,19 +45,13 @@
   else
     expr = expr.substr(1, expr.size()-1);
 
-  // _LOG_DEBUG ("reconstructed expr: " << expr);
-
   m_primaryMatcher = make_shared<RegexPatternListMatcher>(boost::cref(expr),
                                                           boost::cref(m_primaryBackRefManager));
-
-  // _LOG_TRACE ("Exit RegexTopMatcher::compile");
 }
 
 bool 
 RegexTopMatcher::match(const Name & name)
 {
-  // _LOG_DEBUG("Enter RegexTopMatcher::match");
-
   m_secondaryUsed = false;
 
   m_matchResult.clear();
@@ -98,8 +82,6 @@
 Name 
 RegexTopMatcher::expand (const std::string& expandStr)
 {
-  // _LOG_TRACE("Enter RegexTopMatcher::expand");
-
   Name result;
     
   shared_ptr<RegexBackrefManager> backRefManager = (m_secondaryUsed ? m_secondaryBackRefManager : m_primaryBackRefManager);
@@ -149,7 +131,6 @@
 std::string
 RegexTopMatcher::getItemFromExpand(const std::string& expand, int & offset)
 {
-  // _LOG_TRACE("Enter RegexTopMatcher::getItemFromExpand ");
   int begin = offset;
 
   if(expand[offset] == '\\')