Certificate pinning with OkHttp

A while back I wrote about “Using SSL right on Android“. If you are using OkHttp and/or Retrofit, this should be quite simple to implement. You can use the CertificatePinner class when building your OkHttpClient instance.

Example:

  1. CertificatePinner certificatePinner = new CertificatePinner.Builder()
  2.   .add("example.com", "your_pin")
  3.   .build();
  4.  
  5. new OkHttpClient.Builder()
  6.   .certificatePinner(certificatePinner)
  7.   .build();

More details about HTTPS and certificate pinning with OkHttp can be found on their Wiki, as well as a full example.

Leave a Reply

Your email address will not be published. Required fields are marked *