优化
This commit is contained in:
@@ -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 {
|
return when (usePublicKey) {
|
||||||
usePublicKey == true -> KeyType.PublicKey
|
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 {
|
return when (data) {
|
||||||
data is ByteArray -> decrypt(data, getKeyType(usePublicKey))
|
is ByteArray -> decrypt(data, getKeyType(usePublicKey))
|
||||||
data is String -> decrypt(data, getKeyType(usePublicKey))
|
is String -> decrypt(data, getKeyType(usePublicKey))
|
||||||
data is InputStream -> 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 {
|
return when (data) {
|
||||||
data is ByteArray -> String(decrypt(data, getKeyType(usePublicKey)))
|
is ByteArray -> String(decrypt(data, getKeyType(usePublicKey)))
|
||||||
data is String -> decryptStr(data, getKeyType(usePublicKey))
|
is String -> decryptStr(data, getKeyType(usePublicKey))
|
||||||
data is InputStream -> String(decrypt(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 {
|
return when (data) {
|
||||||
data is ByteArray -> encrypt(data, getKeyType(usePublicKey))
|
is ByteArray -> encrypt(data, getKeyType(usePublicKey))
|
||||||
data is String -> encrypt(data, getKeyType(usePublicKey))
|
is String -> encrypt(data, getKeyType(usePublicKey))
|
||||||
data is InputStream -> 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 {
|
return when (data) {
|
||||||
data is ByteArray -> encryptHex(data, getKeyType(usePublicKey))
|
is ByteArray -> encryptHex(data, getKeyType(usePublicKey))
|
||||||
data is String -> encryptHex(data, getKeyType(usePublicKey))
|
is String -> encryptHex(data, getKeyType(usePublicKey))
|
||||||
data is InputStream -> 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 {
|
return when (data) {
|
||||||
data is ByteArray -> encryptBase64(data, getKeyType(usePublicKey))
|
is ByteArray -> encryptBase64(data, getKeyType(usePublicKey))
|
||||||
data is String -> encryptBase64(data, getKeyType(usePublicKey))
|
is String -> encryptBase64(data, getKeyType(usePublicKey))
|
||||||
data is InputStream -> encryptBase64(data, getKeyType(usePublicKey))
|
is InputStream -> encryptBase64(data, getKeyType(usePublicKey))
|
||||||
else -> throw IllegalArgumentException("Unexpected input type")
|
else -> throw IllegalArgumentException("Unexpected input type")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package io.legado.app.help.crypto
|
package io.legado.app.help.crypto
|
||||||
|
|
||||||
import cn.hutool.crypto.asymmetric.Sign as HutoolSign
|
|
||||||
import cn.hutool.crypto.KeyUtil
|
import cn.hutool.crypto.KeyUtil
|
||||||
|
import cn.hutool.crypto.asymmetric.Sign as HutoolSign
|
||||||
|
|
||||||
|
@Suppress("unused")
|
||||||
class Sign(algorithm: String): HutoolSign(algorithm) {
|
class Sign(algorithm: String): HutoolSign(algorithm) {
|
||||||
|
|
||||||
fun setPrivateKey(key: ByteArray): Sign {
|
fun setPrivateKey(key: ByteArray): Sign {
|
||||||
@@ -11,6 +12,7 @@ class Sign(algorithm: String): HutoolSign(algorithm) {
|
|||||||
)
|
)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setPrivateKey(key: String): Sign = setPrivateKey(key.encodeToByteArray())
|
fun setPrivateKey(key: String): Sign = setPrivateKey(key.encodeToByteArray())
|
||||||
|
|
||||||
fun setPublicKey(key: ByteArray): Sign {
|
fun setPublicKey(key: ByteArray): Sign {
|
||||||
@@ -19,6 +21,7 @@ class Sign(algorithm: String): HutoolSign(algorithm) {
|
|||||||
)
|
)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setPublicKey(key: String): Sign = setPublicKey(key.encodeToByteArray())
|
fun setPublicKey(key: String): Sign = setPublicKey(key.encodeToByteArray())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user