diff options
-rw-r--r-- | src/cryptography/hazmat/primitives/asymmetric/ec.py | 6 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_ec.py | 24 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/cryptography/hazmat/primitives/asymmetric/ec.py b/src/cryptography/hazmat/primitives/asymmetric/ec.py index eda7df0c..907a6358 100644 --- a/src/cryptography/hazmat/primitives/asymmetric/ec.py +++ b/src/cryptography/hazmat/primitives/asymmetric/ec.py @@ -302,6 +302,9 @@ class EllipticCurvePublicNumbers(object): def __ne__(self, other): return not self == other + def __hash__(self): + return hash((self.x, self.y, self.curve.name, self.curve.key_size)) + def __repr__(self): return ( "<EllipticCurvePublicNumbers(curve={0.curve.name}, x={0.x}, " @@ -341,6 +344,9 @@ class EllipticCurvePrivateNumbers(object): def __ne__(self, other): return not self == other + def __hash__(self): + return hash((self.private_value, self.public_numbers)) + class ECDH(object): pass diff --git a/tests/hazmat/primitives/test_ec.py b/tests/hazmat/primitives/test_ec.py index 08619b48..8747ea4f 100644 --- a/tests/hazmat/primitives/test_ec.py +++ b/tests/hazmat/primitives/test_ec.py @@ -223,6 +223,30 @@ def test_ec_public_numbers_repr(): assert repr(pn) == "<EllipticCurvePublicNumbers(curve=secp256r1, x=2, y=3>" +def test_ec_public_numbers_hash(): + pn1 = ec.EllipticCurvePublicNumbers(2, 3, ec.SECP256R1()) + pn2 = ec.EllipticCurvePublicNumbers(2, 3, ec.SECP256R1()) + pn3 = ec.EllipticCurvePublicNumbers(1, 3, ec.SECP256R1()) + + assert hash(pn1) == hash(pn2) + assert hash(pn1) != hash(pn3) + + +def test_ec_private_numbers_hash(): + numbers1 = ec.EllipticCurvePrivateNumbers( + 1, ec.EllipticCurvePublicNumbers(2, 3, DummyCurve()) + ) + numbers2 = ec.EllipticCurvePrivateNumbers( + 1, ec.EllipticCurvePublicNumbers(2, 3, DummyCurve()) + ) + numbers3 = ec.EllipticCurvePrivateNumbers( + 2, ec.EllipticCurvePublicNumbers(2, 3, DummyCurve()) + ) + + assert hash(numbers1) == hash(numbers2) + assert hash(numbers1) != hash(numbers3) + + @pytest.mark.requires_backend_interface(interface=EllipticCurveBackend) class TestECWithNumbers(object): @pytest.mark.parametrize( |