Permalink
Browse files

RSA256 working.

  • Loading branch information...
1 parent e99bcc7 commit 29eb107149c28be9d8b91e9c393c9337853b8988 @brianloveswords committed Jun 2, 2011
Showing with 54 additions and 7 deletions.
  1. +1 −0 jws/__init__.py
  2. +3 −2 jws/algos.py
  3. +50 −5 jws/tests.py
View
@@ -3,6 +3,7 @@
# local
import algos
import header
+from algos import SignatureError
class MissingKey(Exception): pass
class MissingSigner(Exception): pass
View
@@ -66,8 +66,9 @@ def sign(self, msg, key):
hashm = SHA256.new()
hashm.update(msg)
- private_key = RSA.importKey(key)
- return PKCS.sign(hashm, private_key)
+ ## assume we are dealing with a real key
+ # private_key = RSA.importKey(key)
+ return PKCS.sign(hashm, key)
def verify(self, msg, crypto, key):
"""
View
@@ -2,6 +2,7 @@
import jws
import ecdsa
import hashlib
+import Crypto.PublicKey.RSA as rsa
class TestJWS_helpers(unittest.TestCase):
def test_default_algorithm_finding(self):
@@ -54,14 +55,14 @@ def sign(self, msg, key):
return 'u mad?' + key
def verify(self, msg, sig, key):
if sig == 'u mad?' + key: return '<trollface>'
- raise jws.algos.SignatureError('Y U NO GIVE GOOD SIGNATURE')
+ raise jws.SignatureError('Y U NO GIVE GOOD SIGNATURE')
jws.algos.CUSTOM = [ ('F7U12', F7U12) ]
header = {'alg': 'F7U12'}
payload = {'some': 'claim'}
sig = jws.sign(header, payload, 'wutlol')
self.assertEqual(jws.verify(header,payload,sig, 'wutlol'), '<trollface>')
- self.assertRaises(jws.algos.SignatureError, jws.verify, header, payload, sig, 'raaaaage')
+ self.assertRaises(jws.SignatureError, jws.verify, header, payload, sig, 'raaaaage')
class TestJWS_ecdsa(unittest.TestCase):
@@ -101,8 +102,52 @@ def test_invalid_ecdsa_decode(self):
sig = jws.sign(header, self.payload, self.sk256)
vk = self.sk256.get_verifying_key()
badkey = self.sk384.get_verifying_key()
- self.assertRaises(jws.algos.SignatureError, jws.verify, header, self.payload, 'not a good sig', vk)
- self.assertRaises(jws.algos.SignatureError, jws.verify, header, {'bad':1}, sig, vk)
- self.assertRaises(jws.algos.SignatureError, jws.verify, header, {'bad':1}, sig, badkey)
+ self.assertRaises(jws.SignatureError, jws.verify, header, self.payload, 'not a good sig', vk)
+ self.assertRaises(jws.SignatureError, jws.verify, header, {'bad':1}, sig, vk)
+ self.assertRaises(jws.SignatureError, jws.verify, header, {'bad':1}, sig, badkey)
+class TestJWS_hmac(unittest.TestCase):
+ def setUp(self):
+ self.payload = {
+ 'whine': {'luke': 'But I was going into Tosche station to pick up some power converters!'},
+ 'rebuttal': {'owen': "You can waste time with your friends when you're done with your chores."},
+ }
+
+ def test_valid_hmac256(self):
+ header = {'alg': 'HS256'}
+ sig = jws.sign(header, self.payload, 'secret')
+ self.assertTrue(len(sig) > 0)
+ self.assertTrue(jws.verify(header, self.payload, sig, 'secret'))
+
+ def test_valid_hmac384(self):
+ header = {'alg': 'HS384'}
+ sig = jws.sign(header, self.payload, 'secret')
+ self.assertTrue(len(sig) > 0)
+ self.assertTrue(jws.verify(header, self.payload, sig, 'secret'))
+
+ def test_valid_hmac512(self):
+ header = {'alg': 'HS512'}
+ sig = jws.sign(header, self.payload, 'secret')
+ self.assertTrue(len(sig) > 0)
+ self.assertTrue(jws.verify(header, self.payload, sig, 'secret'))
+
+ def test_invalid_hmac(self):
+ header = {'alg': 'HS512'}
+ sig = jws.sign(header, self.payload, 'secret')
+ self.assertRaises(jws.SignatureError(header, self.payload, sig, 'failwhale'))
+
+class TestJWS_rsa(unittest.TestCase):
+ private = rsa.generate(1024)
+ def setUp(self):
+ self.payload = {
+ 'whine': {'luke': 'But I was going into Tosche station to pick up some power converters!'},
+ 'rebuttal': {'owen': "You can waste time with your friends when you're done with your chores."},
+ }
+
+ def test_valid_rsa256(self):
+ header = {'alg': 'RS256'}
+ 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 29eb107

Please sign in to comment.