Skip to content

Instantly share code, notes, and snippets.

@virendersran01
Forked from whytarun/CertificateHelper.kt
Created October 8, 2023 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virendersran01/9039a6b12f763c732b038edfb8ddbe31 to your computer and use it in GitHub Desktop.
Save virendersran01/9039a6b12f763c732b038edfb8ddbe31 to your computer and use it in GitHub Desktop.
SSL Support
class CertificateHelper @Inject constructor(@ApplicationContext context: Context) {
private val applicationContext = context
fun createTrustManagers(): Array<TrustManager> {
val certificateInputStream = applicationContext.resources.openRawResource(
R.raw.test)
val certificateFactory = CertificateFactory.getInstance("X.509")
val certificate = certificateFactory.generateCertificate(certificateInputStream)
val trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm())
val keyStore = KeyStore.getInstance(KeyStore.getDefaultType())
keyStore.load(null)
keyStore.setCertificateEntry("test", certificate)
trustManagerFactory.init(keyStore)
return trustManagerFactory.trustManagers
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment