Skip to content

Instantly share code, notes, and snippets.

@berend
Last active January 25, 2017 20:26
Show Gist options
  • Save berend/4eec35806a7bb562cd4e676c047d7599 to your computer and use it in GitHub Desktop.
Save berend/4eec35806a7bb562cd4e676c047d7599 to your computer and use it in GitHub Desktop.
Obtain the ssl certificate from a given url and generate the fingerprint for it (SHA1 and SHA256)
from M2Crypto import X509
import ssl
def colon(s):
return ':'.join(s[i:i+2] for i in range(0, len(s), 2))
addr = ("www.example.com", 443)
cert_pem = ssl.get_server_certificate(addr)
x509 = X509.load_cert_string(str(cert_pem), X509.FORMAT_PEM)
sha1_fingerprint = x509.get_fingerprint('sha1')
sha256_fingerprint = x509.get_fingerprint('sha256')
print("Address: {}:{}\nSHA1: {}\nSHA256: {}".format(
addr[0], addr[1],
colon(sha1_fingerprint),
colon(sha256_fingerprint))
)
@berend
Copy link
Author

berend commented Jan 25, 2017

Some notes:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment