Exemple #1
0
 def encode_name(self, name: Name) -> bytearray:
     """
     Assembly a name-TLV
     :param name: Name
     :return: Name-TLV
     """
     encoder = TlvEncoder()
     # name = name[1:].split('/')
     # for component in name[::-1]:
     #     encoder.writeBlobTlv(Tlv.NameComponent, [ord(c) for c in component])
     # fill backwards
     if name.digest:
         encoder.writeBlobTlv(Tlv.ImplicitSha256DigestComponent, name.digest)
     for c in name._components[::-1]:
         encoder.writeBlobTlv(Tlv.NameComponent, c)
     encoder.writeTypeAndLength(Tlv.Name, len(encoder))
     return encoder.getOutput() #.tobytes()
Exemple #2
0
 def encode_data(self, name: Name, payload: bytearray) -> bytearray:
     """
     Assembly a data packet
     :param name: Name
     :param payload: Payload
     :return: Data-TLV
     """
     encoder = TlvEncoder()
     # Add content
     encoder.writeBlobTlv(Tlv.Content, payload)
     # Add meta info (empty)
     encoder.writeTypeAndLength(Tlv.MetaInfo, 0)
     # Add name
     encoder.writeBuffer(self.encode_name(name))
     # Add data type and len
     encoder.writeTypeAndLength(Tlv.Data, len(encoder))
     return encoder.getOutput().tobytes()
Exemple #3
0
 def encode_interest(self, name: Name) -> bytearray:
     """
     Assembly an interest packet
     :param name: Name
     :return: Interest-TLV
     """
     encoder = TlvEncoder()
     # Add nonce
     nonce = bytearray(4)
     for i in range(4):
         nonce[i] = SystemRandom().randint(0, 0xff)
     encoder.writeBlobTlv(Tlv.Nonce, nonce)
     # TODO: add implicitDigest flag here ?
     # Add name
     encoder.writeBuffer(self.encode_name(name))
     # Add interest type and len
     encoder.writeTypeAndLength(Tlv.Interest, len(encoder))
     return encoder.getOutput().tobytes()
Exemple #4
0
 def encode_data(self, name: Name, payload: bytearray) -> bytearray:
     """
     Assembly a data packet including a signature according to NDN packet format specification 0.3 (DigestSha256).
     :param name: Name
     :param payload: Payload
     :return: Data-TLV
     """
     encoder = TlvEncoder()
     # Add signature (DigestSha256, zeroed)
     encoder.writeBlobTlv(Tlv.SignatureValue, bytearray(32)) # signature value to set later
     encoder.writeBlobTlv(Tlv.SignatureInfo, bytearray([Tlv.SignatureType, 1, 0]))
     # Add content
     encoder.writeBlobTlv(Tlv.Content, payload)
     # Add meta info (empty)
     encoder.writeTypeAndLength(Tlv.MetaInfo, 0)
     # Add name
     encoder.writeBuffer(self.encode_name(name))
     # Add data type and len
     encoder.writeTypeAndLength(Tlv.Data, len(encoder))
     # Add signature value
     packet_without_sig = encoder.getOutput().tobytes()
     m = hashlib.sha256()
     m.update(packet_without_sig[:-32])
     sig = m.digest()
     packet_with_sig = packet_without_sig[:-32] + sig
     return packet_with_sig