From 0ed7822a5fde4724a56259e986edc43bd8f599c7 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Wed, 10 Dec 2014 08:18:02 -0600 Subject: add __ne__ and __eq__ methods to RSA, DSA, and EC numbers classes fixes #1449 --- .../hazmat/primitives/asymmetric/dsa.py | 23 ++++++++++++++++++++++ .../hazmat/primitives/asymmetric/ec.py | 20 +++++++++++++++++++ .../hazmat/primitives/asymmetric/rsa.py | 20 +++++++++++++++++++ 3 files changed, 63 insertions(+) (limited to 'src') diff --git a/src/cryptography/hazmat/primitives/asymmetric/dsa.py b/src/cryptography/hazmat/primitives/asymmetric/dsa.py index 5d942e04..03e49ba5 100644 --- a/src/cryptography/hazmat/primitives/asymmetric/dsa.py +++ b/src/cryptography/hazmat/primitives/asymmetric/dsa.py @@ -59,6 +59,12 @@ class DSAParameterNumbers(object): def parameters(self, backend): return backend.load_dsa_parameter_numbers(self) + def __eq__(self, other): + return self.p == other.p and self.q == other.q and self.g == other.g + + def __ne__(self, other): + return not self == other + class DSAPublicNumbers(object): def __init__(self, y, parameter_numbers): @@ -79,6 +85,15 @@ class DSAPublicNumbers(object): def public_key(self, backend): return backend.load_dsa_public_numbers(self) + def __eq__(self, other): + return ( + self.y == other.y and + self.parameter_numbers == other.parameter_numbers + ) + + def __ne__(self, other): + return not self == other + class DSAPrivateNumbers(object): def __init__(self, x, public_numbers): @@ -97,3 +112,11 @@ class DSAPrivateNumbers(object): def private_key(self, backend): return backend.load_dsa_private_numbers(self) + + def __eq__(self, other): + return ( + self.x == other.x and self.public_numbers == other.public_numbers + ) + + def __ne__(self, other): + return not self == other diff --git a/src/cryptography/hazmat/primitives/asymmetric/ec.py b/src/cryptography/hazmat/primitives/asymmetric/ec.py index d9c41c19..cebd351c 100644 --- a/src/cryptography/hazmat/primitives/asymmetric/ec.py +++ b/src/cryptography/hazmat/primitives/asymmetric/ec.py @@ -161,6 +161,17 @@ class EllipticCurvePublicNumbers(object): x = utils.read_only_property("_x") y = utils.read_only_property("_y") + def __eq__(self, other): + return ( + self.x == other.x and + self.y == other.y and + self.curve.name == other.curve.name and + self.curve.key_size == other.curve.key_size + ) + + def __ne__(self, other): + return not self == other + class EllipticCurvePrivateNumbers(object): def __init__(self, private_value, public_numbers): @@ -184,3 +195,12 @@ class EllipticCurvePrivateNumbers(object): private_value = utils.read_only_property("_private_value") public_numbers = utils.read_only_property("_public_numbers") + + def __eq__(self, other): + return ( + self.private_value == other.private_value and + self.public_numbers == other.public_numbers + ) + + def __ne__(self, other): + return not self == other diff --git a/src/cryptography/hazmat/primitives/asymmetric/rsa.py b/src/cryptography/hazmat/primitives/asymmetric/rsa.py index 6aeed006..ff397225 100644 --- a/src/cryptography/hazmat/primitives/asymmetric/rsa.py +++ b/src/cryptography/hazmat/primitives/asymmetric/rsa.py @@ -160,6 +160,20 @@ class RSAPrivateNumbers(object): def private_key(self, backend): return backend.load_rsa_private_numbers(self) + def __eq__(self, other): + return ( + self.p == other.p and + self.q == other.q and + self.d == other.d and + self.dmp1 == other.dmp1 and + self.dmq1 == other.dmq1 and + self.iqmp == other.iqmp and + self.public_numbers == other.public_numbers + ) + + def __ne__(self, other): + return not self == other + class RSAPublicNumbers(object): def __init__(self, e, n): @@ -180,3 +194,9 @@ class RSAPublicNumbers(object): def __repr__(self): return "".format(self._e, self._n) + + def __eq__(self, other): + return self.e == other.e and self.n == other.n + + def __ne__(self, other): + return not self == other -- cgit v1.2.3 From 285edf80abd3b1b59384e1021d10773150b1c3c3 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Wed, 10 Dec 2014 18:21:46 -0600 Subject: add NotImplemented handling --- src/cryptography/hazmat/primitives/asymmetric/dsa.py | 9 +++++++++ src/cryptography/hazmat/primitives/asymmetric/ec.py | 6 ++++++ src/cryptography/hazmat/primitives/asymmetric/rsa.py | 6 ++++++ 3 files changed, 21 insertions(+) (limited to 'src') diff --git a/src/cryptography/hazmat/primitives/asymmetric/dsa.py b/src/cryptography/hazmat/primitives/asymmetric/dsa.py index 03e49ba5..9b06f3e6 100644 --- a/src/cryptography/hazmat/primitives/asymmetric/dsa.py +++ b/src/cryptography/hazmat/primitives/asymmetric/dsa.py @@ -60,6 +60,9 @@ class DSAParameterNumbers(object): return backend.load_dsa_parameter_numbers(self) def __eq__(self, other): + if not isinstance(other, DSAParameterNumbers): + return NotImplemented + return self.p == other.p and self.q == other.q and self.g == other.g def __ne__(self, other): @@ -86,6 +89,9 @@ class DSAPublicNumbers(object): return backend.load_dsa_public_numbers(self) def __eq__(self, other): + if not isinstance(other, DSAPublicNumbers): + return NotImplemented + return ( self.y == other.y and self.parameter_numbers == other.parameter_numbers @@ -114,6 +120,9 @@ class DSAPrivateNumbers(object): return backend.load_dsa_private_numbers(self) def __eq__(self, other): + if not isinstance(other, DSAPrivateNumbers): + return NotImplemented + return ( self.x == other.x and self.public_numbers == other.public_numbers ) diff --git a/src/cryptography/hazmat/primitives/asymmetric/ec.py b/src/cryptography/hazmat/primitives/asymmetric/ec.py index cebd351c..202f1c97 100644 --- a/src/cryptography/hazmat/primitives/asymmetric/ec.py +++ b/src/cryptography/hazmat/primitives/asymmetric/ec.py @@ -162,6 +162,9 @@ class EllipticCurvePublicNumbers(object): y = utils.read_only_property("_y") def __eq__(self, other): + if not isinstance(other, EllipticCurvePublicNumbers): + return NotImplemented + return ( self.x == other.x and self.y == other.y and @@ -197,6 +200,9 @@ class EllipticCurvePrivateNumbers(object): public_numbers = utils.read_only_property("_public_numbers") def __eq__(self, other): + if not isinstance(other, EllipticCurvePrivateNumbers): + return NotImplemented + return ( self.private_value == other.private_value and self.public_numbers == other.public_numbers diff --git a/src/cryptography/hazmat/primitives/asymmetric/rsa.py b/src/cryptography/hazmat/primitives/asymmetric/rsa.py index ff397225..0cc6b22b 100644 --- a/src/cryptography/hazmat/primitives/asymmetric/rsa.py +++ b/src/cryptography/hazmat/primitives/asymmetric/rsa.py @@ -161,6 +161,9 @@ class RSAPrivateNumbers(object): return backend.load_rsa_private_numbers(self) def __eq__(self, other): + if not isinstance(other, RSAPrivateNumbers): + return NotImplemented + return ( self.p == other.p and self.q == other.q and @@ -196,6 +199,9 @@ class RSAPublicNumbers(object): return "".format(self._e, self._n) def __eq__(self, other): + if not isinstance(other, RSAPublicNumbers): + return NotImplemented + return self.e == other.e and self.n == other.n def __ne__(self, other): -- cgit v1.2.3