Last active
May 25, 2020 08:15
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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