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


Python pem.detect方法代码示例

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


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

示例1: test_dump_private

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def test_dump_private(self):
        def do_run():
            private = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test.key'))

            for password in [None, 'password123']:
                pem_serialized = asymmetric.dump_private_key(private, password, target_ms=20)
                private_reloaded = asymmetric.load_private_key(pem_serialized, password)
                self.assertTrue(pem.detect(pem_serialized))
                self.assertIsInstance(private_reloaded, asymmetric.PrivateKey)
                self.assertEqual('rsa', private_reloaded.algorithm)

        # OpenSSL 0.9.8 and Windows CryptoAPI don't have PBKDF2 implemented in
        # C, thus the dump operation fails since there is no reasonable way to
        # ensure we are using a good number of iterations of PBKDF2
        if openssl_098 or _backend == 'winlegacy':
            with self.assertRaises(OSError):
                do_run()
        else:
            do_run() 
开发者ID:wbond,项目名称:oscrypto,代码行数:21,代码来源:test_asymmetric.py

示例2: pem_to_der

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def pem_to_der(cert: bytes, return_multiple: bool = True):
    """Converts a given certificate or list to PEM format."""

    # initialize the certificate array
    cert_list = []

    # If certificate is in DER then un-armour it
    if pem.detect(cert):
        for _, _, der_bytes in pem.unarmor(cert, multiple=True):
            cert_list.append(der_bytes)
    else:
        cert_list.append(cert)

    # return multiple if return_multiple is set else first element
    if return_multiple:
        return cert_list
    else:
        return cert_list.pop() 
开发者ID:abhishek-ram,项目名称:pyas2-lib,代码行数:20,代码来源:utils.py

示例3: _grab_crl

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def _grab_crl(user_agent, url, timeout):
    """
    Fetches a CRL and parses it

    :param user_agent:
        A unicode string of the user agent to use when fetching the URL

    :param url:
        A unicode string of the URL to fetch the CRL from

    :param timeout:
        The number of seconds after which an HTTP request should timeout

    :return:
        An asn1crypto.crl.CertificateList object
    """
    request = Request(url)
    request.add_header('Accept', 'application/pkix-crl')
    request.add_header('User-Agent', user_agent)
    response = urlopen(request, None, timeout)
    data = response.read()
    if pem.detect(data):
        _, _, data = pem.unarmor(data)
    return crl.CertificateList.load(data) 
开发者ID:wbond,项目名称:certvalidator,代码行数:26,代码来源:crl_client.py

示例4: test_build_paths

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def test_build_paths(self):
        with open(os.path.join(fixtures_dir, 'mozilla.org.crt'), 'rb') as f:
            cert_bytes = f.read()
            if pem.detect(cert_bytes):
                _, _, cert_bytes = pem.unarmor(cert_bytes)
            cert = x509.Certificate.load(cert_bytes)

        with open(os.path.join(fixtures_dir, 'digicert-sha2-secure-server-ca.crt'), 'rb') as f:
            other_certs = [f.read()]

        repo = CertificateRegistry(other_certs=other_certs)
        paths = repo.build_paths(cert)
        self.assertEqual(1, len(paths))

        path = paths[0]
        self.assertEqual(3, len(path))
        self.assertEqual(
            [
                b'\x80Q\x06\x012\xad\x9a\xc2}Q\x87\xa0\xe8\x87\xfb\x01b\x01U\xee',
                b"\x10_\xa6z\x80\x08\x9d\xb5'\x9f5\xce\x83\x0bC\x88\x9e\xa3\xc7\r",
                b'I\xac\x03\xf8\xf3Km\xca)V)\xf2I\x9a\x98\xbe\x98\xdc.\x81'
            ],
            [item.subject.sha1 for item in path]
        ) 
开发者ID:wbond,项目名称:certvalidator,代码行数:26,代码来源:test_registry.py

示例5: test_get_path

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def test_get_path(self):
        trust_list.clear_cache()

        certs = trust_list.get_path()
        with open(certs, 'rb') as f:
            cert_data = f.read()
            self.assertEqual(True, pem.detect(cert_data))
            self.assertLess(10240, len(cert_data)) 
