mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
fix
This commit is contained in:
@@ -215,15 +215,19 @@ java.createAsymmetricCrypto(transformation)
|
||||
|
||||
```
|
||||
> 解密加密参数 data支持ByteArray|Base64String|HexString|InputStream
|
||||
> usePublicKey: true 使用公钥 false 使用私钥
|
||||
```
|
||||
//解密为ByteArray String
|
||||
cipher.decrypt(data, usePublicKey)
|
||||
cipher.decryptStr(data, usePublicKey)
|
||||
cipher.decrypt(data, usePublicKey: Boolean? = true
|
||||
)
|
||||
cipher.decryptStr(data, usePublicKey: Boolean? = true
|
||||
)
|
||||
//加密为ByteArray Base64字符 HEX字符
|
||||
cipher.encrypt(data, usePublicKey)
|
||||
cipher.encryptBase64(data, usePublicKey)
|
||||
cipher.encryptHex(data, usePublicKey)
|
||||
cipher.encrypt(data, usePublicKey: Boolean? = true
|
||||
)
|
||||
cipher.encryptBase64(data, usePublicKey: Boolean? = true
|
||||
)
|
||||
cipher.encryptHex(data, usePublicKey: Boolean? = true
|
||||
)
|
||||
```
|
||||
* 签名
|
||||
> 输入参数 key 支持ByteArray|**Utf8String**
|
||||
|
||||
@@ -23,33 +23,61 @@ class AsymmetricCrypto(algorithm: String) : HutoolAsymmetricCrypto(algorithm) {
|
||||
}
|
||||
fun setPublicKey(key: String): AsymmetricCrypto = setPublicKey(key.encodeToByteArray())
|
||||
|
||||
private fun getKeyType(usePublicKey: Boolean): KeyType {
|
||||
private fun getKeyType(usePublicKey: Boolean? = true): KeyType {
|
||||
return when {
|
||||
usePublicKey == true -> KeyType.PublicKey
|
||||
else -> KeyType.PrivateKey
|
||||
}
|
||||
}
|
||||
|
||||
fun decrypt(data: ByteArray, usePublicKey: Boolean): ByteArray = decrypt(data, getKeyType(usePublicKey))
|
||||
fun decrypt(data: String, usePublicKey: Boolean): ByteArray = decrypt(data, getKeyType(usePublicKeye))
|
||||
fun decrypt(data: InputStream, usePublicKey: Boolean): ByteArray = decrypt(data, getKeyType(usePublicKey))
|
||||
@JvmOverloads
|
||||
fun decrypt(data: Any, usePublicKey: Boolean? = true): ByteArray {
|
||||
return {
|
||||
data is ByteArray -> decrypt(data, getKeyType(usePublicKey))
|
||||
data is String -> decrypt(data, getKeyType(usePublicKey))
|
||||
data is InputStream -> decrypt(data, getKeyType(usePublicKey))
|
||||
else -> throw IllegalArgumentException("Unexpected input type")
|
||||
}
|
||||
}
|
||||
|
||||
fun decryptStr(data: ByteArray, usePublicKey: Boolean): String = decryptStr(data, getKeyType(usePublicKey))
|
||||
fun decryptStr(data: String, usePublicKey: Boolean): String = decryptStr(data, getKeyType(usePublicKey))
|
||||
fun decryptStr(data: InputStream, usePublicKey: Boolean): String = decryptStr(data, getKeyType(usePublicKey))
|
||||
|
||||
fun encrypt(data: ByteArray, usePublicKey: Boolean): ByteArray = encrypt(data, getKeyType(usePublicKey))
|
||||
fun encrypt(data: String, usePublicKey: Boolean): ByteArray = encrypt(data, getKeyType(usePublicKey))
|
||||
fun encrypt(data: InputStream, usePublicKey: Boolean): ByteArray = encrypt(data, getKeyType(usePublicKey))
|
||||
@JvmOverloads
|
||||
fun decryptStr(data: Any, usePublicKey: Boolean? = true): String {
|
||||
return {
|
||||
data is ByteArray -> decryptStr(data, getKeyType(usePublicKey))
|
||||
data is String -> decryptStr(data, getKeyType(usePublicKey))
|
||||
data is InputStream -> decryptStr(data, getKeyType(usePublicKey))
|
||||
else -> throw IllegalArgumentException("Unexpected input type")
|
||||
}
|
||||
}
|
||||
|
||||
fun encryptHex(data: ByteArray, usePublicKey: Boolean): String = encryptHex(data, getKeyType(usePublicKey))
|
||||
fun encryptHex(data: String, usePublicKey: Boolean): String = encryptHex(data, getKeyType(usePublicKey))
|
||||
fun encryptHex(data: InputStream, usePublicKey: Boolean): String = encryptHex(data, getKeyType(usePublicKey))
|
||||
|
||||
fun encryptBase64(data: ByteArray, usePublicKey: Boolean): String =encryptBase64(data, getKeyType(usePublicKey))
|
||||
fun encryptBase64(data: String, usePublicKey: Boolean): String =encryptBase64(data, getKeyType(usePublicKey))
|
||||
fun encryptBase64(data: InputStream, usePublicKey: Boolean): String =encryptBase64(data, getKeyType(usePublicKey))
|
||||
@JvmOverloads
|
||||
fun encrypt(data: Any, usePublicKey: Boolean? = true): ByteArray {
|
||||
return {
|
||||
data is ByteArray -> encrypt(data, getKeyType(usePublicKey))
|
||||
data is String -> encrypt(data, getKeyType(usePublicKey))
|
||||
data is InputStream -> encrypt(data, getKeyType(usePublicKey))
|
||||
else -> throw IllegalArgumentException("Unexpected input type")
|
||||
}
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun encryptHex(data: Any, usePublicKey: Boolean? = true): String {
|
||||
return {
|
||||
data is ByteArray -> encryptHex(data, getKeyType(usePublicKey))
|
||||
data is String -> encryptHex(data, getKeyType(usePublicKey))
|
||||
data is InputStream -> encryptHex(data, getKeyType(usePublicKey))
|
||||
else -> throw IllegalArgumentException("Unexpected input type")
|
||||
}
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun encryptBase64(data: Any, usePublicKey: Boolean? = true): String {
|
||||
return {
|
||||
data is ByteArray -> encryptBase64(data, getKeyType(usePublicKey))
|
||||
data is String -> encryptBase64(data, getKeyType(usePublicKey))
|
||||
data is InputStream -> encryptBase64(data, getKeyType(usePublicKey))
|
||||
else -> throw IllegalArgumentException("Unexpected input type")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,10 +7,10 @@ fun setPublicKey(key: String): T
|
||||
fun setPrivateKey(key: ByteArray): T
|
||||
fun setPrivateKey(key: String): T
|
||||
|
||||
fun decrypt(data: Any, keyType: Int): ByteArray?
|
||||
fun decryptStr(data: Any, keyType: Int): String?
|
||||
fun decrypt(data: Any, usePublicKey: Boolean? = true): ByteArray?
|
||||
fun decryptStr(data: Any, usePublicKey: Boolean? = true): String?
|
||||
|
||||
fun encrypt(data: Any, keyType: Int): ByteArray?
|
||||
fun encryptHex(data: Any, keyType: Int): String?
|
||||
fun encryptBase64(data: Any, keyType: Int): String?
|
||||
fun encrypt(data: Any, usePublicKey: Boolean? = true): ByteArray?
|
||||
fun encryptHex(data: Any, usePublicKey: Boolean? = true): String?
|
||||
fun encryptBase64(data: Any, usePublicKey: Boolean? = true): String?
|
||||
```
|
||||
Reference in New Issue
Block a user