From 86ce9a13461cccf1d3f01d85993bb9d7bd26480e Mon Sep 17 00:00:00 2001 From: adamantike Date: Thu, 17 Mar 2016 13:05:53 -0300 Subject: [PATCH] Simplified parameter verification for PrivateKey constructor --- rsa/key.py | 17 +++-------------- tests/test_key.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/rsa/key.py b/rsa/key.py index 3e3fc42..4605e73 100644 --- a/rsa/key.py +++ b/rsa/key.py @@ -382,20 +382,9 @@ def __init__(self, n, e, d, p, q, exp1=None, exp2=None, coef=None): self.q = q # Calculate the other values if they aren't supplied - if exp1 is None: - self.exp1 = int(d % (p - 1)) - else: - self.exp1 = exp1 - - if exp2 is None: - self.exp2 = int(d % (q - 1)) - else: - self.exp2 = exp2 - - if coef is None: - self.coef = rsa.common.inverse(q, p) - else: - self.coef = coef + self.exp1 = exp1 or int(d % (p - 1)) + self.exp2 = exp2 or int(d % (q - 1)) + self.coef = coef or rsa.common.inverse(q, p) def __getitem__(self, key): return getattr(self, key) diff --git a/tests/test_key.py b/tests/test_key.py index 0e62f55..0d4b827 100644 --- a/tests/test_key.py +++ b/tests/test_key.py @@ -40,3 +40,18 @@ def test_default_exponent(self): self.assertEqual(0x10001, priv.e) self.assertEqual(0x10001, pub.e) + + +class PrivateKeyTest(unittest.TestCase): + def test_supplied_parameters(self): + """Test exponents and coefficient assignation. + + Checks correct assignation for PrivateKey's exponents and coefficient. + """ + + pk = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287, + exp1="", exp2=None) + + self.assertIsInstance(pk.exp1, int) + self.assertIsInstance(pk.exp2, int) + self.assertIsInstance(pk.coef, int)