test: MultiSignature

This commit is contained in:
2020-10-10 09:39:37 +08:00
parent 6387a0cc8d
commit 32ae4618f7
2 changed files with 76 additions and 64 deletions
+4
View File
@@ -24,6 +24,7 @@
#include <ndn-cxx/face.hpp>
#include <string>
#include <ndn-cxx/security/key-chain.hpp>
#include <iostream>
// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
@@ -47,6 +48,8 @@ namespace ndn {
// 设置如果强制刷新,则不会命中途中路由器的缓存
interest.setMustBeFresh(true);
mKeyChain.sign(interest);
// 发送兴趣包
m_face.expressInterest(interest,
bind(&Consumer::onData, this, _1, _2),
@@ -80,6 +83,7 @@ namespace ndn {
private:
Face m_face;
KeyChain mKeyChain;
};
} // namespace examples
+72 -64
View File
@@ -5,90 +5,98 @@
#include <ndn-cxx/face.hpp>
#include <ndn-cxx/security/key-chain.hpp>
#include <ndn-cxx/security/signing-helpers.hpp>
#include <ndn-cxx/lp/lp-headers.hpp>
#include <ndn-cxx/lp/tags.hpp>
#include <iostream>
using namespace std;
// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
namespace ndn {
// Additional nested namespaces should be used to prevent/limit name conflicts
namespace examples {
namespace examples {
class Producer {
public:
Producer() {}
class Producer {
public:
Producer() {}
void
run() {
cout << "before setInterestFilter" << endl;
// 向NFD注册一个名称前缀 /example/testApp
// 如果成功,NFD会在接收到以这个前缀开头的兴趣包转发给本程序,Producer::onInterest回调会被调用
m_face.setInterestFilter("/example/testApp",
bind(&Producer::onInterest, this, _1, _2),
nullptr, // RegisterPrefixSuccessCallback is optional
bind(&Producer::onRegisterFailed, this, _1, _2));
cout << "before processEvents" << endl;
void
run() {
cout << "before setInterestFilter" << endl;
// 向NFD注册一个名称前缀 /example/testApp
// 如果成功,NFD会在接收到以这个前缀开头的兴趣包转发给本程序,Producer::onInterest回调会被调用
m_face.setInterestFilter("/example/testApp",
bind(&Producer::onInterest, this, _1, _2),
nullptr, // RegisterPrefixSuccessCallback is optional
bind(&Producer::onRegisterFailed, this, _1, _2));
cout << "before processEvents" << endl;
// 处理NDN事件,Producer::onInterest回调是有这个函数里面调用的
// 意味着Producer::onInterest回调会在调用 face.processEvents 的线程运行
// 这个函数是一个阻塞函数,调用这个函数会期望处理所有相关的事件,如果向上面那样注册了前缀监听,则会一直处理事件
m_face.processEvents();
cout << "after processEvents" << endl;
// 处理NDN事件,Producer::onInterest回调是有这个函数里面调用的
// 意味着Producer::onInterest回调会在调用 face.processEvents 的线程运行
// 这个函数是一个阻塞函数,调用这个函数会期望处理所有相关的事件,如果向上面那样注册了前缀监听,则会一直处理事件
m_face.processEvents();
cout << "after processEvents" << endl;
}
private:
void
onInterest(const InterestFilter &, const Interest &interest) {
std::cout << ">> I: " << interest << std::endl;
auto tag = interest.getTag<ndn::lp::MultiSignatureTag>();
if (tag) {
for (auto &sigSection : tag->get().getSignatures()) {
cout << "Signature: " << sigSection.signatureInfo.getKeyLocator() << endl;
}
}
// 这边可以提取出来请求的参数
std::string s(reinterpret_cast<const char *>(interest.getApplicationParameters().value()),
interest.getApplicationParameters().value_size());
private:
void
onInterest(const InterestFilter &, const Interest &interest) {
std::cout << ">> I: " << interest << std::endl;
std::cout << "收到的参数: " << s << std::endl;
// 这边可以提取出来请求的参数
std::string s(reinterpret_cast<const char *>(interest.getApplicationParameters().value()),
interest.getApplicationParameters().value_size());
static const std::string content("Hello, world!");
std::cout << "收到的参数: " << s << std::endl;
// Create Data packet
auto data = make_shared<Data>(interest.getName());
data->setFreshnessPeriod(10_s);
data->setContent(reinterpret_cast<const uint8_t *>(content.data()), content.size());
static const std::string content("Hello, world!");
// Sign Data packet with default identity
m_keyChain.sign(*data);
// m_keyChain.sign(*data, signingByIdentity(<identityName>));
// m_keyChain.sign(*data, signingByKey(<keyName>));
// m_keyChain.sign(*data, signingByCertificate(<certName>));
// m_keyChain.sign(*data, signingWithSha256());
// Create Data packet
auto data = make_shared<Data>(interest.getName());
data->setFreshnessPeriod(10_s);
data->setContent(reinterpret_cast<const uint8_t *>(content.data()), content.size());
// Return Data packet to the requester
std::cout << "<< D: " << *data << std::endl;
m_face.put(*data);
}
// Sign Data packet with default identity
m_keyChain.sign(*data);
// m_keyChain.sign(*data, signingByIdentity(<identityName>));
// m_keyChain.sign(*data, signingByKey(<keyName>));
// m_keyChain.sign(*data, signingByCertificate(<certName>));
// m_keyChain.sign(*data, signingWithSha256());
void
onRegisterFailed(const Name &prefix, const std::string &reason) {
std::cerr << "ERROR: Failed to register prefix '" << prefix
<< "' with the local forwarder (" << reason << ")" << std::endl;
m_face.shutdown();
}
// Return Data packet to the requester
std::cout << "<< D: " << *data << std::endl;
m_face.put(*data);
}
private:
Face m_face;
KeyChain m_keyChain;
};
void
onRegisterFailed(const Name &prefix, const std::string &reason) {
std::cerr << "ERROR: Failed to register prefix '" << prefix
<< "' with the local forwarder (" << reason << ")" << std::endl;
m_face.shutdown();
}
private:
Face m_face;
KeyChain m_keyChain;
};
} // namespace examples
} // namespace examples
} // namespace ndn
int
main(int argc, char **argv) {
try {
ndn::examples::Producer producer;
producer.run();
return 0;
}
catch (const std::exception &e) {
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
}
try {
ndn::examples::Producer producer;
producer.run();
return 0;
}
catch (const std::exception &e) {
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
}
}