Skip to content
This repository was archived by the owner on Apr 20, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions rsa/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)