Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
1f60474c
Commit
1f60474c
authored
Mar 22, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix #154
parent
9029dab4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
32 deletions
+55
-32
mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/cryptor/ECDHAndroid.kt
...dMain/kotlin/net/mamoe/mirai/utils/cryptor/ECDHAndroid.kt
+32
-19
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/cryptor/ECDHJvm.kt
...c/jvmMain/kotlin/net/mamoe/mirai/utils/cryptor/ECDHJvm.kt
+23
-13
No files found.
mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/cryptor/ECDHAndroid.kt
View file @
1f60474c
...
...
@@ -11,6 +11,7 @@ package net.mamoe.mirai.utils.cryptor
import
android.annotation.SuppressLint
import
net.mamoe.mirai.utils.MiraiInternalAPI
import
net.mamoe.mirai.utils.MiraiLogger
import
net.mamoe.mirai.utils.MiraiPlatformUtils.md5
import
java.security.*
import
java.security.spec.ECGenParameterSpec
...
...
@@ -40,28 +41,40 @@ actual class ECDH actual constructor(actual val keyPair: ECDHKeyPair) {
actual
val
isECDHAvailable
:
Boolean
get
()
=
_isECDHAvailable
init
{
kotlin
.
runCatching
{
@SuppressLint
(
"PrivateApi"
)
val
clazz
=
Class
.
forName
(
"com.android.org.bouncycastle.jce.provider.BouncyCastleProvider"
,
true
,
ClassLoader
.
getSystemClassLoader
()
)
val
providerName
=
clazz
.
getDeclaredField
(
"PROVIDER_NAME"
).
get
(
null
)
as
String
if
(
Security
.
getProvider
(
providerName
)
!=
null
)
{
Security
.
removeProvider
(
providerName
)
fun
testECDH
()
{
ECDHKeyPairImpl
(
KeyPairGenerator
.
getInstance
(
"ECDH"
)
.
also
{
it
.
initialize
(
ECGenParameterSpec
(
"secp192k1"
))
}
.
genKeyPair
()).
let
{
calculateShareKey
(
it
.
privateKey
,
it
.
publicKey
)
}
Security
.
addProvider
(
clazz
.
newInstance
()
as
Provider
)
generateKeyPair
()
_isECDHAvailable
=
true
}.
exceptionOrNull
()
?.
let
{
throw
IllegalStateException
(
"cannot init BouncyCastle"
,
it
)
}
_isECDHAvailable
=
false
}
@SuppressLint
(
"PrivateApi"
)
if
(
kotlin
.
runCatching
{
testECDH
()
}.
isFailure
)
{
kotlin
.
runCatching
{
val
providerName
=
"BC"
if
(
Security
.
getProvider
(
providerName
)
!=
null
)
{
Security
.
removeProvider
(
providerName
)
}
@Suppress
(
"SpellCheckingInspection"
)
Security
.
addProvider
(
Class
.
forName
(
"com.android.org.bouncycastle.jce.provider.BouncyCastleProvider"
,
true
,
ClassLoader
.
getSystemClassLoader
()
).
newInstance
()
as
Provider
)
testECDH
()
_isECDHAvailable
=
true
}.
exceptionOrNull
()
?.
let
{
_isECDHAvailable
=
false
@Suppress
(
"DEPRECATION"
)
MiraiLogger
.
error
(
it
)
}
}
}
actual
fun
generateKeyPair
():
ECDHKeyPair
{
if
(!
isECDHAvailable
)
{
...
...
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/cryptor/ECDHJvm.kt
View file @
1f60474c
...
...
@@ -35,20 +35,30 @@ actual fun ECDH() = ECDH(ECDH.generateKeyPair())
actual
class
ECDH
actual
constructor
(
actual
val
keyPair
:
ECDHKeyPair
)
{
actual
companion
object
{
@Suppress
(
"ObjectPropertyName"
)
private
val
_isECDHAvailable
:
Boolean
=
kotlin
.
runCatching
{
if
(
Security
.
getProvider
(
BouncyCastleProvider
.
PROVIDER_NAME
)
!=
null
)
{
Security
.
removeProvider
(
BouncyCastleProvider
.
PROVIDER_NAME
)
}
Security
.
addProvider
(
BouncyCastleProvider
())
ECDHKeyPairImpl
(
KeyPairGenerator
.
getInstance
(
"ECDH"
)
.
also
{
it
.
initialize
(
ECGenParameterSpec
(
"secp192k1"
))
}
.
genKeyPair
()).
let
{
calculateShareKey
(
it
.
privateKey
,
it
.
publicKey
)
}
// try if it is working
}.
isSuccess
actual
val
isECDHAvailable
:
Boolean
init
{
isECDHAvailable
=
kotlin
.
runCatching
{
fun
testECDH
()
{
ECDHKeyPairImpl
(
KeyPairGenerator
.
getInstance
(
"ECDH"
)
.
also
{
it
.
initialize
(
ECGenParameterSpec
(
"secp192k1"
))
}
.
genKeyPair
()).
let
{
calculateShareKey
(
it
.
privateKey
,
it
.
publicKey
)
}
}
actual
val
isECDHAvailable
:
Boolean
get
()
=
_isECDHAvailable
if
(
kotlin
.
runCatching
{
testECDH
()
}.
isSuccess
)
{
return
@
runCatching
}
if
(
Security
.
getProvider
(
BouncyCastleProvider
.
PROVIDER_NAME
)
!=
null
)
{
Security
.
removeProvider
(
BouncyCastleProvider
.
PROVIDER_NAME
)
}
Security
.
addProvider
(
BouncyCastleProvider
())
testECDH
()
}.
isSuccess
}
actual
fun
generateKeyPair
():
ECDHKeyPair
{
if
(!
isECDHAvailable
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment