openssl pkey -pubin -in pub.pem -outform DER | openssl dgst -sha256 -c# SHA2-256(stdin)= 21:2c:...:9d# private key: derive the public half in the same pipeopenssl pkey -in key.pem -pubout -outform DER | openssl dgst -sha256 -c # certificate: extract the key first (NOT the same as -fingerprint!)openssl x509 -in cert.pem -pubkey -noout \ | openssl pkey -pubin -outform DER | openssl dgst -sha256 -copenssl pkey -pubin -in pub.pem -outform DER \ | openssl dgst -sha256 -binary | openssl base64How it works
-outform DERis the crucial flag: it re-encodes the key as canonical binary SPKI, so every tool hashing the same key gets the same digest.dgst -cprints colon-separated hex;-binary | base64produces the pin-sha256 form used in HTTP public key pinning.- The certificate variant pipes through
pkeydeliberately — it normalizes the extracted PEM back to DER before hashing.
Gotchas
openssl dgst -sha256 pub.pemhashes the PEM *text* — line endings and wrapping included — and matches nothing computed from DER. The-outform DERpipe is not optional.openssl x509 -fingerprint -sha256is the *certificate* fingerprint (hash of the entire cert), which changes on every renewal; the key fingerprint from this recipe survives re-issue with the same key.- Newer OpenSSL prints the digest labeled
SHA2-256(stdin)=; older builds saySHA256(stdin)=. Scripts that match the prefix textually break across versions — parse the hex after=instead.