开发者ID:wbond,项目名称:oscrypto,代码行数:10,代码来源:test_trust_list.py

示例6: _grab_crl

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def _grab_crl(user_agent, url, timeout):
    """
    Fetches a CRL and parses it

    :param user_agent:
        A unicode string of the user agent to use when fetching the URL

    :param url:
        A unicode string of the URL to fetch the CRL from

    :param timeout:
        The number of seconds after which an HTTP request should timeout

    :return:
        An asn1crypto.crl.CertificateList object
    """

    if sys.version_info < (3,):
        url = util.iri_to_uri(url)
    request = Request(url)
    request.add_header(b'Accept', b'application/pkix-crl')
    request.add_header(b'User-Agent', user_agent.encode('iso-8859-1'))
    response = urlopen(request, None, timeout)
    data = response.read()
    if pem.detect(data):
        _, _, data = pem.unarmor(data)
    return crl.CertificateList.load(data) 
开发者ID:scalyr,项目名称:scalyr-agent-2,代码行数:29,代码来源:crl_client.py

示例7: _validate_unarmor

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def _validate_unarmor(self, certs, var_name):
        """
        Takes a list of byte strings or asn1crypto.x509.Certificates objects,
        validates and loads them while unarmoring any PEM-encoded contents

        :param certs:
            A list of byte strings or asn1crypto.x509.Certificate objects

        :param var_name:
            A unicode variable name to use in any TypeError exceptions

        :return:
            A list of asn1crypto.x509.Certificate objects
        """

        output = []
        for cert in certs:
            if isinstance(cert, x509.Certificate):
                output.append(cert)
            else:
                if not isinstance(cert, byte_cls):
                    raise TypeError(pretty_message(
                        '''
                        %s must contain only byte strings or
                        asn1crypto.x509.Certificate objects, not %s
                        ''',
                        var_name,
                        type_name(cert)
                    ))
                if pem.detect(cert):
                    _, _, cert = pem.unarmor(cert)
                output.append(x509.Certificate.load(cert))
        return output 
开发者ID:scalyr,项目名称:scalyr-agent-2,代码行数:35,代码来源:registry.py

示例8: prepend

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def prepend(self, cert):
        """
        Prepends a cert to the path. This should be the issuer of the previously
        prepended cert.

        :param cert:
            An asn1crypto.x509.Certificate object or a byte string

        :return:
            The current ValidationPath object, for chaining
        """

        if not isinstance(cert, x509.Certificate):
            if not isinstance(cert, byte_cls):
                raise TypeError(pretty_message(
                    '''
                    cert must be a byte string or an
                    asn1crypto.x509.Certificate object, not %s
                    ''',
                    type_name(cert)
                ))
            if pem.detect(cert):
                _, _, cert = pem.unarmor(cert)
            cert = x509.Certificate.load(cert)

        if cert.issuer_serial in self._cert_hashes:
            raise DuplicateCertificateError()

        self._cert_hashes.add(cert.issuer_serial)
        self._certs.insert(0, cert)

        return self 
开发者ID:scalyr,项目名称:scalyr-agent-2,代码行数:34,代码来源:path.py

示例9: test_fetch_ocsp

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def test_fetch_ocsp(self):
        with open(os.path.join(fixtures_dir, 'digicert-sha2-secure-server-ca.crt'), 'rb') as f:
            cert_bytes = f.read()
            if pem.detect(cert_bytes):
                _, _, cert_bytes = pem.unarmor(cert_bytes)
            intermediate = x509.Certificate.load(cert_bytes)

        registry = CertificateRegistry()
        path = registry.build_paths(intermediate)[0]
        issuer = path.find_issuer(intermediate)

        ocsp_response = ocsp_client.fetch(intermediate, issuer, timeout=3)
        context = ValidationContext(ocsps=[ocsp_response])
        verify_ocsp_response(intermediate, path, context) 
开发者ID:wbond,项目名称:certvalidator,代码行数:16,代码来源:test_ocsp_client.py

