|
@@ -1,81 +1,86 @@
|
|
|
package io.legado.app.help.crypto
|
|
package io.legado.app.help.crypto
|
|
|
|
|
|
|
|
-import cn.hutool.crypto.asymmetric.AsymmetricCrypto as HutoolAsymmetricCrypto
|
|
|
|
|
-import cn.hutool.crypto.asymmetric.KeyType
|
|
|
|
|
import cn.hutool.crypto.KeyUtil
|
|
import cn.hutool.crypto.KeyUtil
|
|
|
|
|
+import cn.hutool.crypto.asymmetric.KeyType
|
|
|
import java.io.InputStream
|
|
import java.io.InputStream
|
|
|
|
|
+import cn.hutool.crypto.asymmetric.AsymmetricCrypto as HutoolAsymmetricCrypto
|
|
|
|
|
|
|
|
|
|
+@Suppress("unused")
|
|
|
class AsymmetricCrypto(algorithm: String) : HutoolAsymmetricCrypto(algorithm) {
|
|
class AsymmetricCrypto(algorithm: String) : HutoolAsymmetricCrypto(algorithm) {
|
|
|
|
|
|
|
|
|
|
+ @Suppress("MemberVisibilityCanBePrivate")
|
|
|
fun setPrivateKey(key: ByteArray): AsymmetricCrypto {
|
|
fun setPrivateKey(key: ByteArray): AsymmetricCrypto {
|
|
|
setPrivateKey(
|
|
setPrivateKey(
|
|
|
KeyUtil.generatePrivateKey(this.algorithm, key)
|
|
KeyUtil.generatePrivateKey(this.algorithm, key)
|
|
|
)
|
|
)
|
|
|
return this
|
|
return this
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
fun setPrivateKey(key: String): AsymmetricCrypto = setPrivateKey(key.encodeToByteArray())
|
|
fun setPrivateKey(key: String): AsymmetricCrypto = setPrivateKey(key.encodeToByteArray())
|
|
|
|
|
|
|
|
|
|
+ @Suppress("MemberVisibilityCanBePrivate")
|
|
|
fun setPublicKey(key: ByteArray): AsymmetricCrypto {
|
|
fun setPublicKey(key: ByteArray): AsymmetricCrypto {
|
|
|
setPublicKey(
|
|
setPublicKey(
|
|
|
KeyUtil.generatePublicKey(this.algorithm, key)
|
|
KeyUtil.generatePublicKey(this.algorithm, key)
|
|
|
)
|
|
)
|
|
|
return this
|
|
return this
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
fun setPublicKey(key: String): AsymmetricCrypto = setPublicKey(key.encodeToByteArray())
|
|
fun setPublicKey(key: String): AsymmetricCrypto = setPublicKey(key.encodeToByteArray())
|
|
|
|
|
|
|
|
private fun getKeyType(usePublicKey: Boolean? = true): KeyType {
|
|
private fun getKeyType(usePublicKey: Boolean? = true): KeyType {
|
|
|
- return when {
|
|
|
|
|
- usePublicKey == true -> KeyType.PublicKey
|
|
|
|
|
|
|
+ return when (usePublicKey) {
|
|
|
|
|
+ true -> KeyType.PublicKey
|
|
|
else -> KeyType.PrivateKey
|
|
else -> KeyType.PrivateKey
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@JvmOverloads
|
|
@JvmOverloads
|
|
|
fun decrypt(data: Any, usePublicKey: Boolean? = true): ByteArray {
|
|
fun decrypt(data: Any, usePublicKey: Boolean? = true): ByteArray {
|
|
|
- return when {
|
|
|
|
|
- data is ByteArray -> decrypt(data, getKeyType(usePublicKey))
|
|
|
|
|
- data is String -> decrypt(data, getKeyType(usePublicKey))
|
|
|
|
|
- data is InputStream -> decrypt(data, getKeyType(usePublicKey))
|
|
|
|
|
|
|
+ return when (data) {
|
|
|
|
|
+ is ByteArray -> decrypt(data, getKeyType(usePublicKey))
|
|
|
|
|
+ is String -> decrypt(data, getKeyType(usePublicKey))
|
|
|
|
|
+ is InputStream -> decrypt(data, getKeyType(usePublicKey))
|
|
|
else -> throw IllegalArgumentException("Unexpected input type")
|
|
else -> throw IllegalArgumentException("Unexpected input type")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@JvmOverloads
|
|
@JvmOverloads
|
|
|
fun decryptStr(data: Any, usePublicKey: Boolean? = true): String {
|
|
fun decryptStr(data: Any, usePublicKey: Boolean? = true): String {
|
|
|
- return when {
|
|
|
|
|
- data is ByteArray -> String(decrypt(data, getKeyType(usePublicKey)))
|
|
|
|
|
- data is String -> decryptStr(data, getKeyType(usePublicKey))
|
|
|
|
|
- data is InputStream -> String(decrypt(data, getKeyType(usePublicKey)))
|
|
|
|
|
|
|
+ return when (data) {
|
|
|
|
|
+ is ByteArray -> String(decrypt(data, getKeyType(usePublicKey)))
|
|
|
|
|
+ is String -> decryptStr(data, getKeyType(usePublicKey))
|
|
|
|
|
+ is InputStream -> String(decrypt(data, getKeyType(usePublicKey)))
|
|
|
else -> throw IllegalArgumentException("Unexpected input type")
|
|
else -> throw IllegalArgumentException("Unexpected input type")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@JvmOverloads
|
|
@JvmOverloads
|
|
|
fun encrypt(data: Any, usePublicKey: Boolean? = true): ByteArray {
|
|
fun encrypt(data: Any, usePublicKey: Boolean? = true): ByteArray {
|
|
|
- return when {
|
|
|
|
|
- data is ByteArray -> encrypt(data, getKeyType(usePublicKey))
|
|
|
|
|
- data is String -> encrypt(data, getKeyType(usePublicKey))
|
|
|
|
|
- data is InputStream -> encrypt(data, getKeyType(usePublicKey))
|
|
|
|
|
|
|
+ return when (data) {
|
|
|
|
|
+ is ByteArray -> encrypt(data, getKeyType(usePublicKey))
|
|
|
|
|
+ is String -> encrypt(data, getKeyType(usePublicKey))
|
|
|
|
|
+ is InputStream -> encrypt(data, getKeyType(usePublicKey))
|
|
|
else -> throw IllegalArgumentException("Unexpected input type")
|
|
else -> throw IllegalArgumentException("Unexpected input type")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@JvmOverloads
|
|
@JvmOverloads
|
|
|
fun encryptHex(data: Any, usePublicKey: Boolean? = true): String {
|
|
fun encryptHex(data: Any, usePublicKey: Boolean? = true): String {
|
|
|
- return when {
|
|
|
|
|
- data is ByteArray -> encryptHex(data, getKeyType(usePublicKey))
|
|
|
|
|
- data is String -> encryptHex(data, getKeyType(usePublicKey))
|
|
|
|
|
- data is InputStream -> encryptHex(data, getKeyType(usePublicKey))
|
|
|
|
|
|
|
+ return when (data) {
|
|
|
|
|
+ is ByteArray -> encryptHex(data, getKeyType(usePublicKey))
|
|
|
|
|
+ is String -> encryptHex(data, getKeyType(usePublicKey))
|
|
|
|
|
+ is InputStream -> encryptHex(data, getKeyType(usePublicKey))
|
|
|
else -> throw IllegalArgumentException("Unexpected input type")
|
|
else -> throw IllegalArgumentException("Unexpected input type")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@JvmOverloads
|
|
@JvmOverloads
|
|
|
fun encryptBase64(data: Any, usePublicKey: Boolean? = true): String {
|
|
fun encryptBase64(data: Any, usePublicKey: Boolean? = true): String {
|
|
|
- return when {
|
|
|
|
|
- data is ByteArray -> encryptBase64(data, getKeyType(usePublicKey))
|
|
|
|
|
- data is String -> encryptBase64(data, getKeyType(usePublicKey))
|
|
|
|
|
- data is InputStream -> encryptBase64(data, getKeyType(usePublicKey))
|
|
|
|
|
|
|
+ return when (data) {
|
|
|
|
|
+ is ByteArray -> encryptBase64(data, getKeyType(usePublicKey))
|
|
|
|
|
+ is String -> encryptBase64(data, getKeyType(usePublicKey))
|
|
|
|
|
+ is InputStream -> encryptBase64(data, getKeyType(usePublicKey))
|
|
|
else -> throw IllegalArgumentException("Unexpected input type")
|
|
else -> throw IllegalArgumentException("Unexpected input type")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|