本文整理汇总了Python中cryptography.hazmat.primitives.asymmetric.dsa.generate_private_key方法的典型用法代码示例。如果您正苦于以下问题:Python dsa.generate_private_key方法的具体用法?Python dsa.generate_private_key怎么用?Python dsa.generate_private_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cryptography.hazmat.primitives.asymmetric.dsa
的用法示例。
在下文中一共展示了dsa.generate_private_key方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_key_pair
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def generate_key_pair(key_length):
private_key = rsa.generate_private_key(backend=default_backend(),
public_exponent=65537,
key_size=key_length)
private_key_der = private_key.private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption())
public_key_pem = private_key.public_key().public_bytes(
serialization.Encoding.PEM,
serialization.PublicFormat.SubjectPublicKeyInfo) \
.decode("utf-8")
# strip off header
public_key_der_encoded = ''.join(public_key_pem.split('\n')[1:-2])
return private_key_der, public_key_der_encoded
示例2: _generate
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def _generate(self, key_size):
if any(c != 0 for c in self): # pragma: no cover
raise PGPError("key is already populated")
# generate some big numbers!
pk = rsa.generate_private_key(65537, key_size, default_backend())
pkn = pk.private_numbers()
self.n = MPI(pkn.public_numbers.n)
self.e = MPI(pkn.public_numbers.e)
self.d = MPI(pkn.d)
self.p = MPI(pkn.p)
self.q = MPI(pkn.q)
# from the RFC:
# "- MPI of u, the multiplicative inverse of p, mod q."
# or, simply, p^-1 mod p
# rsa.rsa_crt_iqmp(p, q) normally computes q^-1 mod p,
# so if we swap the values around we get the answer we want
self.u = MPI(rsa.rsa_crt_iqmp(pkn.q, pkn.p))
del pkn
del pk
self._compute_chksum()
示例3: generate
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def generate(bits=1024, progress_func=None):
"""
Generate a new private DSS key. This factory function can be used to
generate a new host key or authentication key.
:param int bits: number of bits the generated key should be.
:param function progress_func: Unused
:return: new `.DSSKey` private key
"""
numbers = dsa.generate_private_key(
bits, backend=default_backend()
).private_numbers()
key = DSSKey(vals=(
numbers.public_numbers.parameter_numbers.p,
numbers.public_numbers.parameter_numbers.q,
numbers.public_numbers.parameter_numbers.g,
numbers.public_numbers.y
))
key.x = numbers.x
return key
### internals...
示例4: generate_rsa_private_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def generate_rsa_private_key(key_size=2048, public_exponent=65537):
"""
Generate RSA private key.
Args:
key_size (int): RSA key size
public_exponent (int): Key public exponent
Return:
rsa.RSAPrivateKey
"""
return rsa.generate_private_key(
public_exponent=public_exponent,
key_size=key_size,
backend=cryptography_default_backend
)
示例5: test_bad_private_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def test_bad_private_key(db_parameters):
db_config = {
'protocol': db_parameters['protocol'],
'account': db_parameters['account'],
'user': db_parameters['user'],
'host': db_parameters['host'],
'port': db_parameters['port'],
'database': db_parameters['database'],
'schema': db_parameters['schema'],
'timezone': 'UTC',
}
dsa_private_key = dsa.generate_private_key(key_size=2048,
backend=default_backend())
dsa_private_key_der = dsa_private_key.private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption())
encrypted_rsa_private_key_der = rsa.generate_private_key(key_size=2048,
public_exponent=65537,
backend=default_backend()) \
.private_bytes(encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.BestAvailableEncryption(
b'abcd'))
bad_private_key_test_cases = ["abcd", 1234, b'abcd', dsa_private_key_der,
encrypted_rsa_private_key_der]
for private_key in bad_private_key_test_cases:
db_config['private_key'] = private_key
with pytest.raises(
snowflake.connector.errors.ProgrammingError) as exec_info:
snowflake.connector.connect(**db_config)
assert (exec_info.value.errno == 251008)
示例6: gen_private_rsa
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def gen_private_rsa():
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048,
backend=default_backend())
pem = private_key.private_bytes(encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption())
return pem
示例7: gen_private_dsa
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def gen_private_dsa():
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import dsa
private_key = dsa.generate_private_key(key_size=1024, backend=default_backend())
pem = private_key.private_bytes(encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption())
return pem
示例8: generateRSAkey
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def generateRSAkey(options):
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
if not options['bits']:
options['bits'] = 1024
keyPrimitive = rsa.generate_private_key(
key_size=int(options['bits']),
public_exponent=65537,
backend=default_backend(),
)
key = keys.Key(keyPrimitive)
_saveKey(key, options)
示例9: generateDSAkey
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def generateDSAkey(options):
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import dsa
if not options['bits']:
options['bits'] = 1024
keyPrimitive = dsa.generate_private_key(
key_size=int(options['bits']),
backend=default_backend(),
)
key = keys.Key(keyPrimitive)
_saveKey(key, options)
示例10: generateECDSAkey
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def generateECDSAkey(options):
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import ec
if not options['bits']:
options['bits'] = 256
# OpenSSH supports only mandatory sections of RFC5656.
# See https://www.openssh.com/txt/release-5.7
curve = b'ecdsa-sha2-nistp' + str(options['bits']).encode('ascii')
keyPrimitive = ec.generate_private_key(
curve=keys._curveTable[curve],
backend=default_backend()
)
key = keys.Key(keyPrimitive)
_saveKey(key, options)
示例11: create_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def create_key(self):
key = dsa.generate_private_key(
backend=crypto_default_backend(), key_size=self.get("KeySize")
)
private_key = key.private_bytes(
crypto_serialization.Encoding.PEM,
crypto_serialization.PrivateFormat.PKCS8,
crypto_serialization.NoEncryption(),
)
public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.OpenSSH,
crypto_serialization.PublicFormat.OpenSSH,
)
return private_key.decode("ascii"), public_key.decode("ascii")
示例12: setUp
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def setUp(self):
self.example_xml_files = (os.path.join(os.path.dirname(__file__), "example.xml"),
os.path.join(os.path.dirname(__file__), "example2.xml"))
self.keys = dict(hmac=b"secret",
rsa=rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend()),
dsa=dsa.generate_private_key(key_size=1024, backend=default_backend()),
ecdsa=ec.generate_private_key(curve=ec.SECP384R1(), backend=default_backend()))
示例13: getRSAKeys
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def getRSAKeys():
"""
Checks for existing RSA Keys, if there are none, generates a 2048 bit
RSA key pair, saves them to a temporary location and returns the keys
formatted as OpenSSH keys.
"""
public_key = os.path.join(SSH_PATH, 'id_rsa.pub')
private_key = os.path.join(SSH_PATH, 'id_rsa')
if not (os.path.exists(public_key) and os.path.exists(private_key)):
ssh_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend())
public_key_string = ssh_key.public_key().public_bytes(
serialization.Encoding.OpenSSH,
serialization.PublicFormat.OpenSSH)
private_key_string = ssh_key.private_bytes(
serialization.Encoding.PEM,
serialization.PrivateFormat.TraditionalOpenSSL,
serialization.NoEncryption())
with open(public_key, 'w+b') as key_file:
key_file.write(public_key_string)
with open(private_key, 'w+b') as key_file:
key_file.write(private_key_string)
else:
with open(public_key) as key_file:
public_key_string = key_file.read()
with open(private_key) as key_file:
private_key_string = key_file.read()
return public_key_string, private_key_string
示例14: getDSAKeys
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def getDSAKeys():
"""
Checks for existing DSA Keys, if there are none, generates a 2048 bit
DSA key pair, saves them to a temporary location and returns the keys
formatted as OpenSSH keys.
"""
public_key = os.path.join(SSH_PATH, 'id_dsa.pub')
private_key = os.path.join(SSH_PATH, 'id_dsa')
if not (os.path.exists(public_key) and os.path.exists(private_key)):
ssh_key = dsa.generate_private_key(
key_size=2048,
backend=default_backend())
public_key_string = ssh_key.public_key().public_bytes(
serialization.Encoding.OpenSSH,
serialization.PublicFormat.OpenSSH)
private_key_string = ssh_key.private_bytes(
serialization.Encoding.PEM,
serialization.PrivateFormat.TraditionalOpenSSL,
serialization.NoEncryption())
with open(public_key, 'w+b') as key_file:
key_file.write(public_key_string)
with open(private_key, 'w+b') as key_file:
key_file.write(private_key_string)
else:
with open(public_key) as key_file:
public_key_string = key_file.read()
with open(private_key) as key_file:
private_key_string = key_file.read()
return public_key_string, private_key_string
示例15: generate_private_key
# 需要导入模块: from cryptography.hazmat.primitives.asymmetric import dsa [as 别名]
# 或者: from cryptography.hazmat.primitives.asymmetric.dsa import generate_private_key [as 别名]
def generate_private_key(key_size, key_type, ecc_curve):
"""Generate a private key.
This function assumes that you called :py:func:`~django_ca.utils.validate_key_parameters` on the input
values and does not do any sanity checks on its own.
Parameters
----------
key_size : int
The size of the private key (not used for ECC keys).
key_type : {'RSA', 'DSA', 'ECC'}
The type of the private key.
ecc_curve : :py:class:`~cg:cryptography.hazmat.primitives.asymmetric.ec.EllipticCurve`
The ECC curve to use for an ECC key.
Returns
-------
key
A private key of the appropriate type.
"""
if key_type == 'DSA':
private_key = dsa.generate_private_key(key_size=key_size, backend=default_backend())
elif key_type == 'ECC':
private_key = ec.generate_private_key(ecc_curve, default_backend())
else:
private_key = rsa.generate_private_key(public_exponent=65537, key_size=key_size,
backend=default_backend())
return private_key