示例10: test_fetch_crl

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def test_fetch_crl(self):
        with open(os.path.join(fixtures_dir, 'digicert-sha2-secure-server-ca.crt'), 'rb') as f:
            file_bytes = f.read()
            if pem.detect(file_bytes):
                _, _, file_bytes = pem.unarmor(file_bytes)
            intermediate = x509.Certificate.load(file_bytes)

        crls = crl_client.fetch(intermediate, timeout=3)
        context = ValidationContext(crls=crls)
        registry = context.certificate_registry
        path = registry.build_paths(intermediate)[0]

        verify_crl(intermediate, path, context) 
开发者ID:wbond,项目名称:certvalidator,代码行数:15,代码来源:test_crl_client.py

示例11: _load_cert_object

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def _load_cert_object(self, *path_components):
        with open(os.path.join(fixtures_dir, *path_components), 'rb') as f:
            cert_bytes = f.read()
            if pem.detect(cert_bytes):
                _, _, cert_bytes = pem.unarmor(cert_bytes)
            cert = x509.Certificate.load(cert_bytes)
        return cert 
开发者ID:wbond,项目名称:certvalidator,代码行数:9,代码来源:test_certificate_validator.py

示例12: _load_cert

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def _load_cert(self, relative_path):
        with open(os.path.join(fixtures_dir, relative_path), 'rb') as f:
            cert_bytes = f.read()
            if pem.detect(cert_bytes):
                _, _, cert_bytes = pem.unarmor(cert_bytes)
            return x509.Certificate.load(cert_bytes) 
开发者ID:wbond,项目名称:asn1crypto,代码行数:8,代码来源:test_x509.py

示例13: test_trusted_certificate

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def test_trusted_certificate(self):
        with open(os.path.join(fixtures_dir, 'sender_dummycorp.com.crt'), 'rb') as f:
            cert_bytes = f.read()
            if pem.detect(cert_bytes):
                _, _, cert_bytes = pem.unarmor(cert_bytes)
            trusted_cert = x509.TrustedCertificate.load(cert_bytes)

        cert = trusted_cert[0]
        aux = trusted_cert[1]

        self.assertEqual(
            cert.subject.native,
            util.OrderedDict([
                ('country_name', 'US'),
                ('state_or_province_name', 'VA'),
                ('locality_name', 'Herndon'),
                ('organization_name', 'Internet Gadgets Pty Ltd'),
                ('common_name', 'Fake Sender'),
                ('email_address', 'sender@dummycorp.com'),
            ])
        )

        self.assertEqual(
            aux['trust'].native,
            ['email_protection']
        )

        self.assertEqual(
            aux['reject'].native,
            ['client_auth', 'server_auth']
        ) 
开发者ID:wbond,项目名称:asn1crypto,代码行数:33,代码来源:test_x509.py

示例14: test_iri_with_port

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def test_iri_with_port(self):
        with open(os.path.join(fixtures_dir, 'admin.ch.crt'), 'rb') as f:
            cert_bytes = f.read()
            if pem.detect(cert_bytes):
                _, _, cert_bytes = pem.unarmor(cert_bytes)
            cert = x509.Certificate.load(cert_bytes)

        self.assertEqual(
            [dp.native for dp in cert.crl_distribution_points],
            [
                util.OrderedDict([
                    ('distribution_point', ['http://www.pki.admin.ch/crl/SSLCA01.crl']),
                    ('reasons', None),
                    ('crl_issuer', None)
                ]),
                util.OrderedDict([
                    (
                        'distribution_point',
                        [
                            'ldap://admindir.admin.ch:389/'
                            'cn=Swiss Government SSL CA 01,'
                            'ou=Certification Authorities,'
                            'ou=Services,'
                            'o=Admin,'
                            'c=CH'
                        ]
                    ),
                    ('reasons', None),
                    ('crl_issuer', None)
                ])
            ]
        ) 
开发者ID:wbond,项目名称:asn1crypto,代码行数:34,代码来源:test_x509.py

示例15: detect

# 需要导入模块: from asn1crypto import pem [as 别名]
# 或者: from asn1crypto.pem import detect [as 别名]
def detect(self, relative_path, is_pem):
        with open(os.path.join(fixtures_dir, relative_path), 'rb') as f:
            byte_string = f.read()
        self.assertEqual(is_pem, pem.detect(byte_string)) 
开发者ID:wbond,项目名称:asn1crypto,代码行数:6,代码来源:test_pem.py


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