Sybren A. Stüvel | 2310b34 | 2016-01-22 13:11:22 +0100 | [diff] [blame] | 1 | """ |
| 2 | Some tests for the rsa/key.py file. |
| 3 | """ |
| 4 | |
Sybren A. Stüvel | 2310b34 | 2016-01-22 13:11:22 +0100 | [diff] [blame] | 5 | import unittest |
| 6 | |
| 7 | import rsa.key |
| 8 | import rsa.core |
| 9 | |
| 10 | |
| 11 | class BlindingTest(unittest.TestCase): |
Sybren A. Stüvel | 2310b34 | 2016-01-22 13:11:22 +0100 | [diff] [blame] | 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) |
Sybren A. Stüvel | 29feb79 | 2016-01-27 18:12:13 +0100 | [diff] [blame^] | 29 | |
| 30 | |
| 31 | class 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) |