本文整理汇总了Python中pyndn.encoding.wire_format.WireFormat.getDefaultWireFormat方法的典型用法代码示例。如果您正苦于以下问题:Python WireFormat.getDefaultWireFormat方法的具体用法?Python WireFormat.getDefaultWireFormat怎么用?Python WireFormat.getDefaultWireFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyndn.encoding.wire_format.WireFormat
的用法示例。
在下文中一共展示了WireFormat.getDefaultWireFormat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wireDecode
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def wireDecode(self, input, wireFormat = None):
"""
Decode the input using a particular wire format and update this Data.
If wireFormat is the default wire format, also set the
defaultWireEncoding to another pointer to the input.
:param input: The array with the bytes to decode. If input is not a
Blob, then copy the bytes to save the defaultWireEncoding (otherwise
take another pointer to the same Blob).
:type input: A Blob or an array type with int elements
:param wireFormat: (optional) A WireFormat object used to decode this
Data object. 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()
# If input is a blob, get its buf().
decodeBuffer = input.buf() if isinstance(input, Blob) else input
(signedPortionBeginOffset, signedPortionEndOffset) = \
wireFormat.decodeData(self, decodeBuffer)
if wireFormat == WireFormat.getDefaultWireFormat():
# This is the default wire encoding. In the Blob constructor, set
# copy true, but if input is already a Blob, it won't copy.
self._setDefaultWireEncoding(SignedBlob(
Blob(input, True),
signedPortionBeginOffset, signedPortionEndOffset),
WireFormat.getDefaultWireFormat())
else:
self._setDefaultWireEncoding(SignedBlob(), None)
示例2: wireEncode
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def wireEncode(self, wireFormat = None):
"""
Encode this Data for a particular wire format. If wireFormat is the
default wire format, also set the defaultWireEncoding field to the
encoded result.
:param wireFormat: (optional) A WireFormat object used to encode this
Data object. If omitted, use WireFormat.getDefaultWireFormat().
:type wireFormat: A subclass of WireFormat
:return: The encoded buffer in a SignedBlob object.
:rtype: SignedBlob
"""
if wireFormat == None:
# Don't use a default argument since getDefaultWireFormat can change.
wireFormat = WireFormat.getDefaultWireFormat()
if (not self.getDefaultWireEncoding().isNull() and
self.getDefaultWireEncodingFormat() == wireFormat):
# We already have an encoding in the desired format.
return self.getDefaultWireEncoding()
(encoding, signedPortionBeginOffset, signedPortionEndOffset) = \
wireFormat.encodeData(self)
wireEncoding = SignedBlob(
encoding, signedPortionBeginOffset, signedPortionEndOffset)
if wireFormat == WireFormat.getDefaultWireFormat():
# This is the default wire encoding.
self._setDefaultWireEncoding(
wireEncoding, WireFormat.getDefaultWireFormat())
return wireEncoding
示例3: registerPrefix
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def registerPrefix(
self, prefix, onInterest, onRegisterFailed, flags = None,
wireFormat = None):
"""
Register prefix with the connected NDN hub and call onInterest when a
matching interest is received.
:param Name prefix: The Name for the prefix to register which is NOT
copied for this internal Node method. The Face registerPrefix is
reponsible for making a copy for Node to use..
:param onInterest: A function object to call when a matching interest is
received.
:type onInterest: function object
:param onRegisterFailed: A function object to call if failed to retrieve
the connected hub's ID or failed to register the prefix.
:type onRegisterFailed: function object
:param ForwardingFlags flags: The flags for finer control of which
interests are forwardedto the application.
:param wireFormat: A WireFormat object used to encode the message.
:type wireFormat: a subclass of WireFormat
"""
if flags == None:
flags = ForwardingFlags()
if wireFormat == None:
# Don't use a default argument since getDefaultWireFormat can change.
wireFormat = WireFormat.getDefaultWireFormat()
# Node.expressInterest requires a copy of the prefix.
self._node.registerPrefix(
prefix, onInterest, onRegisterFailed, flags, wireFormat)
示例4: generate
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
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()
self.prepareCommandInterestName(interest, wireFormat)
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)
示例5: verifyInterest
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def verifyInterest(
self, interest, onVerified, onVerifyFailed, stepCount = 0,
wireFormat = None):
"""
Check the signature on the signed interest and call either onVerify or
onVerifyFailed. We use callback functions because verify may fetch
information to check the signature.
:param Interest interest: The interest with the signature to check.
:param onVerified: If the signature is verified, this calls
onVerified(interest).
:type onVerified: function object
:param onVerifyFailed: If the signature check fails or can't find the
public key, this calls onVerifyFailed(interest).
:type onVerifyFailed: function object
:param int stepCount: (optional) The number of verification steps that
have been done. If omitted, use 0.
"""
if wireFormat == None:
# Don't use a default argument since getDefaultWireFormat can change.
wireFormat = WireFormat.getDefaultWireFormat()
if self._policyManager.requireVerify(interest):
nextStep = self._policyManager.checkVerificationPolicy(
interest, stepCount, onVerified, onVerifyFailed, wireFormat)
if nextStep != None:
self._face.expressInterest(
nextStep.interest, self._makeOnCertificateData(nextStep),
self._makeOnCertificateInterestTimeout(
nextStep.retry, onVerifyFailed, interest, nextStep))
elif self._policyManager.skipVerifyAndTrust(interest):
onVerified(interest)
else:
onVerifyFailed(interest)
示例6: signWithSha256
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def signWithSha256(self, data, wireFormat = None):
"""
Wire encode the Data object, digest it and set its SignatureInfo to a
DigestSha256.
:param Data data: The Data object to be signed. This updates its
signature and wireEncoding.
: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()
data.setSignature(DigestSha256Signature())
# Encode once to get the signed portion.
encoding = data.wireEncode(wireFormat)
# Get the bytes to sign.
signedPortion = encoding.toSignedBuffer()
if sys.version_info[0] == 2:
# In Python 2.x, we need a str. Use Blob to convert signedPortion.
signedPortion = Blob(signedPortion, False).toRawStr()
# Digest and set the signature.
data.getSignature().setSignature(Blob(SHA256.new(signedPortion).digest()))
# Encode again to include the signature.
data.wireEncode(wireFormat)
示例7: _signInterest
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [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.
interest.setName(interest.getName().getPrefix(-1).append(
wireFormat.encodeSignatureValue(signedSignature)))
示例8: verifyInterestSignature
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def verifyInterestSignature(
interest, publicKeyOrCertificate, digestAlgorithm = None, wireFormat = None):
"""
Verify the Interest packet using the public key, where the last two name
components are the SignatureInfo and signature bytes. This does not
check the type of public key or digest algorithm against the type of
SignatureInfo such as Sha256WithRsaSignature.
:param Interest interest: The Interest packet to verify.
:param publicKeyOrCertificate: The object containing the public key, or
the public key DER which is used to make the PublicKey object, or the
certificate containing the public key.
:type publicKeyOrCertificate: Blob, or an object which is the same as
the bytes() operator, or CertificateV2
:param digestAlgorithm: (optional) The digest algorithm. If omitted, use
DigestAlgorithm.SHA256.
:param WireFormat wireFormat: (optional) A WireFormat object used to
encode the Interest packet. If omitted, use
WireFormat.getDefaultWireFormat().
:raises: ValueError for an invalid public key type or digestAlgorithm.
"""
arg3 = digestAlgorithm
arg4 = wireFormat
if type(arg3) is int:
digestAlgorithm = arg3
else:
digestAlgorithm = None
if isinstance(arg3, WireFormat):
wireFormat = arg3
elif isinstance(arg4, WireFormat):
wireFormat = arg4
else:
wireFormat = None
if isinstance(publicKeyOrCertificate, CertificateV2):
try:
publicKey = publicKeyOrCertificate.getPublicKey()
except:
return False
else:
publicKey = publicKeyOrCertificate;
if wireFormat == None:
wireFormat = WireFormat.getDefaultWireFormat()
signature = VerificationHelpers._extractSignature(interest, wireFormat)
if signature == None:
return False
encoding = interest.wireEncode(wireFormat)
return VerificationHelpers.verifySignature(
encoding.toSignedBytes(), signature.getSignature(), publicKey,
digestAlgorithm)
示例9: wireEncodeV2
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def wireEncodeV2(self, wireFormat = None):
"""
Encode this to an EncryptedContent v2 for a particular wire format.
:param wireFormat: (optional) A WireFormat object used to encode this
EncryptedContent. If omitted, use WireFormat.getDefaultWireFormat().
:type wireFormat: A subclass of WireFormat
:return: The encoded buffer.
:rtype: Blob
"""
if wireFormat == None:
# Don't use a default argument since getDefaultWireFormat can change.
wireFormat = WireFormat.getDefaultWireFormat()
return wireFormat.encodeEncryptedContentV2(self)
示例10: wireEncode
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def wireEncode(self, wireFormat = None):
"""
Encode this ControlResponse for a particular wire format.
:param wireFormat: (optional) A WireFormat object used to encode this
ControlParameters. If omitted, use WireFormat.getDefaultWireFormat().
:type wireFormat: A subclass of WireFormat
:return: The encoded buffer.
:rtype: Blob
"""
if wireFormat == None:
# Don't use a default argument since getDefaultWireFormat can change.
wireFormat = WireFormat.getDefaultWireFormat()
return wireFormat.encodeControlResponse(self)
示例11: generate
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
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
示例12: checkVerificationPolicy
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def checkVerificationPolicy(self, dataOrInterest, stepCount, onVerified, onVerifyFailed, wireFormat=None):
"""
Look in the IdentityStorage for the public key with the name in the
KeyLocator (if available) and use it to verify the data packet or
signed interest. If the public key can't be found, call onVerifyFailed.
:param dataOrInterest: The Data object or interest with the signature to
check.
:type dataOrInterest: Data or Interest
:param int stepCount: The number of verification steps that have been
done, used to track the verification progress.
:param onVerified: If the signature is verified, this calls
onVerified(dataOrInterest).
:type onVerified: function object
:param onVerifyFailed: If the signature check fails, this calls
onVerifyFailed(dataOrInterest).
:type onVerifyFailed: function object
:return: None for no further step for looking up a certificate chain.
:rtype: ValidationRequest
"""
if wireFormat == None:
# Don't use a default argument since getDefaultWireFormat can change.
wireFormat = WireFormat.getDefaultWireFormat()
if isinstance(dataOrInterest, Data):
data = dataOrInterest
# wireEncode returns the cached encoding if available.
if self._verify(data.getSignature(), data.wireEncode()):
onVerified(data)
else:
onVerifyFailed(data)
elif isinstance(dataOrInterest, Interest):
interest = dataOrInterest
# Decode the last two name components of the signed interest
signature = wireFormat.decodeSignatureInfoAndValue(
interest.getName().get(-2).getValue().buf(), interest.getName().get(-1).getValue().buf()
)
# wireEncode returns the cached encoding if available.
if self._verify(signature, interest.wireEncode()):
onVerified(interest)
else:
onVerifyFailed(interest)
else:
raise RuntimeError("checkVerificationPolicy: unrecognized type for dataOrInterest")
# No more steps, so return a None.
return None
示例13: putData
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def putData(self, data, wireFormat = None):
"""
The OnInterest callback calls this to put a Data packet which satisfies
an Interest.
:param Data data: The Data packet which satisfies the interest.
:param WireFormat wireFormat: (optional) A WireFormat object used to
encode the Data packet. If omitted, use
WireFormat.getDefaultWireFormat().
:throws: RuntimeError If the encoded Data packet size exceeds
getMaxNdnPacketSize().
"""
if wireFormat == None:
# Don't use a default argument since getDefaultWireFormat can change.
wireFormat = WireFormat.getDefaultWireFormat()
self._node.putData(data, wireFormat)
示例14: _registerPrefixHelper
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def _registerPrefixHelper(
self, registeredPrefixId, prefixCopy, onInterest, onRegisterFailed,
flags = None, wireFormat = None):
"""
This is a protected helper method to do the work of registerPrefix to
resolve the different overloaded forms. The registeredPrefixId is from
getNextEntryId(). This has no return value and can be used in a callback.
"""
if flags == None:
flags = ForwardingFlags()
if wireFormat == None:
# Don't use a default argument since getDefaultWireFormat can change.
wireFormat = WireFormat.getDefaultWireFormat()
return self._node.registerPrefix(
registeredPrefixId, prefixCopy, onInterest, onRegisterFailed, flags,
wireFormat, self._commandKeyChain, self._commandCertificateName, self)
示例15: wireDecode
# 需要导入模块: from pyndn.encoding.wire_format import WireFormat [as 别名]
# 或者: from pyndn.encoding.wire_format.WireFormat import getDefaultWireFormat [as 别名]
def wireDecode(self, input, wireFormat = None):
"""
Decode the input using a particular wire format and update this
ControlResponse.
:param input: The array with the bytes to decode.
:type input: An array type with int elements
:param wireFormat: (optional) A WireFormat object used to decode this
ControlParameters. 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()
# If input is a blob, get its buf().
decodeBuffer = input.buf() if isinstance(input, Blob) else input
wireFormat.decodeControlResponse(self, decodeBuffer)