From 0b06e7395fd4da4d1e229c8860dd04719f457886 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sun, 16 Oct 2022 19:40:15 +0800 Subject: [PATCH] docs: update docs --- app/src/main/assets/help/jsHelp.md | 7 ++- .../java/io/legado/app/constant/AppPattern.kt | 2 - .../java/io/legado/app/help/JsExtensions.kt | 44 +++++++++---------- .../io/legado/app/utils/StringExtensions.kt | 6 --- .../java/io/legado/app/utils/StringUtils.kt | 16 ------- 5 files changed, 28 insertions(+), 47 deletions(-) diff --git a/app/src/main/assets/help/jsHelp.md b/app/src/main/assets/help/jsHelp.md index d9f70de63..7bf08ec75 100644 --- a/app/src/main/assets/help/jsHelp.md +++ b/app/src/main/assets/help/jsHelp.md @@ -162,14 +162,19 @@ deleteFile(path: String) > DES transformation默认实现DES/ECB/PKCS5Padding > TripleDES tansformation默认实现DESede/ECB/PKCS5Padding > 内部实现为cn.hutool.crypto 解密加密接口支持ByteArray|Base64String|HexString|InputStream -> 输入参数key iv 支持ByteArray|Base64String|HexString|Utf8String +> 输入参数key iv 支持ByteArray|Utf8String +> 如果key iv 为Hex Base64,且需要解码为ByteArray,调用java.decodeBase64Hex ``` //解密为ByteArray 字符串 java.createSymmetricCrypto(transformation, key, iv).decrypt(data) + java.createSymmetricCrypto(transformation, key, iv).decryptStr(data) + //加密为ByteArray Base64字符 HEX字符 java.createSymmetricCrypto(transformation, key, iv).encrypt(data) + java.createSymmetricCrypto(transformation, key, iv).encryptBase64(data) + java.createSymmetricCrypto(transformation, key, iv).encryptHex(data) ``` * 摘要 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 cfecd8bcc..731a7296a 100644 --- a/app/src/main/java/io/legado/app/constant/AppPattern.kt +++ b/app/src/main/java/io/legado/app/constant/AppPattern.kt @@ -13,8 +13,6 @@ 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 91686a122..bcda4039d 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -7,6 +7,7 @@ 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 cn.hutool.crypto.SecureUtil import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst.dateFormat import io.legado.app.constant.AppLog @@ -658,51 +659,50 @@ interface JsExtensions { * java.createSymmetricCrypto(transformation, key, iv).encrypt(data) * java.createSymmetricCrypto(transformation, key, iv).encryptBase64(data) * java.createSymmetricCrypto(transformation, key, iv).encryptHex(data) - */ - /* 自动转换key iv 到ByteArray 支持base64 hex uft8*/ - fun createSymmetricCrypto( - transformation: String, - key: String - ): SymmetricCrypto { - return createSymmetricCrypto(transformation, key, null) + * 如果key iv 为Hex Base64,且需要解码为ByteArray,调用java.decodeBase64Hex + + */ + /* iv key 为Base64String HexString 到ByteArray的转换函数 */ + fun decodeBase64Hex(str: String): ByteArray? { + return SecureUtil.decode(str) } + /* 调用SymmetricCrypto key为null时使用随机密钥*/ fun createSymmetricCrypto( transformation: String, - key: String, - iv: String? + key: ByteArray?, + iv: ByteArray? ): SymmetricCrypto { - return createSymmetricCrypto( - transformation, - StringUtils.encodeStringToByteArray(key), - StringUtils.encodeStringToByteArray(iv) - ) + val symmetricCrypto = SymmetricCrypto(transformation, key) + return if (iv != null && !iv.isEmpty()) symmetricCrypto.setIv(iv) else symmetricCrypto } - /* 调用SymmetricCrypto key为null时使用随机密钥*/ fun createSymmetricCrypto( - transformation: String + transformation: String, + key: ByteArray ): SymmetricCrypto { - return createSymmetricCrypto(transformation, null, null) + return createSymmetricCrypto(transformation, key, null) } fun createSymmetricCrypto( transformation: String, - key: ByteArray + key: String ): SymmetricCrypto { return createSymmetricCrypto(transformation, key, null) } fun createSymmetricCrypto( transformation: String, - key: ByteArray?, - iv: ByteArray? + key: String, + iv: String? ): SymmetricCrypto { - val symmetricCrypto = SymmetricCrypto(transformation, key) - return if (iv != null && !iv.isEmpty()) symmetricCrypto.setIv(iv) else symmetricCrypto + return createSymmetricCrypto( + transformation, key.encodeToByteArray(), iv?.encodeToByteArray() + ) } + //******************消息摘要/散列消息鉴别码************************// /** 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 d4ce56ab2..f273cc45a 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -8,7 +8,6 @@ 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,11 +38,6 @@ 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 4c5dda84c..770c48dc0 100644 --- a/app/src/main/java/io/legado/app/utils/StringUtils.kt +++ b/app/src/main/java/io/legado/app/utils/StringUtils.kt @@ -1,12 +1,8 @@ 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.* @@ -291,16 +287,4 @@ 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() - } - } - }