diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2016-04-30 18:57:15 -0400 |
---|---|---|
committer | Paul Kehrer <paul.l.kehrer@gmail.com> | 2016-04-30 17:57:15 -0500 |
commit | c16de483685b0d5afa77bd88bd4a561b09e899b1 (patch) | |
tree | 8294e8389f6ef9d1f010005d47cca89e8800055d | |
parent | 988df9ba0326f5adc84ee6ca51607674737f1469 (diff) | |
download | cryptography-c16de483685b0d5afa77bd88bd4a561b09e899b1.tar.gz cryptography-c16de483685b0d5afa77bd88bd4a561b09e899b1.tar.bz2 cryptography-c16de483685b0d5afa77bd88bd4a561b09e899b1.zip |
Fixed #2887 -- implement __hash__ on EC numbers classes (#2888)
-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( |