mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
更新非对称加密
This commit is contained in:
@@ -3,10 +3,11 @@ package io.legado.app.help
|
||||
import android.util.Base64
|
||||
import cn.hutool.crypto.digest.DigestUtil
|
||||
import cn.hutool.crypto.digest.HMac
|
||||
import cn.hutool.crypto.asymmetric.*
|
||||
import cn.hutool.crypto.symmetric.SymmetricCrypto
|
||||
import io.legado.app.help.crypto.AsymmetricCrypto
|
||||
import io.legado.app.help.crypto.Sign
|
||||
import io.legado.app.utils.MD5Utils
|
||||
import io.legado.app.utils.*
|
||||
|
||||
|
||||
/**
|
||||
* js加解密扩展类, 在js中通过java变量调用
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.legado.app.help
|
||||
|
||||
import cn.hutool.crypto.asymmetric.AsymmetricCrypto as HutoolAsymmetricCrypto
|
||||
import cn.hutool.crypto.asymmetric.KeyType
|
||||
import cn.hutool.crypto.KeyUtil
|
||||
import java.io.InputStream
|
||||
|
||||
class AsymmetricCrypto(algorithm: String) : HutoolAsymmetricCrypto(algorithm) {
|
||||
|
||||
fun setPrivateKey(key: ByteArray): AsymmetricCrypto {
|
||||
return setPrivateKey(
|
||||
KeyUtil.generatePrivateKey(this.algorithm, key)
|
||||
)
|
||||
}
|
||||
fun setPublicKey(key: String): AsymmetricCrypto = setPrivateKey(key.encodeToByteArray())
|
||||
|
||||
fun setPublicKey(key: ByteArray): AsymmetricCrypto {
|
||||
return setPublicKey(
|
||||
KeyUtil.generatePublicKey(this.algorithm, key)
|
||||
)
|
||||
}
|
||||
fun setPrivateKey(key: String): AsymmetricCrypto = setPrivateKey(key.encodeToByteArray())
|
||||
|
||||
private fun getKeyType(): KeyType {
|
||||
return when {
|
||||
this.publicKey != null -> KeyType.PublicKey
|
||||
this.privateKey != null -> KeyType.PrivateKey
|
||||
else -> KeyType.SecretKey
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> cryptoDelegate(
|
||||
data: Any,
|
||||
func: (Any, KeyType) -> T
|
||||
): T {
|
||||
val keyType = getKeyType()
|
||||
return when {
|
||||
data is ByteArray -> func.invoke(data, keyType)
|
||||
data is String -> func.invoke(data, keyType)
|
||||
data is InputStream -> func.invoke(data, keyType)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
fun decrypt(data: Any): ByteArray? = cryptoDelegate<ByteArray?>(data, decrypt)
|
||||
fun decryptStr(data: Any): String? = cryptoDelegate<String?>(data, decryptStr)
|
||||
|
||||
fun encrypt(data: Any): ByteArray? = cryptoDelegate<ByteArray?>(data, encrypt)
|
||||
fun encryptHex(data: Any): String? = cryptoDelegate<String?>(data, encryptHex)
|
||||
fun encryptBase64(data: Any): String? =cryptoDelegate<String?>(data, encryptBase64)
|
||||
|
||||
}
|
||||
17
app/src/main/java/io/legado/app/help/crypto/README.md
Normal file
17
app/src/main/java/io/legado/app/help/crypto/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
https://github.com/gedoor/legado/pull/2880
|
||||
|
||||
非对称加密一般只能知道其中一个密钥,而RhinoJs调用java方法不能传入null, 因此提供以下重载函数
|
||||
```kotlin
|
||||
fun setPublicKey(key: ByteArray): T
|
||||
fun setPublicKey(key: String): T
|
||||
fun setPrivateKey(key: ByteArray): T
|
||||
fun setPrivateKey(key: String): T
|
||||
|
||||
//自动从公钥开始尝试
|
||||
fun decrypt(data: Any): ByteArray?
|
||||
fun decryptStr(data: Any): String?
|
||||
|
||||
fun encrypt(data: Any): ByteArray?
|
||||
fun encryptHex(data: Any): String?
|
||||
fun encryptBase64(data: Any): String?
|
||||
```
|
||||
23
app/src/main/java/io/legado/app/help/crypto/Sign.kt
Normal file
23
app/src/main/java/io/legado/app/help/crypto/Sign.kt
Normal file
@@ -0,0 +1,23 @@
|
||||
package io.legado.app.help
|
||||
|
||||
import cn.hutool.crypto.asymmetric.Sign as HutoolSign
|
||||
import cn.hutool.crypto.KeyUtil
|
||||
|
||||
class Sign(algorithm: String): HutoolSign(algorithm) {
|
||||
|
||||
fun setPrivateKey(key: ByteArray): Sign {
|
||||
return setPrivateKey(
|
||||
KeyUtil.generatePrivateKey(this.algorithm, key)
|
||||
)
|
||||
}
|
||||
fun setPublicKey(key: String): Sign = setPrivateKey(key.encodeToByteArray())
|
||||
|
||||
fun setPublicKey(key: ByteArray): Sign {
|
||||
return setPublicKey(
|
||||
KeyUtil.generatePublicKey(this.algorithm, key)
|
||||
)
|
||||
}
|
||||
fun setPrivateKey(key: String): Sign = setPrivateKey(key.encodeToByteArray())
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user