mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 02:20:14 +08:00
Fix #154
This commit is contained in:
parent
9029dab4e1
commit
1f60474cb0
@ -11,6 +11,7 @@ package net.mamoe.mirai.utils.cryptor
|
|||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import net.mamoe.mirai.utils.MiraiInternalAPI
|
import net.mamoe.mirai.utils.MiraiInternalAPI
|
||||||
|
import net.mamoe.mirai.utils.MiraiLogger
|
||||||
import net.mamoe.mirai.utils.MiraiPlatformUtils.md5
|
import net.mamoe.mirai.utils.MiraiPlatformUtils.md5
|
||||||
import java.security.*
|
import java.security.*
|
||||||
import java.security.spec.ECGenParameterSpec
|
import java.security.spec.ECGenParameterSpec
|
||||||
@ -40,28 +41,40 @@ actual class ECDH actual constructor(actual val keyPair: ECDHKeyPair) {
|
|||||||
actual val isECDHAvailable: Boolean get() = _isECDHAvailable
|
actual val isECDHAvailable: Boolean get() = _isECDHAvailable
|
||||||
|
|
||||||
init {
|
init {
|
||||||
kotlin.runCatching {
|
fun testECDH() {
|
||||||
@SuppressLint("PrivateApi")
|
ECDHKeyPairImpl(
|
||||||
val clazz = Class.forName(
|
KeyPairGenerator.getInstance("ECDH")
|
||||||
"com.android.org.bouncycastle.jce.provider.BouncyCastleProvider",
|
.also { it.initialize(ECGenParameterSpec("secp192k1")) }
|
||||||
true,
|
.genKeyPair()).let {
|
||||||
ClassLoader.getSystemClassLoader()
|
calculateShareKey(it.privateKey, it.publicKey)
|
||||||
)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val providerName = clazz.getDeclaredField("PROVIDER_NAME").get(null) as String
|
@SuppressLint("PrivateApi")
|
||||||
|
if (kotlin.runCatching { testECDH() }.isFailure) {
|
||||||
|
kotlin.runCatching {
|
||||||
|
val providerName = "BC"
|
||||||
|
|
||||||
if (Security.getProvider(providerName) != null) {
|
if (Security.getProvider(providerName) != null) {
|
||||||
Security.removeProvider(providerName)
|
Security.removeProvider(providerName)
|
||||||
}
|
}
|
||||||
Security.addProvider(clazz.newInstance() as Provider)
|
@Suppress("SpellCheckingInspection")
|
||||||
generateKeyPair()
|
Security.addProvider(
|
||||||
|
Class.forName(
|
||||||
|
"com.android.org.bouncycastle.jce.provider.BouncyCastleProvider",
|
||||||
|
true,
|
||||||
|
ClassLoader.getSystemClassLoader()
|
||||||
|
).newInstance() as Provider
|
||||||
|
)
|
||||||
|
testECDH()
|
||||||
_isECDHAvailable = true
|
_isECDHAvailable = true
|
||||||
}.exceptionOrNull()?.let {
|
}.exceptionOrNull()?.let {
|
||||||
throw IllegalStateException("cannot init BouncyCastle", it)
|
|
||||||
}
|
|
||||||
_isECDHAvailable = false
|
_isECDHAvailable = false
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
MiraiLogger.error(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
actual fun generateKeyPair(): ECDHKeyPair {
|
actual fun generateKeyPair(): ECDHKeyPair {
|
||||||
if (!isECDHAvailable) {
|
if (!isECDHAvailable) {
|
||||||
|
@ -35,20 +35,30 @@ actual fun ECDH() = ECDH(ECDH.generateKeyPair())
|
|||||||
|
|
||||||
actual class ECDH actual constructor(actual val keyPair: ECDHKeyPair) {
|
actual class ECDH actual constructor(actual val keyPair: ECDHKeyPair) {
|
||||||
actual companion object {
|
actual companion object {
|
||||||
@Suppress("ObjectPropertyName")
|
actual val isECDHAvailable: Boolean
|
||||||
private val _isECDHAvailable: Boolean = kotlin.runCatching {
|
|
||||||
|
init {
|
||||||
|
isECDHAvailable = kotlin.runCatching {
|
||||||
|
fun testECDH() {
|
||||||
|
ECDHKeyPairImpl(
|
||||||
|
KeyPairGenerator.getInstance("ECDH")
|
||||||
|
.also { it.initialize(ECGenParameterSpec("secp192k1")) }
|
||||||
|
.genKeyPair()).let {
|
||||||
|
calculateShareKey(it.privateKey, it.publicKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (kotlin.runCatching { testECDH() }.isSuccess) {
|
||||||
|
return@runCatching
|
||||||
|
}
|
||||||
|
|
||||||
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) != null) {
|
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) != null) {
|
||||||
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME)
|
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME)
|
||||||
}
|
}
|
||||||
Security.addProvider(BouncyCastleProvider())
|
Security.addProvider(BouncyCastleProvider())
|
||||||
ECDHKeyPairImpl(KeyPairGenerator.getInstance("ECDH")
|
testECDH()
|
||||||
.also { it.initialize(ECGenParameterSpec("secp192k1")) }
|
|
||||||
.genKeyPair()).let {
|
|
||||||
calculateShareKey(it.privateKey, it.publicKey)
|
|
||||||
} // try if it is working
|
|
||||||
}.isSuccess
|
}.isSuccess
|
||||||
|
}
|
||||||
actual val isECDHAvailable: Boolean get() = _isECDHAvailable
|
|
||||||
|
|
||||||
actual fun generateKeyPair(): ECDHKeyPair {
|
actual fun generateKeyPair(): ECDHKeyPair {
|
||||||
if (!isECDHAvailable) {
|
if (!isECDHAvailable) {
|
||||||
|
Loading…
Reference in New Issue
Block a user