From 0170b4fe217d2bc6426bd61a1ace23db97862046 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sun, 16 Oct 2022 12:21:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20js=E6=94=AF=E6=8C=81createSymmetricCryp?= =?UTF-8?q?to=E5=87=BD=E6=95=B0,=20key=20iv=E8=87=AA=E5=8A=A8=E8=AF=86?= =?UTF-8?q?=E5=88=ABBase64=20Hex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/AppPattern.kt | 2 ++ .../java/io/legado/app/help/JsExtensions.kt | 24 +++++++++++++++++++ .../io/legado/app/utils/StringExtensions.kt | 7 ++++++ .../java/io/legado/app/utils/StringUtils.kt | 16 +++++++++++++ 4 files changed, 49 insertions(+) diff --git a/app/src/main/java/io/legado/app/constant/AppPattern.kt b/app/src/main/java/io/legado/app/constant/AppPattern.kt index 731a7296a..cfecd8bcc 100644 --- a/app/src/main/java/io/legado/app/constant/AppPattern.kt +++ b/app/src/main/java/io/legado/app/constant/AppPattern.kt @@ -13,6 +13,8 @@ object AppPattern { //dataURL图片类型 val dataUriRegex = Regex("data:.*?;base64,(.*)") + //Base64字符串 + val base64Regex = Regex("^[a-zA-Z0-9\\+\\/=]+$") val nameRegex = Regex("\\s+作\\s*者.*|\\s+\\S+\\s+著") val authorRegex = Regex("^\\s*作\\s*者[::\\s]+|\\s+著") diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index d2ef307c4..97a00dd3a 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -6,6 +6,7 @@ import androidx.annotation.Keep import cn.hutool.crypto.digest.DigestUtil import cn.hutool.crypto.digest.HMac import cn.hutool.core.util.HexUtil +import cn.hutool.crypto.symmetric.SymmetricCrypto import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst.dateFormat 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 + } + //******************消息摘要/散列消息鉴别码************************// /** diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index 1228907b5..9d62eec3e 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -6,7 +6,9 @@ import android.icu.text.Collator import android.icu.util.ULocale import android.net.Uri import android.text.Editable +import cn.hutool.core.lang.Validator import io.legado.app.constant.AppPattern.dataUriRegex +import io.legado.app.constant.AppPattern.base64Regex import java.io.File import java.util.* @@ -39,6 +41,11 @@ fun String?.isDataUrl() = dataUriRegex.matches(it) } ?: false +fun String?.isBase64() = + this?.let { + base64Regex.matches(it) + } ?: false + fun String?.isJson(): Boolean = this?.run { val str = this.trim() diff --git a/app/src/main/java/io/legado/app/utils/StringUtils.kt b/app/src/main/java/io/legado/app/utils/StringUtils.kt index 770c48dc0..4c5dda84c 100644 --- a/app/src/main/java/io/legado/app/utils/StringUtils.kt +++ b/app/src/main/java/io/legado/app/utils/StringUtils.kt @@ -1,8 +1,12 @@ package io.legado.app.utils +import android.util.Base64 import android.annotation.SuppressLint import android.text.TextUtils.isEmpty +import cn.hutool.core.util.HexUtil +import cn.hutool.core.lang.Validator + import java.text.DecimalFormat import java.text.SimpleDateFormat import java.util.* @@ -287,4 +291,16 @@ object StringUtils { 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() + } + } + }