Skip to content

Instantly share code, notes, and snippets.

@jinal90
Last active May 25, 2020 08:15
Show Gist options
  • Save jinal90/c202dfeb2cb635b86b2f3a5def6acc49 to your computer and use it in GitHub Desktop.
Save jinal90/c202dfeb2cb635b86b2f3a5def6acc49 to your computer and use it in GitHub Desktop.
Code Snippet to implement SSL pinning using SHA256 key and CertificatePinner in OKHTTP
// Create CertificatePinner object which contatins list of domains and associated SHA-256 keys.
// Pin this CertificatePinner object while creating the OKHTTP client.
private val client = OkHttpClient.Builder()
.certificatePinner(
CertificatePinner.Builder()
.add("www.example.com", "sha256/7HIpactkIAq2Y49orFOOQKurWxmmSFZhBCoQYcRhJ3Y=")
.build())
.build()
// Implement rest service call
fun run() {
val request = Request.Builder()
.url("https://www.example.com/somerequest")
.build()
client.newCall(request).execute().use { response ->
if (response.isSuccessful) {
println("Success")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment