feat: js支持createSymmetricCrypto函数, key iv自动识别Base64 Hex
This commit is contained in:
@@ -13,6 +13,8 @@ object AppPattern {
|
|||||||
|
|
||||||
//dataURL图片类型
|
//dataURL图片类型
|
||||||
val dataUriRegex = Regex("data:.*?;base64,(.*)")
|
val dataUriRegex = Regex("data:.*?;base64,(.*)")
|
||||||
|
//Base64字符串
|
||||||
|
val base64Regex = Regex("^[a-zA-Z0-9\\+\\/=]+$")
|
||||||
|
|
||||||
val nameRegex = Regex("\\s+作\\s*者.*|\\s+\\S+\\s+著")
|
val nameRegex = Regex("\\s+作\\s*者.*|\\s+\\S+\\s+著")
|
||||||
val authorRegex = Regex("^\\s*作\\s*者[::\\s]+|\\s+著")
|
val authorRegex = Regex("^\\s*作\\s*者[::\\s]+|\\s+著")
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import androidx.annotation.Keep
|
|||||||
import cn.hutool.crypto.digest.DigestUtil
|
import cn.hutool.crypto.digest.DigestUtil
|
||||||
import cn.hutool.crypto.digest.HMac
|
import cn.hutool.crypto.digest.HMac
|
||||||
import cn.hutool.core.util.HexUtil
|
import cn.hutool.core.util.HexUtil
|
||||||
|
import cn.hutool.crypto.symmetric.SymmetricCrypto
|
||||||
import io.legado.app.constant.AppConst
|
import io.legado.app.constant.AppConst
|
||||||
import io.legado.app.constant.AppConst.dateFormat
|
import io.legado.app.constant.AppConst.dateFormat
|
||||||
import io.legado.app.constant.AppLog
|
import io.legado.app.constant.AppLog
|
||||||
@@ -648,6 +649,29 @@ interface JsExtensions {
|
|||||||
|
|
||||||
//******************对称加密解密************************//
|
//******************对称加密解密************************//
|
||||||
|
|
||||||
|
/* 自动转换key iv 到ByteArray 支持base64 hex uft8*/
|
||||||
|
fun createSymmetricCrypto(
|
||||||
|
transformation: String,
|
||||||
|
key: String,
|
||||||
|
iv: String? = null
|
||||||
|
): SymmetricCrypto {
|
||||||
|
return createSymmetricCrypto(
|
||||||
|
transformation,
|
||||||
|
StringUtils.encodeStringToByteArray(key),
|
||||||
|
StringUtils.encodeStringToByteArray(iv)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 调用SymmetricCrypto key为null时使用随机密钥*/
|
||||||
|
fun createSymmetricCrypto(
|
||||||
|
transformation: String,
|
||||||
|
key: ByteArray? = null,
|
||||||
|
iv: ByteArray? = null
|
||||||
|
): SymmetricCrypto {
|
||||||
|
val symmetricCrypto = SymmetricCrypto(transformation, key)
|
||||||
|
return if (iv != null && !iv.isEmpty()) symmetricCrypto.setIv(iv) else symmetricCrypto
|
||||||
|
}
|
||||||
|
|
||||||
//******************消息摘要/散列消息鉴别码************************//
|
//******************消息摘要/散列消息鉴别码************************//
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ import android.icu.text.Collator
|
|||||||
import android.icu.util.ULocale
|
import android.icu.util.ULocale
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.text.Editable
|
import android.text.Editable
|
||||||
|
import cn.hutool.core.lang.Validator
|
||||||
import io.legado.app.constant.AppPattern.dataUriRegex
|
import io.legado.app.constant.AppPattern.dataUriRegex
|
||||||
|
import io.legado.app.constant.AppPattern.base64Regex
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@@ -39,6 +41,11 @@ fun String?.isDataUrl() =
|
|||||||
dataUriRegex.matches(it)
|
dataUriRegex.matches(it)
|
||||||
} ?: false
|
} ?: false
|
||||||
|
|
||||||
|
fun String?.isBase64() =
|
||||||
|
this?.let {
|
||||||
|
base64Regex.matches(it)
|
||||||
|
} ?: false
|
||||||
|
|
||||||
fun String?.isJson(): Boolean =
|
fun String?.isJson(): Boolean =
|
||||||
this?.run {
|
this?.run {
|
||||||
val str = this.trim()
|
val str = this.trim()
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
package io.legado.app.utils
|
package io.legado.app.utils
|
||||||
|
|
||||||
|
import android.util.Base64
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.text.TextUtils.isEmpty
|
import android.text.TextUtils.isEmpty
|
||||||
|
|
||||||
|
import cn.hutool.core.util.HexUtil
|
||||||
|
import cn.hutool.core.lang.Validator
|
||||||
|
|
||||||
import java.text.DecimalFormat
|
import java.text.DecimalFormat
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -287,4 +291,16 @@ object StringUtils {
|
|||||||
return buf.toString()
|
return buf.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自动识别Base64 Hex Utf8字符串 并转成ByteArray
|
||||||
|
*/
|
||||||
|
fun encodeStringToByteArray(str: String?): ByteArray? {
|
||||||
|
return when {
|
||||||
|
str.isNullOrBlank() -> null
|
||||||
|
Validator.isHex(str) -> HexUtil.decodeHex(str)
|
||||||
|
str.isBase64() -> Base64.decode(str, Base64.DEFAULT)
|
||||||
|
else -> str.encodeToByteArray()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user