This commit is contained in:
kunfei
2023-03-15 11:39:13 +08:00
parent 08612363dc
commit b900c4821e
2 changed files with 33 additions and 25 deletions

View File

@@ -1,81 +1,86 @@
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.asymmetric.KeyType
import java.io.InputStream
import cn.hutool.crypto.asymmetric.AsymmetricCrypto as HutoolAsymmetricCrypto
@Suppress("unused")
class AsymmetricCrypto(algorithm: String) : HutoolAsymmetricCrypto(algorithm) {
@Suppress("MemberVisibilityCanBePrivate")
fun setPrivateKey(key: ByteArray): AsymmetricCrypto {
setPrivateKey(
KeyUtil.generatePrivateKey(this.algorithm, key)
)
return this
}
fun setPrivateKey(key: String): AsymmetricCrypto = setPrivateKey(key.encodeToByteArray())
@Suppress("MemberVisibilityCanBePrivate")
fun setPublicKey(key: ByteArray): AsymmetricCrypto {
setPublicKey(
KeyUtil.generatePublicKey(this.algorithm, key)
)
return this
}
fun setPublicKey(key: String): AsymmetricCrypto = setPublicKey(key.encodeToByteArray())
private fun getKeyType(usePublicKey: Boolean? = true): KeyType {
return when {
usePublicKey == true -> KeyType.PublicKey
return when (usePublicKey) {
true -> KeyType.PublicKey
else -> KeyType.PrivateKey
}
}
@JvmOverloads
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")
}
}
@JvmOverloads
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")
}
}
@JvmOverloads
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")
}
}
@JvmOverloads
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")
}
}
@JvmOverloads
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")
}
}

View File

@@ -1,8 +1,9 @@
package io.legado.app.help.crypto
import cn.hutool.crypto.asymmetric.Sign as HutoolSign
import cn.hutool.crypto.KeyUtil
import cn.hutool.crypto.asymmetric.Sign as HutoolSign
@Suppress("unused")
class Sign(algorithm: String): HutoolSign(algorithm) {
fun setPrivateKey(key: ByteArray): Sign {
@@ -11,6 +12,7 @@ class Sign(algorithm: String): HutoolSign(algorithm) {
)
return this
}
fun setPrivateKey(key: String): Sign = setPrivateKey(key.encodeToByteArray())
fun setPublicKey(key: ByteArray): Sign {
@@ -19,6 +21,7 @@ class Sign(algorithm: String): HutoolSign(algorithm) {
)
return this
}
fun setPublicKey(key: String): Sign = setPublicKey(key.encodeToByteArray())
}