Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added convenience key function wrappers f_public and f_private; updat…

…ed unittests
  • Loading branch information...
commit d670d72e6bb1503373bd4fb4e435527a1288a860 1 parent ce6ee65
Andrew D. Yates authored
Showing with 54 additions and 0 deletions.
  1. +7 −0 README
  2. +20 −0 __init__.py
  3. +27 −0 test.py
View
7 README
@@ -28,6 +28,13 @@ SAMPLE USE:
... print cert_dict["subject"]
... print cert_dict["body"]
+# Return "nice" public and private key functions from a key object
+>>> f_my_public = f_public(key)
+... f_my_private = f_private(key)
+... msg = "hello"
+... assert msg == f_my_public(f_my_public(msg))
+... assert msg == f_my_private(f_my_public(msg))
+
RUN UNIT TESTS:
View
20 __init__.py
@@ -51,3 +51,23 @@ def get_key(parse_dict):
raise Exception("parse_dict type '%s' not supported." % parse_dict['type'])
key = RSA.construct(key_tuple)
return key
+
+def f_public(key):
+ """Return a convenient public key function.
+
+ Args:
+ key: `RSAKey` as returned by get_key(parse_dict).
+ Returns:
+ function(msg) => str of RSA() using `key`
+ """
+ return lambda x: key.encrypt(x, None)[0]
+
+def f_private(key):
+ """Return a convenient public key function.
+
+ Args:
+ key: `RSAKey` as returned by get_key(parse_dict).
+ Returns:
+ function(msg) => str of RSA^-1() using `key`
+ """
+ return key.decrypt
View
27 test.py
@@ -308,6 +308,33 @@ def test_x509_dict_to_key(self):
def test_RSA_obj(self):
self.assertEqual(top.RSAKey, RSA.RSAobj)
+class TestFunctionWrappers(unittest.TestCase):
+
+ def setUp(self):
+ self.pubkey = top.get_key(top.parse(open(KEY_FILE_PAIRS[0][1]).read()))
+ self.privkey = top.get_key(top.parse(open(KEY_FILE_PAIRS[0][0]).read()))
+
+ def test_public(self):
+ f_my_public = top.f_public(self.pubkey)
+ self.assertTrue(f_my_public(MSG1))
+ f_my_public2 = top.f_public(self.privkey)
+ self.assertTrue(f_my_public2(MSG1))
+
+ def test_private(self):
+ f_my_private = top.f_private(self.privkey)
+ self.assertTrue(f_my_private(MSG1))
+ f_my_private2 = top.f_private(self.pubkey)
+ # cannot use private function on a public key
+ self.assertRaises(Exception, f_my_private2, MSG1)
+
+ def test_inverse(self):
+ f_my_private = top.f_private(self.privkey)
+ f_my_public = top.f_public(self.privkey)
+ self.assertEqual(MSG1, f_my_public(f_my_private(MSG1)))
+ self.assertEqual(MSG1, f_my_private(f_my_public(MSG1)))
+ self.assertNotEqual(MSG1, f_my_public(f_my_public(MSG1)))
+ self.assertNotEqual(MSG1, f_my_private(f_my_private(MSG1)))
+
def main():
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.