M2Crypto: How to generate subjectKeyIdentifier / authorityKeyIdentifier

Scott David Daniels Scott.Daniels at Acm.Org
Fri Aug 7 10:37:37 EDT 2009


Matthias Güntert wrote:
>> M2Crypto has a couple of bugs open related that, with potential
>> workarounds that I haven't yet deemed polished enough to checkin, but
>> which might help you out:
>>
>> https://bugzilla.osafoundation.org/show_bug.cgi?id=7530
>> https://bugzilla.osafoundation.org/show_bug.cgi?id=12151
> 
> ... Generating the 'subjectKeyIdentifier':
 > ...
> def get_public_key_fingerprint(self):
> 	h = hashlib.new('sha1')
>     	h.update(self.keypair.as_der())
>     	client_serial = h.hexdigest().upper()
>     	client_serial_hex = ''
>     	for byte in xrange(20):
>     	 client_serial_hex += client_serial[byte*2] + client_serial[byte*2
> +1]
>     		if byte < 19:
>     			client_serial_hex += ':'
>         return client_serial_hex 
> ...

More tersely (code golf?):

     def get_public_key_fingerprint(self):
	digest = hashlib.sha1(self.keypair.as_der()).hexdigest().upper()
	return ':'.join(digest[pos : pos+2] for pos in range(0, 40, 2))

--Scott David Daniels
Scott.Daniels at Acm.Org


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list