mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-09 09:50:16 +08:00
Add default values
This commit is contained in:
parent
7e96aa5a12
commit
b7e4dc0772
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
package net.mamoe.mirai.utils.cryptor
|
package net.mamoe.mirai.utils.cryptor
|
||||||
|
|
||||||
|
import net.mamoe.mirai.utils.io.chunkedHexToBytes
|
||||||
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.*
|
||||||
@ -21,12 +22,19 @@ actual typealias ECDHPrivateKey = PrivateKey
|
|||||||
actual typealias ECDHPublicKey = PublicKey
|
actual typealias ECDHPublicKey = PublicKey
|
||||||
|
|
||||||
actual class ECDHKeyPair(
|
actual class ECDHKeyPair(
|
||||||
private val delegate: KeyPair
|
private val delegate: KeyPair?
|
||||||
) {
|
) {
|
||||||
actual val privateKey: ECDHPrivateKey get() = delegate.private
|
actual val privateKey: ECDHPrivateKey get() = delegate?.private ?: error("ECDH is not available")
|
||||||
actual val publicKey: ECDHPublicKey get() = delegate.public
|
actual val publicKey: ECDHPublicKey get() = delegate?.public ?: defaultPublicKey
|
||||||
|
|
||||||
actual val initialShareKey: ByteArray = ECDH.calculateShareKey(privateKey, initialPublicKey)
|
actual val initialShareKey: ByteArray = if (delegate == null) {
|
||||||
|
defaultShareKey
|
||||||
|
} else ECDH.calculateShareKey(privateKey, initialPublicKey)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
internal val defaultPublicKey = "020b03cf3d99541f29ffec281bebbd4ea211292ac1f53d7128".chunkedHexToBytes().adjustToPublicKey()
|
||||||
|
internal val defaultShareKey = "4da0f614fc9f29c2054c77048a6566d7".chunkedHexToBytes()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("FunctionName")
|
@Suppress("FunctionName")
|
||||||
@ -34,22 +42,33 @@ 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 {
|
||||||
|
private var isECDHAvailable = false
|
||||||
init {
|
init {
|
||||||
Security.addProvider(BouncyCastleProvider())
|
if (kotlin.runCatching {
|
||||||
|
Security.addProvider(BouncyCastleProvider())
|
||||||
|
}.exceptionOrNull() == null) {
|
||||||
|
isECDHAvailable = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun generateKeyPair(): ECDHKeyPair {
|
actual fun generateKeyPair(): ECDHKeyPair {
|
||||||
return ECDHKeyPair(KeyPairGenerator.getInstance("EC", "BC").apply { initialize(ECGenParameterSpec("secp192k1")) }.genKeyPair())
|
return if (!isECDHAvailable) {
|
||||||
|
ECDHKeyPair(null)
|
||||||
|
} else ECDHKeyPair(KeyPairGenerator.getInstance("EC", "BC").apply { initialize(ECGenParameterSpec("secp192k1")) }.genKeyPair())
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun calculateShareKey(
|
actual fun calculateShareKey(
|
||||||
privateKey: ECDHPrivateKey,
|
privateKey: ECDHPrivateKey,
|
||||||
publicKey: ECDHPublicKey
|
publicKey: ECDHPublicKey
|
||||||
): ByteArray {
|
): ByteArray {
|
||||||
val instance = KeyAgreement.getInstance("ECDH", "BC")
|
return if (!isECDHAvailable) {
|
||||||
instance.init(privateKey)
|
ECDHKeyPair.defaultShareKey
|
||||||
instance.doPhase(publicKey, true)
|
} else {
|
||||||
return md5(instance.generateSecret())
|
val instance = KeyAgreement.getInstance("ECDH", "BC")
|
||||||
|
instance.init(privateKey)
|
||||||
|
instance.doPhase(publicKey, true)
|
||||||
|
md5(instance.generateSecret())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun constructPublicKey(key: ByteArray): ECDHPublicKey {
|
actual fun constructPublicKey(key: ByteArray): ECDHPublicKey {
|
||||||
|
Loading…
Reference in New Issue
Block a user