TLDR: Getting a SSL Certificate Fingerprint in Python


Posted:   |  More posts about Snippets Python

For a project I am working on I need to know the SHA1 hash (the fingerprint) of a DER encoded (binary) certificate file. I found it strange that nobody had offered up a here's a hunk of code for something so simple. Any novice programmer would probably eventually arrive at this solution on their own but for the sake of those Googling, here it is.

 1 import hashlib # Comes with Python.
 2 
 3 h = hashlib.sha1()
 4 with open('mycertfile.crt', 'rb') as crt:
 5     h.update(crt.read())
 6 
 7 cert_hash = h.hexdigest()
 8 
 9 print(cert_hash)

This code produces the same result as running sha1sum mycertfile.crt using the sha1sum program in coreutils (included in most Linux distributions).

If you want to copy and paste it's easiest to do so from the source file for this post.

Comments powered by Disqus
Contents © 2015 Daniel Devine - Nikola Powered - Flattr Me! Flattr this Source