Skip to content
Browse files

RSA384 and RSA512 seem to work....

  • Loading branch information...
1 parent 445bcc0 commit 7330c58b0e7e2d88c72e0b8a121769ad369ac2ea @hdknr hdknr committed Jan 16, 2012
Showing with 25 additions and 9 deletions.
  1. +11 −9 jws/algos.py
  2. +14 −0 jws/tests.py
View
20 jws/algos.py
@@ -18,6 +18,7 @@ def __init__(self, bits):
matching hash method from ``hashlib`` if necessary.
"""
self.bits = int(bits)
+ print self.bits,"in",self.supported_bits #:HDKNR
if self.bits not in self.supported_bits:
raise NotImplementedError("%s implements %s bit algorithms (given %d)" %
(self.__class__, ', '.join(self.supported_bits), self.bits))
@@ -62,21 +63,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.new(key).sign(hashm) # pycrypto 2.5
+ return PKCS.new(key).sign(self.hashm) # pycrypto 2.5
def verify(self, msg, crypto, key):
"""
@@ -86,15 +90,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.new( private_key ).verify(hashm, crypto): #:pycrypto 2.5
+ 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 7330c58

Please sign in to comment.
Something went wrong with that request. Please try again.