本文整理汇总了Python中M2Crypto.DSA.load_key方法的典型用法代码示例。如果您正苦于以下问题:Python DSA.load_key方法的具体用法?Python DSA.load_key怎么用?Python DSA.load_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类M2Crypto.DSA
的用法示例。
在下文中一共展示了DSA.load_key方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_pub_verify
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def test_pub_verify(self):
dsa = DSA.load_key(self.privkey)
r, s = dsa.sign(self.data)
dsapub = DSA.load_pub_key(self.pubkey)
assert dsapub.check_key()
assert dsapub.verify(self.data, r, s)
self.assertRaises(DSA.DSAError, dsapub.sign)
示例2: generatedata
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def generatedata(n, owner="freeconet", keyfile="freeconet.priv.pem"):
start = 48600000000
end = 48699999999
thissip = "sip.freeconet.pl"
prevsip = "new.freeconet.pl"
thisdate = datetime.datetime(2009, 2, 14, 12).isoformat()
prevdate = datetime.datetime(2009, 2, 15, 9).isoformat()
from random import randrange
# no duplicates
points = list(set(randrange(start, end) for i in xrange(n)))
points.sort()
first = ["+%s" % start, "+%s" % points[0], thissip, owner, thisdate]
from M2Crypto import DSA
dsa = DSA.load_key(keyfile)
first.append(crypto.sign_record(dsa, *first))
data = [first]
for i in xrange(n - 1):
print i, i + 1, len(points), points[i]
s = points[i]
e = points[i + 1] - 1
thissip, prevsip = prevsip, thissip
thisdate, prevdate = prevdate, thisdate
r = ["+%s" % s, "+%s" % e, thissip, owner, thisdate]
r.append(crypto.sign_record(dsa, *r))
data.append(r)
return data
示例3: test_verify_fail2
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def test_verify_fail2(self):
dsa = DSA.load_key(self.privkey)
r, s = dsa.sign(self.data)
dsa2 = DSA.load_params(self.param)
assert not dsa2.check_key()
with self.assertRaises(AssertionError):
dsa2.verify(self.data, r, s)
示例4: generateSign
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def generateSign(self):
message = self.toRaw()
md = EVP.MessageDigest('sha1')
md.update(message)
digest = md.final()
dsa = DSA.load_key("keys/dsa_priv_ms.pem")
self.signature = dsa.sign(digest)
return
示例5: test_loadkey
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def test_loadkey(self):
dsa = DSA.load_key(self.privkey)
self.assertEqual(len(dsa), 1024)
with self.assertRaises(AttributeError):
getattr(dsa, 'foobar')
for k in ('p', 'q', 'g', 'priv', 'pub'):
with self.assertRaises(DSA.DSAError):
setattr(dsa, k, 1)
示例6: __init__
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def __init__(self,pub_key_Path, priv_key_Path=None ):
##LOAD priv_key
try:
if priv_key_Path is not None:
try:
priv_key = RSA.load_key(priv_key_Path)
except Exception, e:
priv_key = DSA.load_key(priv_key_Path)
else :
示例7: set_security_agent
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def set_security_agent(request):
serviceDIGEST = "user_id=%s&granted_roles=%s×tamp=%s"
serviceACTION = "%s/setgrantedroles?%s&sign=%s"
Roles = ()
serviceURL = ""
try:
if request.method == "POST":
for key, value in request.POST.iteritems():
if key == "serviceURL":
serviceURL = value
elif key == "csrfmiddlewaretoken":
continue
else:
role = Role.objects.get(name=key)
if not isinstance(role, roles):
return HttpResponse('FALSE')
Roles = Roles + ( key, )
validator = URLValidator()
try:
validator(serviceURL)
except Exception, e:
return HttpResponse("Service URL is not well formed")
granted_roles = ""
for value in Roles:
granted_roles += str(value) + ","
granted_roles = granted_roles[:-1]
serviceDIGEST = serviceDIGEST % (request.user.username, granted_roles, str(int(time.time())))
key = DSA.load_key(settings.MOD_AUTH_PRIVTICKET)
serviceSIGN = calculate_sign(key, serviceDIGEST)
requestURL = serviceACTION % (serviceURL, serviceDIGEST, serviceSIGN)
username = "test"
password = "test"
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, requestURL, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
try:
pagehandle = urllib2.urlopen(requestURL)
except:
pagehandle = urllib2.urlopen(requestURL)
if pagehandle.code != 200:
return HttpResponse(' Sec/Agent request refused.')
return HttpResponse('TRUE')
except Exception, e:
return HttpResponse("FALSE")
示例8: generateSign
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def generateSign(self):
message = self.toRaw()
print "GenSign Raw:", (":".join("{0:02x}".format(ord(c)) for c in message))
md = EVP.MessageDigest('sha1')
md.update(message)
digest = md.final()
print "GenSign SHA1:", (":".join("{0:02x}".format(ord(c)) for c in digest))
#print "Mask:", self.mask
dsa = DSA.load_key("keys/dsa_priv_xtr.pem")
self.signature = dsa.sign(digest)
print "GenSign r:", (":".join("{0:02x}".format(ord(c)) for c in self.signature[0]))
print "GenSign s:", (":".join("{0:02x}".format(ord(c)) for c in self.signature[1]))
return
示例9: private_key_type
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def private_key_type(key_file):
""" Determines type of the private key: RSA, DSA, EC.
:param key_file: file path
:type key_file: str
:return: one of "RSA", "DSA" or "EC"
:except CannotFindKeyTypeError
"""
try:
RSA.load_key(key_file)
return "RSA"
except:
pass
try:
DSA.load_key(key_file)
return "DSA"
except:
pass
try:
EC.load_key(key_file)
return "EC"
except:
raise CannotFindKeyTypeError
示例10: main
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def main(keylen, hashalg):
global dsa, dgst # this exists ONLY for speed testing
Rand.load_file('randpool.dat', -1)
pvtkeyfilename = 'DSA%dpvtkey.pem' % (keylen)
pubkeyfilename = 'DSA%dpubkey.pem' % (keylen)
if makenewkey:
print ' making and saving a new key'
dsa = DSA.gen_params(keylen)
dsa.gen_key()
dsa.save_key(pvtkeyfilename, None ) # no pswd callback
dsa.save_pub_key(pubkeyfilename)
else:
print ' loading an existing key'
dsa = DSA.load_key(pvtkeyfilename)
print ' dsa key length:', len(dsa)
if not dsa.check_key():
raise 'key is not initialised'
if showpubkey:
dsa_pub = dsa.pub
pub_pem = base64.encodestring(dsa_pub)
print ' PEM public key is: \n',pub_pem
# since we are testing signing and verification, let's not
# be fussy about the digest. Just make one.
md = EVP.MessageDigest(hashalg)
md.update('can you spell subliminal channel?')
dgst = md.digest()
print ' hash algorithm: %s' % hashalg
if showdigest:
print ' %s digest: \n%s' % (hashalg, base64.encodestring(dgst))
test(dsa, dgst)
# test_asn1(dsa, dgst)
test_speed(dsa, dgst)
Rand.save_file('randpool.dat')
示例11: loadKey
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def loadKey(self, file, callback, cipher=None, parent=None):
key = None
if cipher == "RSA" or cipher == None:
try:
key = RSA.load_key(file, callback)
return key, "RSA"
except IOError, (errno, strerror):
return None, "RSA" # file not found
except RSA.RSAError, args:
print "RSA key generation error: " + args[0]
pass
except ValueError:
pass
if cipher == "DSA" or cipher == None:
try:
key = DSA.load_key(file, callback)
return key, "DSA"
except IOError, (errno, strerror):
return None, "DSA" # file not found
except ValueError:
pass
except DSA.DSAError:
pass
return None, None
def saveKey(self, cipher, filename, handle, callback, save_cipher="des_ede3_cbc", prompt=0, parent=None):
success = 1
if cipher == "RSA":
success = handle.save_key(filename, save_cipher, callback)
elif cipher == "DSA":
handle.save_key(filename, callback)
示例12: Copyright
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
#!/usr/bin/env python
"""DSA demonstration.
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""
from M2Crypto import DSA, EVP, Rand
md=EVP.MessageDigest('sha1')
md.update('can you spell subliminal channel?')
dgst=md.digest()
d=DSA.load_key('dsatest.pem')
def test():
print 'testing signing...',
r,s=d.sign(dgst)
if not d.verify(dgst, r, s):
print 'not ok'
else:
print 'ok'
def test_asn1():
# XXX Randomly fails: bug in there somewhere... (0.9.4)
print 'testing asn1 signing...',
blob=d.sign_asn1(dgst)
if not d.verify_asn1(dgst, blob):
print 'not ok'
else:
print 'ok'
示例13: test_sign
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def test_sign(self):
dsa = DSA.load_key(self.privkey)
assert dsa.check_key()
r, s = dsa.sign(self.data)
assert dsa.verify(self.data, r, s)
assert not dsa.verify(self.data, s, r)
示例14: test_loadparam
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def test_loadparam(self):
with self.assertRaises(DSA.DSAError):
DSA.load_key(self.param)
dsa = DSA.load_params(self.param)
assert not dsa.check_key()
self.assertEqual(len(dsa), 1024)
示例15: test_loadkey_junk
# 需要导入模块: from M2Crypto import DSA [as 别名]
# 或者: from M2Crypto.DSA import load_key [as 别名]
def test_loadkey_junk(self):
with self.assertRaises(DSA.DSAError):
DSA.load_key(self.errkey)