当前位置: 首页>>代码示例>>Python>>正文


Python KeyChain.createIdentityV2方法代码示例

本文整理汇总了Python中pyndn.security.KeyChain.createIdentityV2方法的典型用法代码示例。如果您正苦于以下问题:Python KeyChain.createIdentityV2方法的具体用法?Python KeyChain.createIdentityV2怎么用?Python KeyChain.createIdentityV2使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyndn.security.KeyChain的用法示例。


在下文中一共展示了KeyChain.createIdentityV2方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: IdentityManagementFixture

# 需要导入模块: from pyndn.security import KeyChain [as 别名]
# 或者: from pyndn.security.KeyChain import createIdentityV2 [as 别名]
class IdentityManagementFixture(object):
    def __init__(self):
        self._keyChain = KeyChain("pib-memory:", "tpm-memory:")
        self._identityNames = set()
        self._certificateFiles = set()

    def saveCertificateToFile(self, data, filePath):
        """
        :param Data data: The certificate to save.
        :param str filePath: The file path, which should be writable.
        :return: True if successful.
        :rtype: bool
        """
        self._certificateFiles.add(filePath)

        try:
            encoding = data.wireEncode()
            encodedCertificate = Common.base64Encode(encoding.toBytes(), True)

            with open(filePath, 'w') as keyFile:
                keyFile.write(encodedCertificate)

            return True
        except Exception:
            return False

    def addIdentity(self, identityName, params = None):
        """
        Add an identity for the identityName.

        :param Name identityName: The name of the identity.
        :param KeyParams params: (optional) The key parameters if a key needs to
          be generated for the identity. If omitted, use
          KeyChain.getDefaultKeyParams().
        :return: The created PibIdentity instance.
        :rtype: PibIdentity
        """
        if params == None:
            params = KeyChain.getDefaultKeyParams()

        identity = self._keyChain.createIdentityV2(identityName, params)
        self._identityNames.add(identityName)
        return identity

    def saveCertificate(identity, filePath):
        """
        Save the identity's certificate to a file.

        :param PibIdentity identity: The PibIdentity.
        :param str filePath: The file path, which should be writable.
        :return: True if successful.
        :rtype: str
        """
        try:
            certificate = identity.getDefaultKey().getDefaultCertificate()
            return self.saveCertificateToFile(certificate, filePath)
        except Pib.Error:
            return False

    def addSubCertificate(self, subIdentityName, issuer, params = None):
        """
        Issue a certificate for subIdentityName signed by issuer. If the
        identity does not exist, it is created. A new key is generated as the
        default key for the identity. A default certificate for the key is
        signed by the issuer using its default certificate.
        """
        if params == None:
            params = KeyChain.getDefaultKeyParams()

        subIdentity = self.addIdentity(subIdentityName, params)

        request = subIdentity.getDefaultKey().getDefaultCertificate()

        request.setName(request.getKeyName().append("parent").appendVersion(1))

        certificateParams = SigningInfo(issuer)
        # Validity period of 20 years.
        now = Common.getNowMilliseconds()
        certificateParams.setValidityPeriod(
          ValidityPeriod(now, now + 20 * 365 * 24 * 3600 * 1000.0))

        # Skip the AdditionalDescription.

        self._keyChain.sign(request, certificateParams)
        self._keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request)

        return subIdentity

    def addCertificate(self, key, issuerId):
        """
        Add a self-signed certificate made from the key and issuer ID.

        :param PibKey key: The key for the certificate.
        :param str issuerId: The issuer ID name component for the certificate
          name.
        :return: The new certificate.
        :rtype: CertificateV2
        """
        certificateName = Name(key.getName())
        certificateName.append(issuerId).appendVersion(3)
#.........这里部分代码省略.........
开发者ID:named-data,项目名称:PyNDN2,代码行数:103,代码来源:identity_management_fixture.py


注:本文中的pyndn.security.KeyChain.createIdentityV2方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。