Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1 from hdknr/master

Fix for Pycrypto2.5
  • Loading branch information...
commit 0b103bee9c1c04e87f3c2f291019522cecdcb381 2 parents 83bfc7e + 0523983
Brian J Brennan authored

Showing 2 changed files with 24 additions and 9 deletions. Show diff stats Hide diff stats

  1. +10 9 jws/algos.py
  2. +14 0 jws/tests.py
19 jws/algos.py
@@ -62,21 +62,24 @@ class RSA(HasherBase):
62 62
63 63 Bottom line, you should probably use ECDSA instead.
64 64 """
65   - supported_bits = (256,)
  65 + supported_bits = (256,384,512,) #:Seems to worka > 256
  66 +
  67 + def __init__(self, bits):
  68 + super(RSA,self).__init__(bits)
  69 + from Crypto.Hash import SHA256,SHA384,SHA512
  70 + self.hashm = __import__('Crypto.Hash.SHA%d'%self.bits, globals(), locals(), ['*']).new()
66 71
67 72 def sign(self, msg, key):
68 73 """
69 74 Signs a message with an RSA PrivateKey and hash method
70 75 """
71 76 import Crypto.Signature.PKCS1_v1_5 as PKCS
72   - import Crypto.Hash.SHA256 as SHA256
73 77 import Crypto.PublicKey.RSA as RSA
74 78
75   - hashm = SHA256.new()
76   - hashm.update(msg)
  79 + self.hashm.update(msg)
77 80 ## assume we are dealing with a real key
78 81 # private_key = RSA.importKey(key)
79   - return PKCS.sign(hashm, key)
  82 + return PKCS.new(key).sign(self.hashm) # pycrypto 2.5
80 83
81 84 def verify(self, msg, crypto, key):
82 85 """
@@ -86,15 +89,13 @@ def verify(self, msg, crypto, key):
86 89 ``key`` is the verifying key. Can be a real key object or a string.
87 90 """
88 91 import Crypto.Signature.PKCS1_v1_5 as PKCS
89   - import Crypto.Hash.SHA256 as SHA256
90 92 import Crypto.PublicKey.RSA as RSA
91 93
92   - hashm = SHA256.new()
93   - hashm.update(msg)
  94 + self.hashm.update(msg)
94 95 private_key = key
95 96 if not isinstance(key, RSA._RSAobj):
96 97 private_key = RSA.importKey(key)
97   - if not PKCS.verify(hashm, private_key, crypto):
  98 + if not PKCS.new( private_key ).verify(self.hashm, crypto): #:pycrypto 2.5
98 99 raise SignatureError("Could not validate signature")
99 100 return True
100 101
14 jws/tests.py
@@ -151,3 +151,17 @@ def test_valid_rsa256(self):
151 151 public = self.private.publickey()
152 152 self.assertTrue(len(sig) > 0)
153 153 self.assertTrue(jws.verify(header, self.payload, sig, public))
  154 +
  155 + def test_valid_rsa384(self):
  156 + header = {'alg': 'RS384'}
  157 + sig = jws.sign(header, self.payload, self.private)
  158 + public = self.private.publickey()
  159 + self.assertTrue(len(sig) > 0)
  160 + self.assertTrue(jws.verify(header, self.payload, sig, public))
  161 +
  162 + def test_valid_rsa512(self):
  163 + header = {'alg': 'RS512'}
  164 + sig = jws.sign(header, self.payload, self.private)
  165 + public = self.private.publickey()
  166 + self.assertTrue(len(sig) > 0)
  167 + self.assertTrue(jws.verify(header, self.payload, sig, public))

0 comments on commit 0b103be

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