本文整理汇总了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()
示例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)
示例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)
示例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()
示例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()
示例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()
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)