本文整理匯總了Python中Crypto.PublicKey.DSA.construct方法的典型用法代碼示例。如果您正苦於以下問題:Python DSA.construct方法的具體用法?Python DSA.construct怎麽用?Python DSA.construct使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Crypto.PublicKey.DSA
的用法示例。
在下文中一共展示了DSA.construct方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: getDSAKey
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def getDSAKey(self):
"""
Return a PyCrypto DSA key to support the tests.
@return: The DSA key to support the tests.
@rtype: C{Crypto.PublicKey.DSA}
"""
# Use lazy import as PyCrypto will be deprecated.
from Crypto.PublicKey import DSA
return DSA.construct((
keydata.DSAData['y'],
keydata.DSAData['g'],
keydata.DSAData['p'],
keydata.DSAData['q'],
keydata.DSAData['x'],
))
示例2: test_keyObjectSetRSAPublic
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def test_keyObjectSetRSAPublic(self):
"""
Setting the L{keys.Key.keyObject} property to a PyCrypto public RSA key
instance updates the internal key.
"""
key = keys.Key.fromString(keydata.publicDSA_openssh)
newPyCryptoKey = Crypto.PublicKey.RSA.construct((
keydata.RSAData['n'],
keydata.RSAData['e'],
))
self.assertEqual('DSA', key.type())
key.keyObject = newPyCryptoKey
[warning] = self.flushWarnings([
KeyKeyObjectTests.test_keyObjectSetRSAPublic])
self.assertIs(warning['category'], DeprecationWarning)
self.assertEqual('RSA', key.type())
self.assertEqual({
'n': keydata.RSAData['n'],
'e': keydata.RSAData['e'],
},
key.data())
示例3: test_keyObjectSetDSAPublic
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def test_keyObjectSetDSAPublic(self):
"""
Setting the L{keys.Key.keyObject} property to a PyCrypto public DSA key
instance updates the internal key.
"""
key = keys.Key.fromString(keydata.publicRSA_openssh)
newPyCryptoKey = Crypto.PublicKey.DSA.construct((
keydata.DSAData['y'],
keydata.DSAData['g'],
keydata.DSAData['p'],
keydata.DSAData['q'],
))
self.assertEqual('RSA', key.type())
key.keyObject = newPyCryptoKey
self.assertEqual('DSA', key.type())
self.assertEqual({
'y': keydata.DSAData['y'],
'g': keydata.DSAData['g'],
'p': keydata.DSAData['p'],
'q': keydata.DSAData['q'],
},
key.data())
示例4: test_constructorPyCrypto
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def test_constructorPyCrypto(self):
"""
Passing a PyCrypto key object to L{keys.Key} is deprecated.
"""
pycryptoKey = Crypto.PublicKey.RSA.construct((
keydata.RSAData['n'],
keydata.RSAData['e']))
key = self.callDeprecated(
(Version('Twisted', 16, 0, 0),
'passing a cryptography key object'),
keys.Key,
pycryptoKey)
self.assertEqual('RSA', key.type())
self.assertEqual({
'n': keydata.RSAData['n'],
'e': keydata.RSAData['e'],
},
key.data())
示例5: verifyMsg
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def verifyMsg(text, imported_key, sig):
seq2 = asn1.DerSequence()
data = '\n'.join(imported_key.strip().split('\n')[1:-1]).decode('base64')
seq2.decode(data)
p, q, g, y, x = seq2[1:]
key2 = DSA.construct((y, g, p, q, x))
k1 = random.StrongRandom().randint(1,key2.q-1)
h = SHA.new(text).digest()
a = key2.verify(h, sig)
return a
示例6: signMsg
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def signMsg(text, imported_key):
seq2 = asn1.DerSequence()
data = '\n'.join(imported_key.strip().split('\n')[1:-1]).decode('base64')
seq2.decode(data)
p, q, g, y, x = seq2[1:]
key2 = DSA.construct((y, g, p, q, x))
k1 = random.StrongRandom().randint(1,key2.q-1)
h = SHA.new(text).digest()
sig = key2.sign(h, k1)
return sig
示例7: test_generate_1024_DSA_key_in_pem_and_reconstruct_key_der
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def test_generate_1024_DSA_key_in_pem_and_reconstruct_key_der(self):
generate_dto = plugin.GenerateDTO('dsa', 1024, None, None)
kek_meta_dto = self._get_mocked_kek_meta_dto()
private_dto, public_dto, passwd_dto = self.plugin.generate_asymmetric(
generate_dto,
kek_meta_dto,
mock.MagicMock()
)
decrypt_dto = plugin.DecryptDTO(private_dto.cypher_text)
private_dto = self.plugin.decrypt(decrypt_dto,
kek_meta_dto,
private_dto.kek_meta_extended,
mock.MagicMock())
prv_seq = asn1.DerSequence()
prv_seq.decode(private_dto)
p, q, g, y, x = prv_seq[1:]
private_dto = DSA.construct((y, g, p, q, x))
self.assertTrue(private_dto.has_private())
示例8: sign_ssh_data
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [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
示例9: verify_ssh_sig
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def verify_ssh_sig(self, data, msg):
if len(str(msg)) == 40:
# spies.com bug: signature has no header
sig = str(msg)
else:
kind = msg.get_string()
if kind != 'ssh-dss':
return 0
sig = msg.get_string()
# pull out (r, s) which are NOT encoded as mpints
sigR = util.inflate_long(sig[:20], 1)
sigS = util.inflate_long(sig[20:], 1)
sigM = util.inflate_long(SHA.new(data).digest(), 1)
dss = DSA.construct((long(self.y), long(self.g), long(self.p), long(self.q)))
return dss.verify(sigM, (sigR, sigS))
示例10: _fromString_PUBLIC_LSH
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def _fromString_PUBLIC_LSH(Class, data):
"""
Return a public key corresponding to this LSH public key string.
The LSH public key string format is::
<s-expression: ('public-key', (<key type>, (<name, <value>)+))>
The names for a RSA (key type 'rsa-pkcs1-sha1') key are: n, e.
The names for a DSA (key type 'dsa') key are: y, g, p, q.
@type data: C{str}
@return: a C{Crypto.PublicKey.pubkey.pubkey} object
@raises BadKeyError: if the key type is unknown
"""
sexp = sexpy.parse(base64.decodestring(data[1:-1]))
assert sexp[0] == 'public-key'
kd = {}
for name, data in sexp[1][1:]:
kd[name] = common.getMP(common.NS(data))[0]
if sexp[1][0] == 'dsa':
return Class(DSA.construct((kd['y'], kd['g'], kd['p'], kd['q'])))
elif sexp[1][0] == 'rsa-pkcs1-sha1':
return Class(RSA.construct((kd['n'], kd['e'])))
else:
raise BadKeyError('unknown lsh key type %s' % sexp[1][0])
示例11: getPublicKeyObject
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def getPublicKeyObject(data):
"""
Return a C{Crypto.PublicKey.pubkey.pubkey} corresponding to the SSHv2
public key data. data is in the over-the-wire public key format.
@type data: C{str}
@rtype: C{Crypto.PublicKey.pubkey.pubkey}
"""
keyKind, rest = common.getNS(data)
if keyKind == 'ssh-rsa':
e, rest = common.getMP(rest)
n, rest = common.getMP(rest)
return RSA.construct((n, e))
elif keyKind == 'ssh-dss':
p, rest = common.getMP(rest)
q, rest = common.getMP(rest)
g, rest = common.getMP(rest)
y, rest = common.getMP(rest)
return DSA.construct((y, g, p, q))
else:
raise BadKeyError('unknown key type %s' % keyKind)
示例12: getPrivateKeyObject_lsh
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def getPrivateKeyObject_lsh(data, passphrase):
#assert passphrase == ''
data = ''.join(data)
sexp = sexpy.parse(data)
assert sexp[0] == 'private-key'
kd = {}
for name, data in sexp[1][1:]:
kd[name] = common.getMP(common.NS(data))[0]
if sexp[1][0] == 'dsa':
assert len(kd) == 5, len(kd)
return DSA.construct((kd['y'], kd['g'], kd['p'], kd['q'], kd['x']))
elif sexp[1][0] == 'rsa-pkcs1':
assert len(kd) == 8, len(kd)
return RSA.construct((kd['n'], kd['e'], kd['d'], kd['p'], kd['q']))
else:
raise BadKeyError('unknown lsh key type %s' % sexp[1][0])
示例13: getPrivateKeyObject_agentv3
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def getPrivateKeyObject_agentv3(data, passphrase):
if passphrase:
raise BadKeyError("agent v3 key should not be encrypted")
keyType, data = common.getNS(data)
if keyType == 'ssh-dss':
p, data = common.getMP(data)
q, data = common.getMP(data)
g, data = common.getMP(data)
y, data = common.getMP(data)
x, data = common.getMP(data)
return DSA.construct((y,g,p,q,x))
elif keyType == 'ssh-rsa':
e, data = common.getMP(data)
d, data = common.getMP(data)
n, data = common.getMP(data)
u, data = common.getMP(data)
p, data = common.getMP(data)
q, data = common.getMP(data)
return RSA.construct((n,e,d,p,q,u))
else:
raise BadKeyError("unknown key type %s" % keyType)
示例14: test1
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def test1(self):
q = 0x4000000000000000000020108A2E0CC0D99F8A5EF
x = 0x09A4D6792295A7F730FC3F2B49CBC0F62E862272F
p = 2 * q + 1
y = pow(2, x, p)
key = DSA.construct([pow(y, 2, p), 2, p, q, x], False)
signer = DSS.new(key, 'deterministic-rfc6979')
# Test _int2octets
self.assertEqual(hexlify(signer._int2octets(x)),
b'009a4d6792295a7f730fc3f2b49cbc0f62e862272f')
# Test _bits2octets
h1 = SHA256.new(b"sample").digest()
self.assertEqual(hexlify(signer._bits2octets(h1)),
b'01795edf0d54db760f156d0dac04c0322b3a204224')
示例15: getRSAKey
# 需要導入模塊: from Crypto.PublicKey import DSA [as 別名]
# 或者: from Crypto.PublicKey.DSA import construct [as 別名]
def getRSAKey(self):
"""
Return a PyCrypto RSA key to support the tests.
@return: The RSA key to support the tests.
@rtype: C{Crypto.PublicKey.RSA}
"""
# Use lazy import as PyCrypto will be deprecated.
from Crypto.PublicKey import RSA
return RSA.construct((
keydata.RSAData['n'],
keydata.RSAData['e'],
keydata.RSAData['d'],
))