當前位置: 首頁>>代碼示例>>Python>>正文


Python tlv_encoder.TlvEncoder類代碼示例

本文整理匯總了Python中pyndn.encoding.tlv.tlv_encoder.TlvEncoder的典型用法代碼示例。如果您正苦於以下問題:Python TlvEncoder類的具體用法?Python TlvEncoder怎麽用?Python TlvEncoder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了TlvEncoder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: encodeEncryptedContent

    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)
開發者ID:MAHIS,項目名稱:PyNDN2,代碼行數:27,代碼來源:tlv_0_1_1_wire_format.py

示例2: encodeName

    def encodeName(self, name):
        """
        Encode name in NDN-TLV and return the encoding.

        :param Name name: The Name object to encode.
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        self._encodeName(name, encoder)
        return Blob(encoder.getOutput(), False)
開發者ID:WeiqiJust,項目名稱:NDN-total,代碼行數:11,代碼來源:tlv_0_1_wire_format.py

示例3: encodeControlParameters

    def encodeControlParameters(self, controlParameters):
        """
        Encode controlParameters and return the encoding.

        :param controlParameters: The ControlParameters object to encode.
        :type controlParameters: ControlParameters
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        self._encodeControlParameters(controlParameters, encoder)
        return Blob(encoder.getOutput(), False)
開發者ID:MAHIS,項目名稱:PyNDN2,代碼行數:12,代碼來源:tlv_0_1_1_wire_format.py

示例4: encodeSignatureInfo

    def encodeSignatureInfo(self, signature):
        """
        Encode signature as an NDN-TLV SignatureInfo and return the encoding.

        :param signature: An object of a subclass of Signature to encode.
        :type signature: An object of a subclass of Signature
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)
        self._encodeSignatureInfo(signature, encoder)

        return Blob(encoder.getOutput(), False)
開發者ID:MAHIS,項目名稱:PyNDN2,代碼行數:13,代碼來源:tlv_0_1_1_wire_format.py

示例5: wireEncode

    def wireEncode(self):
    #    if self.m_wire.hasWire():
    #       return m_wire;

    #    EncodingEstimator estimator;
    #    size_t estimatedSize = wireEncode(estimator);

        buffer = TlvEncoder()
        
        # Ummm, this is not how polymorphism should look
        self.wireEncodeX(buffer)
        output = buffer.getOutput()
        return Blob((output), False)
開發者ID:mengchenpei,項目名稱:Mu-lighting,代碼行數:13,代碼來源:CommandParameters.py

示例6: encodeControlResponse

    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)
開發者ID:MAHIS,項目名稱:PyNDN2,代碼行數:28,代碼來源:tlv_0_1_1_wire_format.py

示例7: encodeData

    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)
開發者ID:WeiqiJust,項目名稱:NDN-total,代碼行數:35,代碼來源:tlv_0_1_wire_format.py

示例8: wireEncode

    def wireEncode(self):
        """
        Encode this Schedule.

        :return: The encoded buffer.
        :rtype: Blob
        """
        # For now, don't use WireFormat and hardcode to use TLV since the
        # encoding doesn't go out over the wire, only into the local SQL database.
        encoder = TlvEncoder(256)
        saveLength = len(encoder)

        # Encode backwards.
        # Encode the blackIntervalList.
        saveLengthForList = len(encoder)
        for i in range(len(self._blackIntervalList) - 1, -1, -1):
            Schedule._encodeRepetitiveInterval(self._blackIntervalList[i], encoder)
        encoder.writeTypeAndLength(Tlv.Encrypt_BlackIntervalList, len(encoder) - saveLengthForList)

        # Encode the whiteIntervalList.
        saveLengthForList = len(encoder)
        for i in range(len(self._whiteIntervalList) - 1, -1, -1):
            Schedule._encodeRepetitiveInterval(self._whiteIntervalList[i], encoder)
        encoder.writeTypeAndLength(Tlv.Encrypt_WhiteIntervalList, len(encoder) - saveLengthForList)

        encoder.writeTypeAndLength(Tlv.Encrypt_Schedule, len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
開發者ID:named-data,項目名稱:PyNDN2,代碼行數:28,代碼來源:schedule.py

示例9: encodeSignatureValue

    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)
開發者ID:MAHIS,項目名稱:PyNDN2,代碼行數:15,代碼來源:tlv_0_1_1_wire_format.py

示例10: generate

    def generate(self, interest, keyChain, certificateName, wireFormat = None):
        """
        Append a timestamp component and a random value component to interest's
        name. This ensures that the timestamp is greater than the timestamp used 
        in the previous call. Then use keyChain to sign the interest which 
        appends a SignatureInfo component and a component with the signature 
        bits. If the interest lifetime is not set, this sets it.

        :param Interest interest: The interest whose name is append with 
          components.
        :param KeyChain keyChain: The KeyChain for calling sign.
        :param Name certificateName: The certificate name of the key to use for 
          signing.
        :param wireFormat: (optional) A WireFormat object used to encode the 
          SignatureInfo and to encode interest name for signing. 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()

        timestamp =  round(Common.getNowMilliseconds())
        while timestamp <= self._lastTimestamp:
          timestamp += 1.0

        # The timestamp is encoded as a TLV nonNegativeInteger.
        encoder = TlvEncoder(8)
        encoder.writeNonNegativeInteger(int(timestamp))
        interest.getName().append(Blob(encoder.getOutput(), False))
        
        # The random value is a TLV nonNegativeInteger too, but we know it is 8 
        # bytes, so we don't need to call the nonNegativeInteger encoder.        
        randomBuffer = bytearray(8)
        for i in range(len(randomBuffer)):
            randomBuffer[i] = _systemRandom.randint(0, 0xff)                
        interest.getName().append(Blob(randomBuffer, False))

        keyChain.sign(interest, certificateName, wireFormat)

        if (interest.getInterestLifetimeMilliseconds() == None or
            interest.getInterestLifetimeMilliseconds()< 0):
          # The caller has not set the interest lifetime, so set it here.
          interest.setInterestLifetimeMilliseconds(1000.0)

        # We successfully signed the interest, so update the timestamp.
        self._lastTimestamp = timestamp
        
