Skip to content

Instantly share code, notes, and snippets.

@doctaphred
Last active July 9, 2021 14:59
Show Gist options
  • Save doctaphred/15fd1539484e0c159ee2859bc17a1c0c to your computer and use it in GitHub Desktop.
Save doctaphred/15fd1539484e0c159ee2859bc17a1c0c to your computer and use it in GitHub Desktop.
Read an X.509 cert from stdin and print its formatted SHA1 fingerprint to stdout
#!/usr/bin/env python3
import sys
from base64 import b64decode
from hashlib import sha1
cert = sys.stdin.read()
lines = cert.splitlines()
assert lines[0] == '-----BEGIN CERTIFICATE-----'
assert lines[-1] == '-----END CERTIFICATE-----'
data_lines = lines[1:-1]
data = b64decode(''.join(data_lines))
fingerprint = sha1(data).hexdigest()
pairwise = zip(fingerprint[::2], fingerprint[1::2])
octets = map(''.join, pairwise)
formatted_fingerprint = ':'.join(octets).upper()
print(formatted_fingerprint)
# Or, just run `openssl x509 -fingerprint` and copy/paste the value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment