summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rsa/key.py6
-rw-r--r--tests/test_key.py11
2 files changed, 17 insertions, 0 deletions
diff --git a/rsa/key.py b/rsa/key.py
index a5afced..73494b9 100644
--- a/rsa/key.py
+++ b/rsa/key.py
@@ -228,6 +228,9 @@ class PublicKey(AbstractKey):
def __ne__(self, other):
return not (self == other)
+ def __hash__(self):
+ return hash((self.n, self.e))
+
@classmethod
def _load_pkcs1_der(cls, keyfile):
"""Loads a key in PKCS#1 DER format.
@@ -430,6 +433,9 @@ class PrivateKey(AbstractKey):
def __ne__(self, other):
return not (self == other)
+ def __hash__(self):
+ return hash((self.n, self.e, self.d, self.p, self.q, self.exp1, self.exp2, self.coef))
+
def blinded_decrypt(self, encrypted):
"""Decrypts the message using blinding to prevent side-channel attacks.
diff --git a/tests/test_key.py b/tests/test_key.py
index 833a6aa..afefa3a 100644
--- a/tests/test_key.py
+++ b/tests/test_key.py
@@ -59,3 +59,14 @@ class KeyGenTest(unittest.TestCase):
exponent=exponent)
self.assertEqual(39317, p)
self.assertEqual(33107, q)
+
+
+class HashTest(unittest.TestCase):
+ """Test hashing of keys"""
+
+ def test_hash_possible(self):
+ priv, pub = rsa.key.newkeys(16)
+
+ # This raises a TypeError when hashing isn't possible.
+ hash(priv)
+ hash(pub)