開發者ID:sanchitgupta05,項目名稱:PyNDN2,代碼行數:47,代碼來源:command_interest_generator.py

示例11: encode

    def encode(message):
        """
        Encode the Protobuf message object as NDN-TLV.

        :param google.protobuf.message message: The Protobuf message object.
          This calls message.IsInitialized() to ensure that all required fields
          are present and raises an exception if not.
        :return: The encoded buffer in a Blob object.
        :rtype: Blob
        """
        if not message.IsInitialized():
            raise RuntimeError("message is not initialized")
        encoder = TlvEncoder(256)

        ProtobufTlv._encodeMessageValue(message, encoder)
        return Blob(encoder.getOutput(), False)
開發者ID:MAHIS,項目名稱:PyNDN2,代碼行數:16,代碼來源:protobuf_tlv.py

示例12: _encodeLpNack

    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)
開發者ID:named-data,項目名稱:PyNDN2,代碼行數:41,代碼來源:node.py

示例13: encodeDelegationSet

    def encodeDelegationSet(self, delegationSet):
        """
        Encode the DelegationSet in NDN-TLV and return the encoding. Note that
        the sequence of Delegation does not have an outer TLV type and length
        because it is intended to use the type and length of a Data packet's
        Content.

        :param DelegationSet delegationSet: The DelegationSet object to
          encode.
        :return: A Blob containing the encoding.
        :rtype: Blob
        """
        encoder = TlvEncoder(256)

        # Encode backwards.
        for i in range(delegationSet.size() - 1, -1, -1):
            saveLength = len(encoder)

            Tlv0_1_1WireFormat._encodeName(delegationSet.get(i).getName(), encoder)
            encoder.writeNonNegativeIntegerTlv(
              Tlv.Link_Preference, delegationSet.get(i).getPreference())

            encoder.writeTypeAndLength(
              Tlv.Link_Delegation, len(encoder) - saveLength)

        return Blob(encoder.getOutput(), False)
開發者ID:MAHIS,項目名稱:PyNDN2,代碼行數:26,代碼來源:tlv_0_1_1_wire_format.py

示例14: _signInterest

    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)))
開發者ID:priestd09,項目名稱:PyNDN2,代碼行數:37,代碼來源:key_chain.py

示例15: prepareCommandInterestName

    def prepareCommandInterestName(self, interest, wireFormat = None):
        """
        Append a timestamp component and a random nonce component to interest's
        name. This ensures that the timestamp is greater than the timestamp used
        in the previous call.

        :param Interest interest: The interest whose name is append with
          components.
        :param WireFormat wireFormat: (optional) A WireFormat object used to
          encode the SignatureInfo. If omitted, use WireFormat
          getDefaultWireFormat().
        """
        if wireFormat == None:
            wireFormat = WireFormat.getDefaultWireFormat()

        # _nowOffsetMilliseconds is only used for testing.
        now = Common.getNowMilliseconds() + self._nowOffsetMilliseconds
        timestamp =  round(now)
        while timestamp <= self._lastUsedTimestamp:
          timestamp += 1.0

        # Update the timestamp now. In the small chance that signing fails, it
        # just means that we have bumped the timestamp.
        self._lastUsedTimestamp = timestamp

        # The timestamp is encoded as a TLV nonNegativeInteger.
        encoder = TlvEncoder(8)
        encoder.writeNonNegativeInteger(int(timestamp))
        interest.getName().append(Blob(encoder.getOutput(), False))

        # The random value is a TLV nonNegativeInteger too, but we know it is 8
        # bytes, so we don't need to call the nonNegativeInteger encoder.
        randomBuffer = bytearray(8)
        for i in range(len(randomBuffer)):
            randomBuffer[i] = _systemRandom.randint(0, 0xff)
        interest.getName().append(Blob(randomBuffer, False))
開發者ID:named-data,項目名稱:PyNDN2,代碼行數:36,代碼來源:command_interest_preparer.py


注:本文中的pyndn.encoding.tlv.tlv_encoder.TlvEncoder類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。