esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
helper.hpp
Go to the documentation of this file.
1#ifndef NDNPH_KEYCHAIN_HELPER_HPP
2#define NDNPH_KEYCHAIN_HELPER_HPP
3
4#include "../packet/name.hpp"
5#include "../port/random/port.hpp"
6#include "../port/sha256/port.hpp"
7#include "../tlv/nni.hpp"
8#include "private-key.hpp"
9#include "public-key.hpp"
10
11namespace ndnph {
12namespace detail {
13
14inline bool
15computeDigest(std::initializer_list<tlv::Value> chunks, uint8_t digest[NDNPH_SHA256_LEN]) {
16 port::Sha256 hash;
17 for (const auto& chunk : chunks) {
18 hash.update(chunk.begin(), chunk.size());
19 }
20 return hash.final(digest);
21}
22
23inline Component
25 uint8_t value[8];
26 if (!port::RandomSource::generate(value, sizeof(value))) {
27 return Component();
28 }
29 return Component(region, type, sizeof(value), value);
30}
31
32class NamedKey {
33public:
35 const Name& getName() const {
36 return m_name;
37 }
38
40 void setName(const Name& v) {
41 m_name = v;
42 }
43
44protected:
45 ~NamedKey() = default;
46
47private:
48 Name m_name;
49};
50
51template<uint8_t sigType>
53 : public PublicKey
54 , public virtual NamedKey {
55public:
56 bool matchSigInfo(const SigInfo& sigInfo) const override {
57 return sigInfo.sigType == sigType && (!getName() || sigInfo.name.isPrefixOf(getName()));
58 }
59};
60
61template<uint8_t sigType>
63 : public PrivateKey
64 , public virtual NamedKey {
65public:
66 void updateSigInfo(SigInfo& sigInfo) const override {
67 sigInfo.sigType = sigType;
68 sigInfo.name = getName();
69 }
70};
71
72} // namespace detail
73} // namespace ndnph
74
75#endif // NDNPH_KEYCHAIN_HELPER_HPP
static bool generate(uint8_t *output, size_t count)
SHA256 algorithm, implemented with BearSSL.
Definition sha256-bearssl.hpp:11
void update(const uint8_t *chunk, size_t size)
Definition sha256-bearssl.hpp:17
bool final(uint8_t *digest)
Definition sha256-bearssl.hpp:21
Name component.
Definition component.hpp:16
Name.
Definition name.hpp:14
bool isPrefixOf(const Name &other) const
Determine if this name is a prefix of other.
Definition name.hpp:239
Private key.
Definition private-key.hpp:9
Public key.
Definition public-key.hpp:9
Region-based memory allocator thats owns memory of NDNph objects.
Definition region.hpp:9
SignatureInfo.
Definition sig-info.hpp:12
Name name
Definition sig-info.hpp:45
uint8_t sigType
Definition sig-info.hpp:47
Definition helper.hpp:32
void setName(const Name &v)
Assign KeyLocator name.
Definition helper.hpp:40
const Name & getName() const
Retrieve KeyLocator name.
Definition helper.hpp:35
Definition helper.hpp:64
void updateSigInfo(SigInfo &sigInfo) const override
Write SigType and KeyLocator.
Definition helper.hpp:66
Definition helper.hpp:54
bool matchSigInfo(const SigInfo &sigInfo) const override
Determine whether packet was signed by corresponding private key.
Definition helper.hpp:56
#define NDNPH_SHA256_LEN
SHA256 digest length.
Definition common.hpp:34
@ GenericNameComponent
Definition an.hpp:20
bool computeDigest(std::initializer_list< tlv::Value > chunks, uint8_t digest[NDNPH_SHA256_LEN])
Definition helper.hpp:15
Component makeRandomComponent(Region &region, uint16_t type=TT::GenericNameComponent)
Definition helper.hpp:24
Definition fs.hpp:33