本文整理匯總了Python中Crypto.PublicKey.DSA.generate方法的典型用法代碼示例。如果您正苦於以下問題:Python DSA.generate方法的具體用法?Python DSA.generate怎麽用?Python DSA.generate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Crypto.PublicKey.DSA
的用法示例。
在下文中一共展示了DSA.generate方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sign_ssh_data
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def sign_ssh_data(self, rng, data):
digest = SHA.new(data).digest()
dss = DSA.construct((long(self.y), long(self.g), long(self.p), long(self.q), long(self.x)))
# generate a suitable k
qsize = len(util.deflate_long(self.q, 0))
while True:
k = util.inflate_long(rng.read(qsize), 1)
if (k > 2) and (k < self.q):
break
r, s = dss.sign(util.inflate_long(digest, 1), k)
m = Message()
m.add_string('ssh-dss')
# apparently, in rare cases, r or s may be shorter than 20 bytes!
rstr = util.deflate_long(r, 0)
sstr = util.deflate_long(s, 0)
if len(rstr) < 20:
rstr = '\x00' * (20 - len(rstr)) + rstr
if len(sstr) < 20:
sstr = '\x00' * (20 - len(sstr)) + sstr
m.add_string(rstr + sstr)
return m
示例2: generate
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [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 bits: number of bits the generated key should be.
@type bits: int
@param progress_func: an optional function to call at key points in
key generation (used by C{pyCrypto.PublicKey}).
@type progress_func: function
@return: new private key
@rtype: L{DSSKey}
"""
dsa = DSA.generate(bits, rng.read, progress_func)
key = DSSKey(vals=(dsa.p, dsa.q, dsa.g, dsa.y))
key.x = dsa.x
return key
示例3: test_nonce_reuse
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def test_nonce_reuse(secret_key=DSA.generate(1024)):
# choose a "random" - k :) this time random is static in order to allow this attack to work
k = random.StrongRandom().randint(1, secret_key.q - 1)
# sign two messages using the same k
samples = (Tests.Dsa.signMessage(secret_key, "This is a signed message!", k),
Tests.Dsa.signMessage(secret_key, "Another signed Message - :)", k))
logger.debug("generated sample signatures: %s" % repr(samples))
signatures = [DsaSignature(sig, h, pubkey) for h, sig, pubkey in samples]
logger.debug("Signature Objects: %r" % signatures)
two_sigs = []
for sig in signatures:
two_sigs.append(sig)
if not len(two_sigs) == 2:
continue
sample = two_sigs.pop(0)
logger.debug("%r - recovering privatekey from nonce reuse..." % sample)
assert (sample.x is None) # not yet resolved
sample.recover_nonce_reuse(two_sigs[0])
assert (sample.x is not None) # privkey recovered
assert (sample.privkey == secret_key)
logger.debug("%r - Private key recovered! \n%s" % (sample, sample.export_key()))
示例4: genKeys
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def genKeys(bits):
key = DSA.generate(1024)
seq = asn1.DerSequence()
seq[:] = [ 0, key.p, key.q, key.g, key.y, key.x ]
k2 = "-----BEGIN DSA PRIVATE KEY-----\n%s-----END DSA PRIVATE KEY-----" % seq.encode().encode("base64")
return k2
示例5: signMessage
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def signMessage(privkey, msg, k=None):
"""
create DSA signed message
@arg privkey ... privatekey as DSA obj
@arg msg ... message to sign
@arg k ... override random k
"""
k = k or random.StrongRandom().randint(2, privkey.q - 1)
# generate msg hash
# sign the messages using privkey
h = SHA.new(msg).digest()
r, s = privkey.sign(h, k)
return h, (r, s), privkey.publickey()
示例6: generateRSAkey
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def generateRSAkey(options):
from Crypto.PublicKey import RSA
print 'Generating public/private rsa key pair.'
key = RSA.generate(int(options['bits']), randbytes.secureRandom)
_saveKey(key, options)
示例7: generateDSAkey
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def generateDSAkey(options):
from Crypto.PublicKey import DSA
print 'Generating public/private dsa key pair.'
key = DSA.generate(int(options['bits']), randbytes.secureRandom)
_saveKey(key, options)
示例8: generateRSAkey
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def generateRSAkey(options):
from Crypto.PublicKey import RSA
print 'Generating public/private rsa key pair.'
key = RSA.generate(int(options['bits']), common.entropy.get_bytes)
_saveKey(key, options)
示例9: generateDSAkey
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def generateDSAkey(options):
from Crypto.PublicKey import DSA
print 'Generating public/private dsa key pair.'
key = DSA.generate(int(options['bits']), common.entropy.get_bytes)
_saveKey(key, options)
示例10: test_generate_1arg
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def test_generate_1arg(self):
"""DSA (default implementation) generated key (1 argument)"""
dsaObj = self.dsa.generate(1024)
self._check_private_key(dsaObj)
pub = dsaObj.publickey()
self._check_public_key(pub)
示例11: test_generate_2arg
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def test_generate_2arg(self):
"""DSA (default implementation) generated key (2 arguments)"""
dsaObj = self.dsa.generate(1024, Random.new().read)
self._check_private_key(dsaObj)
pub = dsaObj.publickey()
self._check_public_key(pub)
示例12: test_domain1
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def test_domain1(self):
"""Verify we can generate new keys in a given domain"""
dsa_key_1 = DSA.generate(1024)
domain_params = dsa_key_1.domain()
dsa_key_2 = DSA.generate(1024, domain=domain_params)
self.assertEqual(dsa_key_1.p, dsa_key_2.p)
self.assertEqual(dsa_key_1.q, dsa_key_2.q)
self.assertEqual(dsa_key_1.g, dsa_key_2.g)
self.assertEqual(dsa_key_1.domain(), dsa_key_2.domain())
示例13: test_generate_error_weak_domain
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def test_generate_error_weak_domain(self):
"""Verify that domain parameters with composite q are rejected"""
domain_params = self._get_weak_domain()
self.assertRaises(ValueError, DSA.generate, 1024, domain=domain_params)
示例14: generate_asymmetric
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import generate [as 別名]
def generate_asymmetric(self, generate_dto, kek_meta_dto, project_id):
"""Generate asymmetric keys based on below rules:
- RSA, with passphrase (supported)
- RSA, without passphrase (supported)
- DSA, without passphrase (supported)
- DSA, with passphrase (not supported)
Note: PyCrypto is not capable of serializing DSA
keys and DER formated keys. Such keys will be
serialized to Base64 PEM to store in DB.
TODO (atiwari/reaperhulk): PyCrypto is not capable to serialize
DSA keys and DER formated keys, later we need to pick better
crypto lib.
"""
if(generate_dto.algorithm is None or generate_dto
.algorithm.lower() == 'rsa'):
private_key = RSA.generate(
generate_dto.bit_length, None, None, 65537)
elif generate_dto.algorithm.lower() == 'dsa':
private_key = DSA.generate(generate_dto.bit_length, None, None)
else:
raise c.CryptoPrivateKeyFailureException()
public_key = private_key.publickey()
# Note (atiwari): key wrapping format PEM only supported
if generate_dto.algorithm.lower() == 'rsa':
public_key, private_key = self._wrap_key(public_key, private_key,
generate_dto.passphrase)
if generate_dto.algorithm.lower() == 'dsa':
if generate_dto.passphrase:
raise ValueError(u._('Passphrase not supported for DSA key'))
public_key, private_key = self._serialize_dsa_key(public_key,
private_key)
private_dto = self.encrypt(c.EncryptDTO(private_key),
kek_meta_dto,
project_id)
public_dto = self.encrypt(c.EncryptDTO(public_key),
kek_meta_dto,
project_id)
passphrase_dto = None
if generate_dto.passphrase:
if isinstance(generate_dto.passphrase, six.text_type):
generate_dto.passphrase = generate_dto.passphrase.encode(
'utf-8')
passphrase_dto = self.encrypt(c.EncryptDTO(generate_dto.
passphrase),
kek_meta_dto,
project_id)
return private_dto, public_dto, passphrase_dto