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


Python asymmetric.load_private_key方法代碼示例

本文整理匯總了Python中oscrypto.asymmetric.load_private_key方法的典型用法代碼示例。如果您正苦於以下問題:Python asymmetric.load_private_key方法的具體用法?Python asymmetric.load_private_key怎麽用?Python asymmetric.load_private_key使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在oscrypto.asymmetric的用法示例。


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

示例1: test_dump_private

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [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: test_rsa_generate

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_rsa_generate(self):
        public, private = asymmetric.generate_pair('rsa', bit_size=2048)

        self.assertEqual('rsa', public.algorithm)
        self.assertEqual(2048, public.bit_size)

        original_data = b'This is data to sign'
        signature = asymmetric.rsa_pkcs1v15_sign(private, original_data, 'sha1')
        self.assertIsInstance(signature, byte_cls)
        asymmetric.rsa_pkcs1v15_verify(public, signature, original_data, 'sha1')

        raw_public = asymmetric.dump_public_key(public)
        asymmetric.load_public_key(raw_public)
        raw_private = asymmetric.dump_private_key(private, None)
        asymmetric.load_private_key(raw_private, None)

        self.assertIsInstance(private.fingerprint, byte_cls)
        self.assertIsInstance(public.fingerprint, byte_cls)
        self.assertEqual(private.fingerprint, public.fingerprint) 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:21,代碼來源:test_asymmetric.py

示例3: test_ec_generate

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_ec_generate(self):
        public, private = asymmetric.generate_pair('ec', curve='secp256r1')

        self.assertEqual('ec', public.algorithm)
        self.assertEqual('secp256r1', public.asn1.curve[1])

        original_data = b'This is data to sign'
        signature = asymmetric.ecdsa_sign(private, original_data, 'sha1')
        self.assertIsInstance(signature, byte_cls)
        asymmetric.ecdsa_verify(public, signature, original_data, 'sha1')

        raw_public = asymmetric.dump_public_key(public)
        asymmetric.load_public_key(raw_public)
        raw_private = asymmetric.dump_private_key(private, None)
        asymmetric.load_private_key(raw_private, None)

        self.assertIsInstance(private.fingerprint, byte_cls)
        self.assertIsInstance(public.fingerprint, byte_cls)
        self.assertEqual(private.fingerprint, public.fingerprint) 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:21,代碼來源:test_asymmetric.py

示例4: test_dsa_2048_sha1_sign

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_dsa_2048_sha1_sign(self):
        def do_run():
            original_data = b'This is data to sign'
            private = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test-dsa-2048.key'))
            public = asymmetric.load_public_key(os.path.join(fixtures_dir, 'keys/test-dsa-2048.crt'))

            signature = asymmetric.dsa_sign(private, original_data, 'sha1')
            self.assertIsInstance(signature, byte_cls)

            asymmetric.dsa_verify(public, signature, original_data, 'sha1')

        if sys.platform == 'win32':
            with self.assertRaises(errors.AsymmetricKeyError):
                do_run()
        else:
            do_run() 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:18,代碼來源:test_asymmetric.py

示例5: test_dsa_3072_sign

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_dsa_3072_sign(self):
        def do_run():
            original_data = b'This is data to sign'
            private = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test-dsa.key'))
            public = asymmetric.load_public_key(os.path.join(fixtures_dir, 'keys/test-dsa.crt'))

            signature = asymmetric.dsa_sign(private, original_data, 'sha256')
            self.assertIsInstance(signature, byte_cls)

            asymmetric.dsa_verify(public, signature, original_data, 'sha256')

        if not _should_support_sha2():
            with self.assertRaises(errors.AsymmetricKeyError):
                do_run()
        else:
            do_run() 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:18,代碼來源:test_asymmetric.py

示例6: test_dsa_3072_sign_sha1

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_dsa_3072_sign_sha1(self):
        def do_run():
            original_data = b'This is data to sign'
            private = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test-dsa.key'))
            public = asymmetric.load_public_key(os.path.join(fixtures_dir, 'keys/test-dsa.crt'))

            signature = asymmetric.dsa_sign(private, original_data, 'sha1')
            self.assertIsInstance(signature, byte_cls)

            asymmetric.dsa_verify(public, signature, original_data, 'sha1')

        if _backend == 'mac' or openssl_098 or _backend == 'winlegacy':
            with self.assertRaises(errors.AsymmetricKeyError):
                do_run()
        elif _backend == 'win':
            if _win_version_pair() < (6, 2):
                exception_class = errors.AsymmetricKeyError
            else:
                exception_class = ValueError
            with self.assertRaises(exception_class):
                do_run()
        else:
            do_run() 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:25,代碼來源:test_asymmetric.py

示例7: test_build_no_certificate

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_build_no_certificate(self):
        issuer_key = asymmetric.load_private_key(os.path.join(fixtures_dir, 'test.key'))
        issuer_cert = asymmetric.load_certificate(os.path.join(fixtures_dir, 'test.crt'))
        subject_cert = asymmetric.load_certificate(os.path.join(fixtures_dir, 'test-inter.crt'))

        with self.assertRaisesRegex(ValueError, 'must be set if the response_status is "successful"'):
            builder = OCSPResponseBuilder('successful', subject_cert, 'good')
            builder.certificate = None
            builder.build(issuer_key, issuer_cert)

        with self.assertRaisesRegex(ValueError, 'must be set if the response_status is "successful"'):
            builder = OCSPResponseBuilder('successful', subject_cert, 'good')
            builder.certificate_status = None
            builder.build(issuer_key, issuer_cert)

        with self.assertRaisesRegex(ValueError, 'must be set if the response_status is "successful"'):
            builder = OCSPResponseBuilder('successful', subject_cert)
            builder.build(issuer_key, issuer_cert)

        with self.assertRaisesRegex(ValueError, 'must be set if the response_status is "successful"'):
            builder = OCSPResponseBuilder('successful', None, 'good')
            builder.build(issuer_key, issuer_cert) 
開發者ID:wbond,項目名稱:ocspbuilder,代碼行數:24,代碼來源:test_ocsp_response_builder.py

示例8: test_build_revoked_no_reason

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_build_revoked_no_reason(self):
        issuer_key = asymmetric.load_private_key(os.path.join(fixtures_dir, 'test.key'))
        issuer_cert = asymmetric.load_certificate(os.path.join(fixtures_dir, 'test.crt'))
        subject_cert = asymmetric.load_certificate(os.path.join(fixtures_dir, 'test-inter.crt'))

        revoked_time = datetime(2015, 9, 1, 12, 0, 0, tzinfo=timezone.utc)
        builder = OCSPResponseBuilder('successful', subject_cert, 'revoked', revoked_time)
        ocsp_response = builder.build(issuer_key, issuer_cert)
        der_bytes = ocsp_response.dump()

        new_response = asn1crypto.ocsp.OCSPResponse.load(der_bytes)
        basic_response = new_response['response_bytes']['response'].parsed
        response_data = basic_response['tbs_response_data']
        cert_response = response_data['responses'][0]

        self.assertEqual('revoked', cert_response['cert_status'].name)
        self.assertEqual(revoked_time, cert_response['cert_status'].chosen['revocation_time'].native)
        self.assertEqual('unspecified', cert_response['cert_status'].chosen['revocation_reason'].native) 
開發者ID:wbond,項目名稱:ocspbuilder,代碼行數:20,代碼來源:test_ocsp_response_builder.py

示例9: test_cert_get

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_cert_get(self):
        ca = self.cas['child']
        cert = self.certs['child-cert']

        priv_path, cert_path, ocsp_cert = ca.generate_ocsp_key()
        self.ocsp_private_key = asymmetric.load_private_key(ca_storage.path(priv_path))

        url = reverse('django_ca:ocsp-cert-get', kwargs={
            'serial': self.cas['child'].serial,
            'data': base64.b64encode(req1).decode('utf-8'),
        })
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        # URL config sets expires to 3600
        self.assertOCSP(response, requested=[cert], nonce=req1_nonce, ocsp_cert=ocsp_cert, expires=3600)

        priv_path, cert_path, ocsp_cert = ca.generate_ocsp_key(key_size=1024)
        self.ocsp_private_key = asymmetric.load_private_key(ca_storage.path(priv_path))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        # URL config sets expires to 3600
        self.assertOCSP(response, requested=[cert], nonce=req1_nonce, ocsp_cert=ocsp_cert, expires=3600) 
開發者ID:mathiasertl,項目名稱:django-ca,代碼行數:26,代碼來源:tests_views_ocsp.py

示例10: test_private_key_attributes

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_private_key_attributes(self):
        private_key = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test.key'))
        self.assertEqual(2048, private_key.bit_size)
        self.assertEqual(256, private_key.byte_size)
        self.assertEqual('rsa', private_key.algorithm) 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:7,代碼來源:test_asymmetric.py

示例11: test_private_key_ec_attributes

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_private_key_ec_attributes(self):
        private_key = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test-ec-named.key'))
        self.assertEqual(256, private_key.bit_size)
        self.assertEqual(32, private_key.byte_size)
        self.assertEqual('secp256r1', private_key.curve)
        self.assertEqual('ec', private_key.algorithm) 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:8,代碼來源:test_asymmetric.py

示例12: test_dump_private_openssl

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_dump_private_openssl(self):
        private = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test.key'))
        pem_serialized = asymmetric.dump_openssl_private_key(private, 'password123')
        private_reloaded = asymmetric.load_private_key(pem_serialized, 'password123')
        self.assertIsInstance(private_reloaded, asymmetric.PrivateKey)
        self.assertEqual('rsa', private_reloaded.algorithm) 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:8,代碼來源:test_asymmetric.py

示例13: test_rsa_pkcs1v15_encrypt

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_rsa_pkcs1v15_encrypt(self):
        original_data = b'This is data to encrypt'
        private = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test.key'))
        public = asymmetric.load_public_key(os.path.join(fixtures_dir, 'keys/test.crt'))

        ciphertext = asymmetric.rsa_pkcs1v15_encrypt(public, original_data)
        self.assertIsInstance(ciphertext, byte_cls)

        plaintext = asymmetric.rsa_pkcs1v15_decrypt(private, ciphertext)
        self.assertEqual(original_data, plaintext) 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:12,代碼來源:test_asymmetric.py

示例14: test_rsa_oaep_encrypt

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_rsa_oaep_encrypt(self):
        original_data = b'This is data to encrypt'
        private = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test.key'))
        public = asymmetric.load_public_key(os.path.join(fixtures_dir, 'keys/test.crt'))

        ciphertext = asymmetric.rsa_oaep_encrypt(public, original_data)
        self.assertIsInstance(ciphertext, byte_cls)

        plaintext = asymmetric.rsa_oaep_decrypt(private, ciphertext)
        self.assertEqual(original_data, plaintext) 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:12,代碼來源:test_asymmetric.py

示例15: test_rsa_private_pkcs1v15_decrypt

# 需要導入模塊: from oscrypto import asymmetric [as 別名]
# 或者: from oscrypto.asymmetric import load_private_key [as 別名]
def test_rsa_private_pkcs1v15_decrypt(self):
        original_data = b'This is the message to sign'
        private = asymmetric.load_private_key(os.path.join(fixtures_dir, 'keys/test.key'))

        with open(os.path.join(fixtures_dir, 'rsa_public_encrypted'), 'rb') as f:
            plaintext = asymmetric.rsa_pkcs1v15_decrypt(private, f.read())
            self.assertEqual(original_data, plaintext) 
開發者ID:wbond,項目名稱:oscrypto,代碼行數:9,代碼來源:test_asymmetric.py


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