blob: 0e62f55da70edaa1712ff609282a68590692a263 [file] [log] [blame]
Sybren A. Stüvel2310b342016-01-22 13:11:22 +01001"""
2Some tests for the rsa/key.py file.
3"""
4
Sybren A. Stüvel2310b342016-01-22 13:11:22 +01005import unittest
6
7import rsa.key
8import rsa.core
9
10
11class BlindingTest(unittest.TestCase):
Sybren A. Stüvel2310b342016-01-22 13:11:22 +010012 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üvel29feb792016-01-27 18:12:13 +010029
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)