本文整理汇总了Python中pyndn.encoding.tlv.tlv_encoder.TlvEncoder.writeBlobTlv方法的典型用法代码示例。如果您正苦于以下问题:Python TlvEncoder.writeBlobTlv方法的具体用法?Python TlvEncoder.writeBlobTlv怎么用?Python TlvEncoder.writeBlobTlv使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyndn.encoding.tlv.tlv_encoder.TlvEncoder
的用法示例。
在下文中一共展示了TlvEncoder.writeBlobTlv方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encodeData
# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBlobTlv [as 别名]
def encodeData(self, data):
"""
Encode data in NDN-TLV and return the encoding and signed offsets.
:param Data data: The Data object to encode.
:return: A Tuple of (encoding, signedPortionBeginOffset,
signedPortionEndOffset) where encoding is a Blob containing the
encoding, signedPortionBeginOffset is the offset in the encoding of
the beginning of the signed portion, and signedPortionEndOffset is
the offset in the encoding of the end of the signed portion.
:rtype: (Blob, int, int)
"""
encoder = TlvEncoder(1500)
saveLength = len(encoder)
# Encode backwards.
# TODO: The library needs to handle other signature types than
# SignatureSha256WithRsa.
encoder.writeBlobTlv(Tlv.SignatureValue,
data.getSignature().getSignature().buf())
signedPortionEndOffsetFromBack = len(encoder)
self._encodeSignatureSha256WithRsa(data.getSignature(), encoder)
encoder.writeBlobTlv(Tlv.Content, data.getContent().buf())
self._encodeMetaInfo(data.getMetaInfo(), encoder)
self._encodeName(data.getName(), encoder)
signedPortionBeginOffsetFromBack = len(encoder)
encoder.writeTypeAndLength(Tlv.Data, len(encoder) - saveLength)
signedPortionBeginOffset = (len(encoder) -
signedPortionBeginOffsetFromBack)
signedPortionEndOffset = len(encoder) - signedPortionEndOffsetFromBack
return (Blob(encoder.getOutput(), False), signedPortionBeginOffset,
signedPortionEndOffset)
示例2: encodeForwardingEntry
# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBlobTlv [as 别名]
def encodeForwardingEntry(self, forwardingEntry):
"""
Encode forwardingEntry and return the encoding.
:param forwardingEntry: The ForwardingEntry object to encode.
:type forwardingEntry: ForwardingEntry
:return: A Blob containing the encoding.
:rtype: Blob
"""
encoder = TlvEncoder(256)
saveLength = len(encoder)
# Encode backwards.
encoder.writeOptionalNonNegativeIntegerTlvFromFloat(
Tlv.FreshnessPeriod, forwardingEntry.getFreshnessPeriod())
encoder.writeNonNegativeIntegerTlv(
Tlv.ForwardingFlags,
forwardingEntry.getForwardingFlags().getForwardingEntryFlags())
encoder.writeOptionalNonNegativeIntegerTlv(
Tlv.FaceID, forwardingEntry.getFaceId())
self._encodeName(forwardingEntry.getPrefix(), encoder)
if (forwardingEntry.getAction() != None and
len(forwardingEntry.getAction()) > 0):
# Convert str to a bytearray.
encoder.writeBlobTlv(
Tlv.Action, bytearray(forwardingEntry.getAction(), 'ascii'))
encoder.writeTypeAndLength(Tlv.ForwardingEntry,
len(encoder) - saveLength)
return Blob(encoder.getOutput(), False)
示例3: encodeEncryptedContent
# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBlobTlv [as 别名]
def encodeEncryptedContent(self, encryptedContent):
"""
Encode the EncryptedContent in NDN-TLV and return the encoding.
:param EncryptedContent encryptedContent: The EncryptedContent object to
encode.
:return: A Blob containing the encoding.
:rtype: Blob
"""
encoder = TlvEncoder(256)
saveLength = len(encoder)
# Encode backwards.
encoder.writeBlobTlv(
Tlv.Encrypt_EncryptedPayload, encryptedContent.getPayload().buf())
encoder.writeOptionalBlobTlv(
Tlv.Encrypt_InitialVector, encryptedContent.getInitialVector().buf())
# Assume the algorithmType value is the same as the TLV type.
encoder.writeNonNegativeIntegerTlv(
Tlv.Encrypt_EncryptionAlgorithm, encryptedContent.getAlgorithmType())
Tlv0_1_1WireFormat._encodeKeyLocator(
Tlv.KeyLocator, encryptedContent.getKeyLocator(), encoder)
encoder.writeTypeAndLength(
Tlv.Encrypt_EncryptedContent, len(encoder) - saveLength)
return Blob(encoder.getOutput(), False)
示例4: encodeControlResponse
# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBlobTlv [as 别名]
def encodeControlResponse(self, controlResponse):
"""
Encode controlResponse and return the encoding.
:param controlResponse: The ControlResponse object to encode.
:type controlResponse: ControlResponse
:return: A Blob containing the encoding.
:rtype: Blob
"""
encoder = TlvEncoder(256)
saveLength = len(encoder)
# Encode backwards.
# Encode the body.
if controlResponse.getBodyAsControlParameters() != None:
self._encodeControlParameters(
controlResponse.getBodyAsControlParameters(), encoder)
encoder.writeBlobTlv(
Tlv.NfdCommand_StatusText, Blob(controlResponse.getStatusText()).buf())
encoder.writeNonNegativeIntegerTlv(
Tlv.NfdCommand_StatusCode, controlResponse.getStatusCode())
encoder.writeTypeAndLength(Tlv.NfdCommand_ControlResponse,
len(encoder) - saveLength)
return Blob(encoder.getOutput(), False)
示例5: encodeSignatureValue
# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBlobTlv [as 别名]
def encodeSignatureValue(self, signature):
"""
Encode the signatureValue in the Signature object as an NDN-TLV
SignatureValue (the signature bits) and return the encoding.
:param signature: An object of a subclass of Signature with the
signature value to encode.
:type signature: An object of a subclass of Signature
:return: A Blob containing the encoding.
:rtype: Blob
"""
encoder = TlvEncoder(256)
encoder.writeBlobTlv(Tlv.SignatureValue, signature.getSignature().buf())
return Blob(encoder.getOutput(), False)
示例6: _encodeLpNack
# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBlobTlv [as 别名]
def _encodeLpNack(interest, networkNack):
"""
Encode the interest into an NDN-TLV LpPacket as a NACK with the reason
code in the networkNack object.
TODO: Generalize this and move to WireFormat.encodeLpPacket.
:param Interest interest: The Interest to put in the LpPacket fragment.
:param NetworkNack networkNack: The NetworkNack with the reason code.
:return: A Blob containing the encoding.
:rtype: Blob
"""
encoder = TlvEncoder(256)
saveLength = len(encoder)
# Encode backwards.
# Encode the fragment with the Interest.
encoder.writeBlobTlv(
Tlv.LpPacket_Fragment, interest.wireEncode(TlvWireFormat.get()).buf())
# Encode the reason.
if (networkNack.getReason() == NetworkNack.Reason.NONE or
networkNack.getReason() == NetworkNack.Reason.CONGESTION or
networkNack.getReason() == NetworkNack.Reason.DUPLICATE or
networkNack.getReason() == NetworkNack.Reason.NO_ROUTE):
# The Reason enum is set up with the correct integer for each NDN-TLV Reason.
reason = networkNack.getReason()
elif networkNack.getReason() == NetworkNack.Reason.OTHER_CODE:
reason = networkNack.getOtherReasonCode()
else:
# We don't expect this to happen.
raise RuntimeError("unrecognized NetworkNack.getReason() value")
nackSaveLength = len(encoder)
encoder.writeNonNegativeIntegerTlv(Tlv.LpPacket_NackReason, reason)
encoder.writeTypeAndLength(
Tlv.LpPacket_Nack, len(encoder) - nackSaveLength)
encoder.writeTypeAndLength(
Tlv.LpPacket_LpPacket, len(encoder) - saveLength)
return Blob(encoder.getOutput(), False)
示例7: wireEncode
# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBlobTlv [as 别名]
def wireEncode(self, wireFormat = None):
"""
Encode this as an NDN-TLV SafeBag.
:return: The encoded byte array as a Blob.
:rtype: Blob
"""
# Encode directly as TLV. We don't support the WireFormat abstraction
# because this isn't meant to go directly on the wire.
encoder = TlvEncoder(256)
saveLength = len(encoder)
# Encode backwards.
encoder.writeBlobTlv(
Tlv.SafeBag_EncryptedKeyBag, self._privateKeyBag.buf())
# Add the entire Data packet encoding as is.
encoder.writeBuffer(
self._certificate.wireEncode(TlvWireFormat.get()).buf())
encoder.writeTypeAndLength(
Tlv.SafeBag_SafeBag, len(encoder) - saveLength)
return Blob(encoder.getOutput(), False)
示例8: encodeInterest
# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBlobTlv [as 别名]
def encodeInterest(self, interest):
"""
Encode interest in NDN-TLV and return the encoding.
:param Interest interest: The Interest object to encode.
:return: A Blob containing the encoding.
:rtype: Blob
"""
encoder = TlvEncoder(256)
saveLength = len(encoder)
# Encode backwards.
encoder.writeOptionalNonNegativeIntegerTlvFromFloat(
Tlv.InterestLifetime, interest.getInterestLifetimeMilliseconds())
encoder.writeOptionalNonNegativeIntegerTlv(
Tlv.Scope, interest.getScope())
# Encode the Nonce as 4 bytes.
if interest.getNonce().size() == 0:
# This is the most common case. Generate a nonce.
nonce = bytearray(4)
for i in range(4):
nonce[i] = _systemRandom.randint(0, 0xff)
encoder.writeBlobTlv(Tlv.Nonce, nonce)
elif interest.getNonce().size() < 4:
nonce = bytearray(4)
# Copy existing nonce bytes.
nonce[:interest.getNonce().size()] = interest.getNonce().buf()
# Generate random bytes for remaining bytes in the nonce.
for i in range(interest.getNonce().size(), 4):
nonce[i] = _systemRandom.randint(0, 0xff)
encoder.writeBlobTlv(Tlv.Nonce, nonce)
elif interest.getNonce().size() == 4:
# Use the nonce as-is.
encoder.writeBlobTlv(Tlv.Nonce, interest.getNonce().buf())
else:
# Truncate.
encoder.writeBlobTlv(Tlv.Nonce, interest.getNonce().buf()[:4])
self._encodeSelectors(interest, encoder)
self._encodeName(interest.getName(), encoder)
encoder.writeTypeAndLength(Tlv.Interest, len(encoder) - saveLength)
return Blob(encoder.getOutput(), False)
示例9: _signInterest
# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBlobTlv [as 别名]
def _signInterest(self, interest, certificateName, wireFormat = None):
"""
Append a SignatureInfo to the Interest name, sign the name components
and append a final name component with the signature bits.
:param Interest interest: The Interest object to be signed. This appends
name components of SignatureInfo and the signature bits.
:param Name certificateName: The certificate name of the key to use for
signing.
:param wireFormat: (optional) A WireFormat object used to encode the
input. If omitted, use WireFormat.getDefaultWireFormat().
:type wireFormat: A subclass of WireFormat
"""
if wireFormat == None:
# Don't use a default argument since getDefaultWireFormat can change.
wireFormat = WireFormat.getDefaultWireFormat()
# TODO: Handle signature algorithms other than Sha256WithRsa.
signature = Sha256WithRsaSignature()
signature.getKeyLocator().setType(KeyLocatorType.KEYNAME)
signature.getKeyLocator().setKeyName(certificateName.getPrefix(-1))
# Append the encoded SignatureInfo.
interest.getName().append(wireFormat.encodeSignatureInfo(signature))
# Append an empty signature so that the "signedPortion" is correct.
interest.getName().append(Name.Component())
# Encode once to get the signed portion.
encoding = interest.wireEncode(wireFormat)
signedSignature = self.sign(encoding.toSignedBuffer(), certificateName)
# Remove the empty signature and append the real one.
encoder = TlvEncoder(256)
encoder.writeBlobTlv(
Tlv.SignatureValue, signedSignature.getSignature().buf())
interest.setName(interest.getName().getPrefix(-1).append(
wireFormat.encodeSignatureValue(signedSignature)))
示例10: encodeInterest
# 需要导入模块: from pyndn.encoding.tlv.tlv_encoder import TlvEncoder [as 别名]
# 或者: from pyndn.encoding.tlv.tlv_encoder.TlvEncoder import writeBlobTlv [as 别名]
def encodeInterest(self, interest):
"""
Encode interest in NDN-TLV and return the encoding.
:param Interest interest: The Interest object to encode.
:return: A Tuple of (encoding, signedPortionBeginOffset,
signedPortionEndOffset) where encoding is a Blob containing the
encoding, signedPortionBeginOffset is the offset in the encoding of
the beginning of the signed portion, and signedPortionEndOffset is
the offset in the encoding of the end of the signed portion. The
signed portion starts from the first name component and ends just
before the final name component (which is assumed to be a signature
for a signed interest).
:rtype: (Blob, int, int)
"""
encoder = TlvEncoder(256)
saveLength = len(encoder)
# Encode backwards.
encoder.writeOptionalNonNegativeIntegerTlvFromFloat(
Tlv.InterestLifetime, interest.getInterestLifetimeMilliseconds())
encoder.writeOptionalNonNegativeIntegerTlv(
Tlv.Scope, interest.getScope())
# Encode the Nonce as 4 bytes.
if interest.getNonce().size() == 0:
# This is the most common case. Generate a nonce.
nonce = bytearray(4)
for i in range(4):
nonce[i] = _systemRandom.randint(0, 0xff)
encoder.writeBlobTlv(Tlv.Nonce, nonce)
elif interest.getNonce().size() < 4:
nonce = bytearray(4)
# Copy existing nonce bytes.
nonce[:interest.getNonce().size()] = interest.getNonce().buf()
# Generate random bytes for remaining bytes in the nonce.
for i in range(interest.getNonce().size(), 4):
nonce[i] = _systemRandom.randint(0, 0xff)
encoder.writeBlobTlv(Tlv.Nonce, nonce)
elif interest.getNonce().size() == 4:
# Use the nonce as-is.
encoder.writeBlobTlv(Tlv.Nonce, interest.getNonce().buf())
else:
# Truncate.
encoder.writeBlobTlv(Tlv.Nonce, interest.getNonce().buf()[:4])
self._encodeSelectors(interest, encoder)
(tempSignedPortionBeginOffset, tempSignedPortionEndOffset) = \
self._encodeName(interest.getName(), encoder)
signedPortionBeginOffsetFromBack = (len(encoder) -
tempSignedPortionBeginOffset)
signedPortionEndOffsetFromBack = (len(encoder) -
tempSignedPortionEndOffset)
encoder.writeTypeAndLength(Tlv.Interest, len(encoder) - saveLength)
signedPortionBeginOffset = (len(encoder) -
signedPortionBeginOffsetFromBack)
signedPortionEndOffset = len(encoder) - signedPortionEndOffsetFromBack
return (Blob(encoder.getOutput(), False), signedPortionBeginOffset,
signedPortionEndOffset)