Skip to content

Commit b763a75

Browse files
authored
Allows an x509.Name to be hashable if it contains unique_identifier (#271)
Fixes #268
1 parent 1a7a5ba commit b763a75

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

asn1crypto/x509.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from ._errors import unwrap
2828
from ._iri import iri_to_uri, uri_to_iri
2929
from ._ordereddict import OrderedDict
30-
from ._types import type_name, str_cls, bytes_to_list
30+
from ._types import type_name, str_cls, byte_cls, bytes_to_list
3131
from .algos import AlgorithmIdentifier, AnyAlgorithmIdentifier, DigestAlgorithm, SignedDigestAlgorithm
3232
from .core import (
3333
Any,
@@ -708,7 +708,13 @@ def prepped_value(self):
708708
"""
709709

710710
if self._prepped is None:
711-
self._prepped = self._ldap_string_prep(self['value'].native)
711+
native = self['value'].native
712+
if isinstance(native, str_cls):
713+
self._prepped = self._ldap_string_prep(native)
714+
else:
715+
if isinstance(native, byte_cls):
716+
native = ' ' + native.decode('cp1252') + ' '
717+
self._prepped = native
712718
return self._prepped
713719

714720
def __ne__(self, other):

tests/test_x509.py

+17
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,23 @@ def test_build_name_type_by_oid(self):
485485
self.assertEqual("unique_identifier", complex_name.chosen[3][0]['type'].native)
486486
self.assertIsInstance(complex_name.chosen[3][0]['value'], core.OctetBitString)
487487

488+
def test_name_hashable(self):
489+
complex_name = x509.Name.build(
490+
{
491+
'country_name': 'US',
492+
'tpm_manufacturer': 'Acme Co',
493+
'unique_identifier': b'\x04\x10\x03\x09',
494+
'email_address': '[email protected]'
495+
}
496+
)
497+
self.assertEqual(
498+
"country_name: us \x1e"
499+
"email_address: [email protected] \x1e"
500+
"tpm_manufacturer: acme co \x1e"
501+
"unique_identifier: \x04\x10\x03\x09 ",
502+
complex_name.hashable
503+
)
504+
488505
def test_v1_cert(self):
489506
cert = self._load_cert('chromium/ndn.ca.crt')
490507
tbs_cert = cert['tbs_certificate']

0 commit comments

Comments
 (0)