Skip to content

Instantly share code, notes, and snippets.

@WeRockStar
Forked from pollux-/publickey
Last active February 2, 2022 15:45
Show Gist options
  • Save WeRockStar/e18dfeb3b550832e95e03d3deb2a72a6 to your computer and use it in GitHub Desktop.
Save WeRockStar/e18dfeb3b550832e95e03d3deb2a72a6 to your computer and use it in GitHub Desktop.
Extract the public key from a certificate
Generate public key + sha256 rely on X.509
1. openssl s_client -connect api.github.com:443 | openssl x509 -pubkey -noout | openssl rsa -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64
2. openssl x509 -noout -in certificate.pem -pubkey | openssl asn1parse -noout -inform pem -out public.key openssl dgst -sha256 -binary public.key | openssl enc -base64
3. openssl rsa -in my-rsa-key-file.key -outform der -pubout | openssl dgst -sha256 -binary | openssl enc -base64
4. openssl req -in my-signing-request.csr -pubkey -noout | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64
5. openssl x509 -in my-certificate.crt -pubkey -noout | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64
Covert PEM to DER
openssl x509 -outform der -in certificate.pem -out certificate.der
Covert DER to PEM
openssl x509 -inform der -in certificate.cer -out certificate.pem
Check or View Certificate (PEM)
openssl x509 -in cert.pem -text -noout
openssl x509 -in cert.cer -text -noout
openssl x509 -in cert.crt -text -noout
Check or View DER encoded Certificate
openssl x509 -in certificate.der -inform der -text -noout
openssl x509 -inform der -in cerfile.cer -noout -text
openssl x509 -inform pem -in cerfile.cer -noout -text