1""" 2Some tests for the rsa/key.py file. 3""" 4 5import unittest 6 7import rsa.key 8import rsa.core 9 10 11class BlindingTest(unittest.TestCase): 12 def test_blinding(self): 13 """Test blinding and unblinding. 14 15 This is basically the doctest of the PrivateKey.blind method, but then 16 implemented as unittest to allow running on different Python versions. 17 """ 18 19 pk = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) 20 21 message = 12345 22 encrypted = rsa.core.encrypt_int(message, pk.e, pk.n) 23 24 blinded = pk.blind(encrypted, 4134431) # blind before decrypting 25 decrypted = rsa.core.decrypt_int(blinded, pk.d, pk.n) 26 unblinded = pk.unblind(decrypted, 4134431) 27 28 self.assertEqual(unblinded, message) 29 30 31class KeyGenTest(unittest.TestCase): 32 def test_custom_exponent(self): 33 priv, pub = rsa.key.newkeys(16, exponent=3) 34 35 self.assertEqual(3, priv.e) 36 self.assertEqual(3, pub.e) 37 38 def test_default_exponent(self): 39 priv, pub = rsa.key.newkeys(16) 40 41 self.assertEqual(0x10001, priv.e) 42 self.assertEqual(0x10001, pub.e) 43 44 def test_exponents_coefficient_calculation(self): 45 pk = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) 46 47 self.assertEqual(pk.exp1, 55063) 48 self.assertEqual(pk.exp2, 10095) 49 self.assertEqual(pk.coef, 50797) 50 51 def test_custom_getprime_func(self): 52 # List of primes to test with, in order [p, q, p, q, ....] 53 # By starting with two of the same primes, we test that this is 54 # properly rejected. 55 primes = [64123, 64123, 64123, 50957, 39317, 33107] 56 57 def getprime(_): 58 return primes.pop(0) 59 60 # This exponent will cause two other primes to be generated. 61 exponent = 136407 62 63 (p, q, e, d) = rsa.key.gen_keys(64, 64 accurate=False, 65 getprime_func=getprime, 66 exponent=exponent) 67 self.assertEqual(39317, p) 68 self.assertEqual(33107, q) 69 70 71class HashTest(unittest.TestCase): 72 """Test hashing of keys""" 73 74 def test_hash_possible(self): 75 priv, pub = rsa.key.newkeys(16) 76 77 # This raises a TypeError when hashing isn't possible. 78 hash(priv) 79 hash(pub) 80