From 9442fa9afab67146c02a994463186b7c09908f77 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 24 Oct 2015 18:32:10 -0400 Subject: Fixed #2444 -- added an __hash__ to x509 Names --- src/cryptography/x509/name.py | 8 ++++++++ tests/test_x509.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/cryptography/x509/name.py b/src/cryptography/x509/name.py index 992786ef..9d93ece1 100644 --- a/src/cryptography/x509/name.py +++ b/src/cryptography/x509/name.py @@ -40,6 +40,9 @@ class NameAttribute(object): def __ne__(self, other): return not self == other + def __hash__(self): + return hash((self.oid, self.value)) + def __repr__(self): return "".format(self) @@ -60,6 +63,11 @@ class Name(object): def __ne__(self, other): return not self == other + def __hash__(self): + # TODO: this is relatively expensive, if this looks like a bottleneck + # for you, consider optimizing! + return hash(tuple(self._attributes)) + def __iter__(self): return iter(self._attributes) diff --git a/tests/test_x509.py b/tests/test_x509.py index cb05daf0..e7de2efd 100644 --- a/tests/test_x509.py +++ b/tests/test_x509.py @@ -2755,6 +2755,23 @@ class TestName(object): assert name1 != name2 assert name1 != object() + def test_hah(self): + name1 = x509.Name([ + x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'), + x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'), + ]) + name2 = x509.Name([ + x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'), + x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'), + ]) + name3 = x509.Name([ + x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'), + x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'), + ]) + + assert hash(name1) == hash(name2) + assert hash(name1) != hash(name3) + def test_repr(self): name = x509.Name([ x509.NameAttribute(NameOID.COMMON_NAME, u'cryptography.io'), -- cgit v1.2.3 From d94de17073ceedef035ec83f9ed35b02bf4ccdb3 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 24 Oct 2015 18:55:13 -0400 Subject: changelog entry --- CHANGELOG.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b64a0d18..08ac1093 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,7 @@ Changelog * Add support for AES key wrapping with :func:`~cryptography.hazmat.primitives.keywrap.aes_key_wrap` and :func:`~cryptography.hazmat.primitives.keywrap.aes_key_unwrap`. +* Added an ``__hash__`` method to :class:`~cryptography.x509.Name`. 1.0.2 - 2015-09-27 ~~~~~~~~~~~~~~~~~~ -- cgit v1.2.3 From 1aecec7012b554fef96a543e1c0581e00de53583 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 24 Oct 2015 19:26:02 -0400 Subject: typo --- tests/test_x509.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_x509.py b/tests/test_x509.py index e7de2efd..b9ea139b 100644 --- a/tests/test_x509.py +++ b/tests/test_x509.py @@ -2755,7 +2755,7 @@ class TestName(object): assert name1 != name2 assert name1 != object() - def test_hah(self): + def test_hash(self): name1 = x509.Name([ x509.NameAttribute(x509.ObjectIdentifier('oid'), u'value1'), x509.NameAttribute(x509.ObjectIdentifier('oid2'), u'value2'), -- cgit v1.2.3