diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2015-07-06 18:52:41 -0400 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2015-07-06 18:52:41 -0400 |
commit | 969f3a50c491b76a39261e3413277c3135050d97 (patch) | |
tree | 7f8151f87a612adcc827120e879b5b2f6b5f4d2e | |
parent | 11bd1a13627098468707177a1e1fddfc92601ff3 (diff) | |
download | cryptography-969f3a50c491b76a39261e3413277c3135050d97.tar.gz cryptography-969f3a50c491b76a39261e3413277c3135050d97.tar.bz2 cryptography-969f3a50c491b76a39261e3413277c3135050d97.zip |
Fixed #2120 -- added __hash__ to x509.Cert
-rw-r--r-- | src/cryptography/hazmat/backends/openssl/x509.py | 4 | ||||
-rw-r--r-- | src/cryptography/x509.py | 6 | ||||
-rw-r--r-- | tests/test_x509.py | 23 |
3 files changed, 33 insertions, 0 deletions
diff --git a/src/cryptography/hazmat/backends/openssl/x509.py b/src/cryptography/hazmat/backends/openssl/x509.py index 7bfeb2ce..5e42b8e4 100644 --- a/src/cryptography/hazmat/backends/openssl/x509.py +++ b/src/cryptography/hazmat/backends/openssl/x509.py @@ -226,6 +226,10 @@ class _Certificate(object): def __ne__(self, other): return not self == other + def __hash__(self): + # TODO: Using fingerprint() with SHA256 is way overkill. + return hash(self.fingerprint(hashes.SHA256())) + def fingerprint(self, algorithm): h = hashes.Hash(algorithm, self._backend) bio = self._backend._create_mem_bio() diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py index afd28f20..8d28c35c 100644 --- a/src/cryptography/x509.py +++ b/src/cryptography/x509.py @@ -1324,6 +1324,12 @@ class Certificate(object): """ @abc.abstractmethod + def __hash__(self): + """ + Computes a hash. + """ + + @abc.abstractmethod def public_bytes(self, encoding): """ Serializes the certificate to PEM or DER format. diff --git a/tests/test_x509.py b/tests/test_x509.py index 90b3fe5f..d4a27bc3 100644 --- a/tests/test_x509.py +++ b/tests/test_x509.py @@ -347,6 +347,29 @@ class TestRSACertificate(object): assert cert != cert2 assert cert != object() + def test_hash(self, backend): + cert1 = _load_cert( + os.path.join("x509", "custom", "post2000utctime.pem"), + x509.load_pem_x509_certificate, + backend + ) + cert2 = _load_cert( + os.path.join("x509", "custom", "post2000utctime.pem"), + x509.load_pem_x509_certificate, + backend + ) + cert3 = _load_cert( + os.path.join( + "x509", "PKITS_data", "certs", + "ValidGeneralizedTimenotAfterDateTest8EE.crt" + ), + x509.load_der_x509_certificate, + backend + ) + + assert hash(cert1) == hash(cert2) + assert hash(cert1) != hash(cert3) + def test_version_1_cert(self, backend): cert = _load_cert( os.path.join("x509", "v1_cert.pem"), |