Permalink
Browse files

Merge pull request #1 from hdknr/master

Fix for Pycrypto2.5
  • Loading branch information...
2 parents 83bfc7e + 0523983 commit 0b103bee9c1c04e87f3c2f291019522cecdcb381 @brianloveswords committed Jan 16, 2012
Showing with 24 additions and 9 deletions.
  1. +10 −9 jws/algos.py
  2. +14 −0 jws/tests.py
View
19 jws/algos.py
@@ -62,21 +62,24 @@ class RSA(HasherBase):
Bottom line, you should probably use ECDSA instead.
"""
- supported_bits = (256,)
+ supported_bits = (256,384,512,) #:Seems to worka > 256
+
+ def __init__(self, bits):
+ super(RSA,self).__init__(bits)
+ from Crypto.Hash import SHA256,SHA384,SHA512
+ self.hashm = __import__('Crypto.Hash.SHA%d'%self.bits, globals(), locals(), ['*']).new()
def sign(self, msg, key):
"""
Signs a message with an RSA PrivateKey and hash method
"""
import Crypto.Signature.PKCS1_v1_5 as PKCS
- import Crypto.Hash.SHA256 as SHA256
import Crypto.PublicKey.RSA as RSA
- hashm = SHA256.new()
- hashm.update(msg)
+ self.hashm.update(msg)
## assume we are dealing with a real key
# private_key = RSA.importKey(key)
- return PKCS.sign(hashm, key)
+ return PKCS.new(key).sign(self.hashm) # pycrypto 2.5
def verify(self, msg, crypto, key):
"""
@@ -86,15 +89,13 @@ def verify(self, msg, crypto, key):
``key`` is the verifying key. Can be a real key object or a string.
"""
import Crypto.Signature.PKCS1_v1_5 as PKCS
- import Crypto.Hash.SHA256 as SHA256
import Crypto.PublicKey.RSA as RSA
- hashm = SHA256.new()
- hashm.update(msg)
+ self.hashm.update(msg)
private_key = key
if not isinstance(key, RSA._RSAobj):
private_key = RSA.importKey(key)
- if not PKCS.verify(hashm, private_key, crypto):
+ if not PKCS.new( private_key ).verify(self.hashm, crypto): #:pycrypto 2.5
raise SignatureError("Could not validate signature")
return True
View
14 jws/tests.py
@@ -151,3 +151,17 @@ def test_valid_rsa256(self):
public = self.private.publickey()
self.assertTrue(len(sig) > 0)
self.assertTrue(jws.verify(header, self.payload, sig, public))
+
+ def test_valid_rsa384(self):
+ header = {'alg': 'RS384'}
+ sig = jws.sign(header, self.payload, self.private)
+ public = self.private.publickey()
+ self.assertTrue(len(sig) > 0)
+ self.assertTrue(jws.verify(header, self.payload, sig, public))
+
+ def test_valid_rsa512(self):
+ header = {'alg': 'RS512'}
+ sig = jws.sign(header, self.payload, self.private)
+ public = self.private.publickey()
+ self.assertTrue(len(sig) > 0)
+ self.assertTrue(jws.verify(header, self.payload, sig, public))

0 comments on commit 0b103be

Please sign in to comment.