Skip to content

Commit

Permalink
Allows an x509.Name to be hashable if it contains unique_identifier
Browse files Browse the repository at this point in the history
Fixes #268
  • Loading branch information
wbond committed Nov 3, 2023
1 parent 8609892 commit 505429b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 8 additions & 2 deletions asn1crypto/x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from ._errors import unwrap
from ._iri import iri_to_uri, uri_to_iri
from ._ordereddict import OrderedDict
from ._types import type_name, str_cls, bytes_to_list
from ._types import type_name, str_cls, byte_cls, bytes_to_list
from .algos import AlgorithmIdentifier, AnyAlgorithmIdentifier, DigestAlgorithm, SignedDigestAlgorithm
from .core import (
Any,
Expand Down Expand Up @@ -708,7 +708,13 @@ def prepped_value(self):
"""

if self._prepped is None:
self._prepped = self._ldap_string_prep(self['value'].native)
native = self['value'].native
if isinstance(native, str_cls):
self._prepped = self._ldap_string_prep(native)
else:
if isinstance(native, byte_cls):
native = ' ' + native.decode('cp1252') + ' '
self._prepped = native
return self._prepped

def __ne__(self, other):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,23 @@ def test_build_name_printable(self):
self.assertIsInstance(printable_name.chosen[2][0]['value'].chosen, core.PrintableString)
self.assertEqual('common_name', printable_name.chosen[2][0]['type'].native)

def test_name_hashable(self):
complex_name = x509.Name.build(
{
'country_name': 'US',
'tpm_manufacturer': 'Acme Co',
'unique_identifier': b'\x04\x10\x03\x09',
'email_address': 'will@codexns.io'
}
)
self.assertEqual(
"country_name: us \x1e"
"email_address: will@codexns.io \x1e"
"tpm_manufacturer: acme co \x1e"
"unique_identifier: \x04\x10\x03\x09 ",
complex_name.hashable
)

def test_v1_cert(self):
cert = self._load_cert('chromium/ndn.ca.crt')
tbs_cert = cert['tbs_certificate']
Expand Down

0 comments on commit 505429b

Please sign in to comment.