From 8993ba4c9a7c213eb5868a0cdf7e90faadc7b75d Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Fri, 27 May 2022 11:15:41 +0800 Subject: [PATCH 1/5] refactor: remove DigestUtils --- .../java/io/legado/app/help/JsExtensions.kt | 10 +-- .../java/io/legado/app/utils/DigestUtils.kt | 62 ------------------- .../main/java/io/legado/app/utils/MD5Utils.kt | 3 +- 3 files changed, 8 insertions(+), 67 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/utils/DigestUtils.kt 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 cad84aba4..e0697afc7 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -3,6 +3,8 @@ package io.legado.app.help import android.net.Uri import android.util.Base64 import androidx.annotation.Keep +import cn.hutool.crypto.digest.DigestUtil +import cn.hutool.crypto.digest.HMac import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst.dateFormat import io.legado.app.constant.AppLog @@ -960,7 +962,7 @@ interface JsExtensions { data: String, algorithm: String, ): String { - return DigestUtils.getDigest(algorithm, data) + return DigestUtil.digester(algorithm).digestHex(data) } /** @@ -974,7 +976,7 @@ interface JsExtensions { data: String, algorithm: String, ): String { - return Base64.encodeToString(DigestUtils.getDigest(algorithm, data.toByteArray()), Base64.NO_WRAP) + return Base64.encodeToString(DigestUtil.digester(algorithm).digest(data), Base64.NO_WRAP) } /** @@ -990,7 +992,7 @@ interface JsExtensions { algorithm: String, key: String ): String { - return DigestUtils.getHMac(algorithm, key, data) + return HMac(algorithm, key.toByteArray()).digestHex(data) } /** @@ -1006,7 +1008,7 @@ interface JsExtensions { algorithm: String, key: String ): String { - return Base64.encodeToString(DigestUtils.getHMac(algorithm, key.toByteArray(), data.toByteArray()), Base64.NO_WRAP) + return Base64.encodeToString(HMac(algorithm, key.toByteArray()).digest(data), Base64.NO_WRAP) } fun md5Encode(str: String): String { diff --git a/app/src/main/java/io/legado/app/utils/DigestUtils.kt b/app/src/main/java/io/legado/app/utils/DigestUtils.kt deleted file mode 100644 index b708b770c..000000000 --- a/app/src/main/java/io/legado/app/utils/DigestUtils.kt +++ /dev/null @@ -1,62 +0,0 @@ -package io.legado.app.utils - -import java.security.MessageDigest -import javax.crypto.Mac -import javax.crypto.spec.SecretKeySpec - -object DigestUtils { - - /** - * 消息摘要 - * 支持MD5 SHA-1 SHA-224 SHA-256 SHA-384 SHA-512 - * https://developer.android.google.cn/reference/java/security/MessageDigest?hl=en - */ - fun getDigest( - algorithm: String, - data: String? - ): String { - data ?: return "" - val bytes = getDigest(algorithm, data.toByteArray()) - return StringUtils.byteToHexString(bytes) - } - - fun getDigest( - algorithm: String, - data: ByteArray - ): ByteArray { - return kotlin.runCatching { - val messageDigest = MessageDigest.getInstance(algorithm) - messageDigest.digest(data) - }.getOrThrow() - } - - /** - * 散列消息鉴别码 - * 支持DESMAC DESMAC/CFB8 DESedeMAC DESedeMAC/CFB8 DESedeMAC64 DESwithISO9797 HmacMD5 HmacSHA* ISO9797ALG3MAC PBEwithSHA* - * https://developer.android.google.cn/reference/kotlin/javax/crypto/Mac?hl=en - */ - fun getHMac( - algorithm: String, - key: String, - data: String? - ): String { - data ?: return "" - val bytes = getHMac(algorithm, key.toByteArray(), data.toByteArray()) - return StringUtils.byteToHexString(bytes) - } - - fun getHMac( - algorithm: String, - key: ByteArray, - data: ByteArray - ): ByteArray { - return kotlin.runCatching { - val mac= Mac.getInstance(algorithm) - val keySpec = SecretKeySpec(key, algorithm) - mac.init(keySpec) - mac.update(data) - mac.doFinal() - }.getOrThrow() - } - -} diff --git a/app/src/main/java/io/legado/app/utils/MD5Utils.kt b/app/src/main/java/io/legado/app/utils/MD5Utils.kt index 377f511e7..d018508fa 100644 --- a/app/src/main/java/io/legado/app/utils/MD5Utils.kt +++ b/app/src/main/java/io/legado/app/utils/MD5Utils.kt @@ -1,5 +1,6 @@ package io.legado.app.utils +import cn.hutool.crypto.digest.DigestUtil /** * 将字符串转化为MD5 */ @@ -7,7 +8,7 @@ package io.legado.app.utils object MD5Utils { fun md5Encode(str: String?): String { - return DigestUtils.getDigest("MD5", str) + return DigestUtil.digester("MD5").digestHex(str) } fun md5Encode16(str: String): String { From 8468d2e3574bed9224b2c0125343c885bf462c8b Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Fri, 27 May 2022 11:24:29 +0800 Subject: [PATCH 2/5] Revert "refactor: remove cn.hutool:hutool-crypto" This reverts commit 620bd6a7492b2e636955894b3a43b221b27ab42f. --- .../java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt index 3836cdb3d..36dd38f86 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt @@ -2,12 +2,11 @@ package io.legado.app.ui.book.remote import android.content.Context import android.view.ViewGroup +import cn.hutool.core.date.LocalDateTimeUtil import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.databinding.ItemRemoteBookBinding import io.legado.app.utils.ConvertUtils -import java.text.SimpleDateFormat -import java.util.Date /** @@ -39,7 +38,7 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) : tvName.text = item.filename.substringBeforeLast(".") tvContentType.text = item.contentType tvSize.text = ConvertUtils.formatFileSize(item.size) - tvDate.text = SimpleDateFormat("yyyy-MM-dd").format(Date(item.lastModify)) + tvDate.text = LocalDateTimeUtil.format(LocalDateTimeUtil.of(item.lastModify), "yyyy-MM-dd") } } From d6a7cc743a60077beab4115959a9b3691ec7454b Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Fri, 27 May 2022 11:48:10 +0800 Subject: [PATCH 3/5] docs: add hutool doc link --- app/src/main/assets/help/jsHelp.md | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/assets/help/jsHelp.md b/app/src/main/assets/help/jsHelp.md index 1f91ed457..819e20204 100644 --- a/app/src/main/assets/help/jsHelp.md +++ b/app/src/main/assets/help/jsHelp.md @@ -134,6 +134,7 @@ deleteFile(path: String) > [常见加密解密算法介绍](https://www.yijiyong.com/algorithm/encryption/01-intro.html) > [相关概念](https://blog.csdn.net/OrangeJack/article/details/82913804) > [Android支持的transformation](https://developer.android.google.cn/reference/kotlin/javax/crypto/Cipher?hl=en) +> 其他加密方式 可在js中[调用](https://m.jb51.net/article/92138.htm)[hutool-crypto](https://www.hutool.cn/docs/#/) * AES > transformation默认实现AES/ECB/PKCS5Padding ``` From e35b592691c3925c58d00fa107963e60c1afa8ae Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Fri, 27 May 2022 12:45:29 +0800 Subject: [PATCH 4/5] feat(EncoderUtils): support zeropadding --- .../java/io/legado/app/utils/EncoderUtils.kt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/EncoderUtils.kt b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt index f410075ec..9b3d192de 100644 --- a/app/src/main/java/io/legado/app/utils/EncoderUtils.kt +++ b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt @@ -2,9 +2,9 @@ package io.legado.app.utils import android.util.Base64 import java.security.spec.AlgorithmParameterSpec -import javax.crypto.Cipher import javax.crypto.spec.IvParameterSpec import javax.crypto.spec.SecretKeySpec +import cn.hutool.crypto.symmetric.SymmetricCrypto /** * transformations https://developer.android.google.cn/reference/kotlin/javax/crypto/Cipher?hl=en @@ -328,16 +328,14 @@ object EncoderUtils { ): ByteArray? { return if (data == null || data.isEmpty() || key == null || key.isEmpty()) null else { + //hutool support zeropadding val keySpec = SecretKeySpec(key, algorithm) - val cipher = Cipher.getInstance(transformation) - val mode = if (isEncrypt) Cipher.ENCRYPT_MODE else Cipher.DECRYPT_MODE - if (iv == null || iv.isEmpty()) { - cipher.init(mode, keySpec) - } else { - val params: AlgorithmParameterSpec = IvParameterSpec(iv) - cipher.init(mode, keySpec, params) + var params: AlgorithmParameterSpec? = null + if (iv != null && !iv.isEmpty()) { + params = IvParameterSpec(iv) } - cipher.doFinal(data) + val symmetricCrypto = SymmetricCrypto(transformation, keySpec, params) + return if (isEncrypt) symmetricCrypto.encrypt(data) else symmetricCrypto.decrypt(data) } } From 0a08522ae64ecb2d718aa515db0fc71d6607bd51 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Fri, 27 May 2022 13:30:03 +0800 Subject: [PATCH 5/5] docs: update jshelp.md --- app/src/main/assets/help/jsHelp.md | 12 +++++++++--- .../main/java/io/legado/app/utils/EncoderUtils.kt | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/assets/help/jsHelp.md b/app/src/main/assets/help/jsHelp.md index 819e20204..64f1b915f 100644 --- a/app/src/main/assets/help/jsHelp.md +++ b/app/src/main/assets/help/jsHelp.md @@ -57,7 +57,9 @@ java.ajaxAll(urlList: Array): Array java.connect(urlStr): StrResponse java.post(url: String, body: String, headerMap: Map): Connection.Response + java.get(url: String, headerMap: Map): Connection.Response + java.head(url: String, headerMap: Map): Connection.Response * 使用webView访问网络 @@ -132,9 +134,13 @@ deleteFile(path: String) ``` **** > [常见加密解密算法介绍](https://www.yijiyong.com/algorithm/encryption/01-intro.html) + > [相关概念](https://blog.csdn.net/OrangeJack/article/details/82913804) + > [Android支持的transformation](https://developer.android.google.cn/reference/kotlin/javax/crypto/Cipher?hl=en) + > 其他加密方式 可在js中[调用](https://m.jb51.net/article/92138.htm)[hutool-crypto](https://www.hutool.cn/docs/#/) + * AES > transformation默认实现AES/ECB/PKCS5Padding ``` @@ -165,14 +171,14 @@ java.tripleDESEncodeBase64Str(data: String,key: String,mode: String,padding: Str java.tripleDESDecodeStr(data: String,key: String,mode: String,padding: String,iv: String): String? ``` * 摘要 -> algorithm支持MD5 SHA-1 SHA-224 SHA-256 SHA-384 SHA-512 +> MD5 SHA-1 SHA-224 SHA-256 SHA-384 SHA-512 ``` java.digestHex(data: String, algorithm: String,): String? java.digestBase64Str(data: String, algorithm: String,): String? ``` -* HMac(散列消息鉴别码) -> algorithm支持DESMAC DESMAC/CFB8 DESedeMAC DESedeMAC/CFB8 DESedeMAC64 DESwithISO9797 HmacMD5 HmacSHA* ISO9797ALG3MAC PBEwithSHA* +* HMac(部分算法暂不支持) +> DESMAC DESMAC/CFB8 DESedeMAC DESedeMAC/CFB8 DESedeMAC64 DESwithISO9797 HmacMD5 HmacSHA* ISO9797ALG3MAC PBEwithSHA* ``` java.HMacHex(data: String, algorithm: String, key: String): String diff --git a/app/src/main/java/io/legado/app/utils/EncoderUtils.kt b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt index 9b3d192de..208261289 100644 --- a/app/src/main/java/io/legado/app/utils/EncoderUtils.kt +++ b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt @@ -335,7 +335,7 @@ object EncoderUtils { params = IvParameterSpec(iv) } val symmetricCrypto = SymmetricCrypto(transformation, keySpec, params) - return if (isEncrypt) symmetricCrypto.encrypt(data) else symmetricCrypto.decrypt(data) + if (isEncrypt) symmetricCrypto.encrypt(data) else symmetricCrypto.decrypt(data) } }