diff --git a/app/src/main/assets/help/jsHelp.md b/app/src/main/assets/help/jsHelp.md index 1f91ed457..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,8 +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 ``` @@ -164,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/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/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") } } 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/EncoderUtils.kt b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt index f410075ec..208261289 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) + if (isEncrypt) symmetricCrypto.encrypt(data) else symmetricCrypto.decrypt(data) } } 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 {