Select and Convert bytes object from a list

I have tried everything in my mind but wasn’t successfull. Can anybody help me to select bytes object from a list bellow?

[('uid=xxxxxx,o=center', {'userCertificate;binary': [b'0\x82\x07q0\x82\x05Y\xa0\x03\x02\x01\x02\x02\x10`\ .....']})]

This is the result from a openldap search and I want to select the userCertificate part of the entry and convert it to a base64 text. When I manually select only the bytes object – the b’0\x82\x07q0\x82\x05Y\xa0\x03\x02\x01\x02\x02\x10`\ …..’ section I am able to convert it with base64.b64encode(bytes_variable) into a certificate string, but I have to automatize the selection of this bytes object. I have tried to convert this list into string and use regex to pickup only the part with b”, but then I am not able to convert it to correct certificate format. Any ideas how to do this?

>Solution :

You have a Python list containing a single tuple. The 2nd element of that tuple is a dictionary. That dictionary has a single key. Its value is a list containing one item.

So…

list_ = [('uid=xxxxxx,o=center', {'userCertificate;binary': [b'0\x82\x07q0\x82\x05Y\xa0\x03\x02\x01\x02\x02\x10']})]

bytes_object = list_[0][1]['userCertificate;binary'][0]

Note:

bytes truncated to make this syntactically correct

Leave a Reply