Last active
July 9, 2021 14:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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