security: Add helpers for Command Interest

refs: #1238

Change-Id: I5a42f888b83bcc6dc51ea02045e438a4905ed145
diff --git a/src/helper/command-interest-generator.cpp b/src/helper/command-interest-generator.cpp
new file mode 100644
index 0000000..f08d524
--- /dev/null
+++ b/src/helper/command-interest-generator.cpp
@@ -0,0 +1,56 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "command-interest-generator.hpp"
+#include "../util/time.hpp"
+#include "../util/random.hpp"
+
+#include <unistd.h>
+
+namespace ndn
+{
+const Name CommandInterestGenerator::DEFAULT_CERTIFICATE_NAME = Name();
+
+CommandInterestGenerator::CommandInterestGenerator()
+  : m_lastTimestamp(time::now() / 1000000)
+{}
+
+void
+CommandInterestGenerator::generate(Interest& interest, 
+				   const Name& certificateName /*= DEFAULT_CERTIFICATE_NAME*/)
+{
+  int64_t timestamp = time::now();
+  while(timestamp == m_lastTimestamp)
+    {
+      usleep(1000); //Guarantee unqiueness of timestamp
+      timestamp = time::now();
+    }
+  
+  interest.getName().append(name::Component::fromNumber(timestamp)).append(name::Component::fromNumber(random::generateWord64()));
+
+  if(certificateName == DEFAULT_CERTIFICATE_NAME)
+    m_keyChain.sign(interest);
+  else
+    m_keyChain.sign(interest, certificateName);
+
+  m_lastTimestamp = timestamp;
+}
+  
+void
+CommandInterestGenerator::generateWithIdentity(Interest& interest, const Name& identity)
+{
+  int64_t timestamp = time::now() / 1000000;
+  if(timestamp <= m_lastTimestamp)
+    timestamp = m_lastTimestamp + 1;
+  
+  interest.getName().append(name::Component::fromNumber(timestamp)).append(name::Component::fromNumber(random::generateWord64()));
+
+  m_keyChain.signByIdentity(interest, identity);
+
+  m_lastTimestamp = timestamp;
+}
+
+}//ndn