Certificate pinning on Android: calculate pin-sha256 and use it correctly

For certificate pinning Android needs the SHA-256 hash of the public key in Base64. You can calculate it in the certificate converter with one click or with a single OpenSSL line. More important than the calculation is which key you pin, so the app doesn't break at the next certificate change.

What the pin is

The pin is the SHA-256 hash of the SubjectPublicKeyInfo (SPKI), that is the public key including its algorithm, Base64-encoded. Android (network_security_config.xml) and OkHttp (CertificatePinner) use the same format; OkHttp just prefixes it with sha256/. Because the pin belongs to the key, not the certificate, it stays the same when a certificate is renewed with the same key.

Calculate the pin

In the browser: drop the certificate on the “Inspect” tab of the certificate converter. The pin appears under “Fingerprints”, with a ready-made example configuration below it. The file never leaves your browser.

With OpenSSL from a file:

openssl x509 -in cert.pem -pubkey -noout \
  | openssl pkey -pubin -outform der \
  | openssl dgst -sha256 -binary | openssl enc -base64

From the running server, here the server certificate:

openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null \
  | openssl x509 -pubkey -noout \
  | openssl pkey -pubin -outform der \
  | openssl dgst -sha256 -binary | openssl enc -base64

OkHttp's error message helps too: when a pin fails, it lists the pins of every certificate in the chain.

Which key to pin?

Pin toAdvantageRisk
Server certificatetightestNew key at every renewal unless the same one is reused explicitly
Intermediatesurvives renewalsCAs rotate intermediates; Let's Encrypt most recently in May 2026 to Generation Y
Rootmost stableonly protects against other CAs
Your own internal CAfull controlneeds your own PKI

Let's Encrypt lifetimes are getting shorter: 64 days in 2027, 45 days in 2028. Pinning the server certificate only works if the key is kept at renewal, with certbot via --reuse-key. Pins on Let's Encrypt intermediates are risky. Apps that had pinned R10 to R14 or E5 to E9 lost their connection with the switch to Generation Y, see Let's Encrypt Generation Y.

Configuration

<!-- res/xml/network_security_config.xml -->
<network-security-config>
  <domain-config>
    <domain includeSubdomains="true">api.example.com</domain>
    <pin-set expiration="2027-12-31">
      <pin digest="SHA-256">current+pin+in+Base64=</pin>
      <pin digest="SHA-256">backup+pin+in+Base64=</pin>
    </pin-set>
  </domain-config>
</network-security-config>

// OkHttp
val pinner = CertificatePinner.Builder()
    .add("api.example.com", "sha256/current+pin+in+Base64=")
    .add("api.example.com", "sha256/backup+pin+in+Base64=")
    .build()

For admins: ask before every change

Pinning is the most common reason why an app breaks after a harmless certificate change. Before you change the key, CA or chain of an API server, ask the app developers whether and what they pin. You can then compare the pins of the current and the new certificate in the converter. For internal apps, your own CA as trust-anchors is often a better choice than pinning.

FAQ

Is the pin the same as the certificate's SHA-256 fingerprint?

No. The fingerprint is calculated over the whole certificate and changes at every renewal. The pin is calculated over the public key only and is Base64 instead of hex.

What happens if no pin matches any more?

The app can't connect until an update with a new pin is installed or the expiration date is reached.

Do I need pinning at all?

For most apps, no. It pays off for apps with especially sensitive data, but only with a backup pin and a process for certificate changes.

Sources and documentation