mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-09 09:47:15 +08:00
Fix ECDH curve
This commit is contained in:
parent
e878307c13
commit
89c614d0a1
@ -12,6 +12,7 @@ package net.mamoe.mirai.utils.cryptor
|
|||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import net.mamoe.mirai.utils.md5
|
import net.mamoe.mirai.utils.md5
|
||||||
import java.security.*
|
import java.security.*
|
||||||
|
import java.security.spec.ECGenParameterSpec
|
||||||
import java.security.spec.X509EncodedKeySpec
|
import java.security.spec.X509EncodedKeySpec
|
||||||
import javax.crypto.KeyAgreement
|
import javax.crypto.KeyAgreement
|
||||||
|
|
||||||
@ -60,11 +61,14 @@ actual class ECDH actual constructor(actual val keyPair: ECDHKeyPair) {
|
|||||||
_isECDHAvailable = false
|
_isECDHAvailable = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
actual fun generateKeyPair(): ECDHKeyPair {
|
actual fun generateKeyPair(): ECDHKeyPair {
|
||||||
if (!isECDHAvailable) {
|
if (!isECDHAvailable) {
|
||||||
return ECDHKeyPair.DefaultStub
|
return ECDHKeyPair.DefaultStub
|
||||||
}
|
}
|
||||||
return ECDHKeyPairImpl(KeyPairGenerator.getInstance("ECDH").genKeyPair())
|
return ECDHKeyPairImpl(KeyPairGenerator.getInstance("ECDH")
|
||||||
|
.also { it.initialize(ECGenParameterSpec("secp192k1")) }
|
||||||
|
.genKeyPair())
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun calculateShareKey(
|
actual fun calculateShareKey(
|
||||||
|
@ -12,6 +12,7 @@ package net.mamoe.mirai.utils.cryptor
|
|||||||
import net.mamoe.mirai.utils.md5
|
import net.mamoe.mirai.utils.md5
|
||||||
import org.bouncycastle.jce.provider.BouncyCastleProvider
|
import org.bouncycastle.jce.provider.BouncyCastleProvider
|
||||||
import java.security.*
|
import java.security.*
|
||||||
|
import java.security.spec.ECGenParameterSpec
|
||||||
import java.security.spec.X509EncodedKeySpec
|
import java.security.spec.X509EncodedKeySpec
|
||||||
import javax.crypto.KeyAgreement
|
import javax.crypto.KeyAgreement
|
||||||
|
|
||||||
@ -34,24 +35,23 @@ 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")
|
@Suppress("ObjectPropertyName")
|
||||||
private val _isECDHAvailable: Boolean
|
private val _isECDHAvailable: Boolean = kotlin.runCatching {
|
||||||
actual val isECDHAvailable: Boolean get() = _isECDHAvailable
|
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) != null) {
|
||||||
|
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME)
|
||||||
|
}
|
||||||
|
Security.addProvider(BouncyCastleProvider())
|
||||||
|
generateKeyPair() // try if it is working
|
||||||
|
}.isSuccess
|
||||||
|
|
||||||
init {
|
actual val isECDHAvailable: Boolean get() = _isECDHAvailable
|
||||||
_isECDHAvailable = kotlin.runCatching {
|
|
||||||
if (Security.getProvider("BouncyCastle") != null) {
|
|
||||||
Security.removeProvider("BouncyCastle")
|
|
||||||
}
|
|
||||||
Security.addProvider(BouncyCastleProvider())
|
|
||||||
generateKeyPair() // try if it is working
|
|
||||||
}.isSuccess
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun generateKeyPair(): ECDHKeyPair {
|
actual fun generateKeyPair(): ECDHKeyPair {
|
||||||
if (!isECDHAvailable) {
|
if (!isECDHAvailable) {
|
||||||
return ECDHKeyPair.DefaultStub
|
return ECDHKeyPair.DefaultStub
|
||||||
}
|
}
|
||||||
return ECDHKeyPairImpl(KeyPairGenerator.getInstance("ECDH").genKeyPair())
|
return ECDHKeyPairImpl(KeyPairGenerator.getInstance("ECDH")
|
||||||
|
.also { it.initialize(ECGenParameterSpec("secp192k1")) }
|
||||||
|
.genKeyPair())
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun calculateShareKey(
|
actual fun calculateShareKey(
|
||||||
|
Loading…
Reference in New Issue
Block a user