Skip to content

Instantly share code, notes, and snippets.

@sezabass
Last active June 10, 2024 09:09
Show Gist options
  • Save sezabass/6a95deee8656571ce7046f3adc2ba573 to your computer and use it in GitHub Desktop.
Save sezabass/6a95deee8656571ce7046f3adc2ba573 to your computer and use it in GitHub Desktop.
Kotlin version for this Java solution: https://stackoverflow.com/a/58325184/1404279
package crawlers
import java.security.KeyManagementException
import java.security.NoSuchAlgorithmException
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
class SSLHelperKotlin {
companion object {
@JvmStatic
fun socketFactory(): SSLSocketFactory {
val trustAllCerts = arrayOf<TrustManager>(
object : X509TrustManager {
override fun getAcceptedIssuers() = arrayOf<X509Certificate>()
override fun checkClientTrusted(certs: Array<X509Certificate>, authType: String) {}
override fun checkServerTrusted(certs: Array<X509Certificate>, authType: String) {}
}
)
return try {
val sslContext: SSLContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, SecureRandom())
sslContext.socketFactory
} catch (e: NoSuchAlgorithmException) {
throw RuntimeException("Failed to create a SSL socket factory", e)
} catch (e: KeyManagementException) {
throw RuntimeException("Failed to create a SSL socket factory", e)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment