From 123b9aacf2ac690cd6e6c16c0d4b2ec153e3e568 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Tue, 24 May 2022 07:07:37 +0800 Subject: [PATCH] feat(JsExtensions): add Hmac and update docs --- app/src/main/assets/help/jsHelp.md | 32 +++++++++-------- app/src/main/assets/updateLog.md | 4 ++- .../java/io/legado/app/help/JsExtensions.kt | 34 ++++++++++++++++++- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/app/src/main/assets/help/jsHelp.md b/app/src/main/assets/help/jsHelp.md index 9e5ba8bc4..d7b312197 100644 --- a/app/src/main/assets/help/jsHelp.md +++ b/app/src/main/assets/help/jsHelp.md @@ -54,7 +54,11 @@ java.put(key, value) java.ajax(urlStr): String java.ajaxAll(urlList: Array): Array //返回Response 方法body() code() message() header() raw() toString() -java.connect(urlStr): Response +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访问网络 * @param html 直接用webView载入的html, 如果html为空直接访问url @@ -130,11 +134,8 @@ deleteFile(path: String) > [常见加密解密算法介绍](https://www.yijiyong.com/algorithm/encryption/01-intro.html) > [相关概念](https://blog.csdn.net/OrangeJack/article/details/82913804) * AES +> transformation默认实现AES/ECB/PKCS5Padding ``` -* @param data 传入的原始数据 -* @param key AES加密的key -* @param transformation AES加密的方式 例如AES/ECB/PKCS5Padding -* @param iv ECB模式的偏移向量 java.aesDecodeToString(str: String, key: String, transformation: String, iv: String) java.aesBase64DecodeToString(str: String, key: String, transformation: String, iv: String) @@ -144,6 +145,7 @@ java.aesEncodeToString(str: String, key: String, transformation: String, iv: Str java.aesEncodeToBase64String(str: String, key: String, transformation: String, iv: String) ``` * DES +> transformation默认实现DES/ECB/PKCS5Padding ``` java.desDecodeToString(str: String, key: String, transformation: String, iv: String) @@ -154,23 +156,25 @@ java.desEncodeToString(str: String, key: String, transformation: String, iv: Str java.desEncodeToBase64String(str: String, key: String, transformation: String, iv: String) ``` * 3DES +> tansformation默认实现DESede/ECB/PKCS5Padding ``` -* @param data 被加密的字符串 -* @param key 密钥 -* @param mode 模式 ECB/CBC/CFB/OFB/CTR -* @param padding 补码方式 NoPadding/PKCS5Padding/ -* @param iv 加盐 java.tripleDESEncodeBase64Str(data: String,key: String,mode: String,padding: String,iv: String): String? java.tripleDESDecodeStr(data: String,key: String,mode: String,padding: String,iv: String): String? ``` * 摘要 +> algorithm支持MD2 MD5 SHA-1 SHA-256 SHA-384 SHA-512 +``` +java.digestHex(data: String, algorithm: String,): String? + +java.digestBase64Str(data: String, algorithm: String,): String? +``` +* HMac(散列消息鉴别码) +> algorithm支持HmacMD5 HmacSHA1 HmacSHA224 HmacSHA256 HmacSHA384 HmacSHA512 ``` -* @param data 被摘要数据 -* @param algorithm 签名算法 MD5/SHA1/SHA256/SHA512 -java.digestHex(data: String,algorithm: String,): String? +java.HMacHex(data: String, algorithm: String, key: String): String -java.digestBase64Str(data: String,algorithm: String,): String? +java.HMacBase64(data: String, algorithm: String, key: String): String ``` * md5 ``` diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index d689e0f54..f7731b3a4 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -15,6 +15,8 @@ * 修复部分txt章节结尾乱码bug * 优化翻页流畅度 +* js添加des HMac +* 登录ui用户信息默认加密方式改变,需要重新登录 **2022/05/21** @@ -322,4 +324,4 @@ eval(String(java.cacheFile(url))) **2022/01/01** * 修复本地txt问题,不在拷贝到私有目录,可以正常打开 -* 优化txt目录识别,取目录数量最多的规则 \ No newline at end of file +* 优化txt目录识别,取目录数量最多的规则 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 50ec5f276..cad84aba4 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -947,7 +947,7 @@ interface JsExtensions { )?.let { String(it) } } -//******************消息摘要************************// +//******************消息摘要/散列消息鉴别码************************// /** * 生成摘要,并转为16进制字符串 @@ -977,6 +977,38 @@ interface JsExtensions { return Base64.encodeToString(DigestUtils.getDigest(algorithm, data.toByteArray()), Base64.NO_WRAP) } + /** + * 生成散列消息鉴别码,并转为16进制字符串 + * + * @param data 被摘要数据 + * @param algorithm 签名算法 + * @param key 密钥 + * @return 16进制字符串 + */ + fun HMacHex( + data: String, + algorithm: String, + key: String + ): String { + return DigestUtils.getHMac(algorithm, key, data) + } + + /** + * 生成散列消息鉴别码,并转为Base64字符串 + * + * @param data 被摘要数据 + * @param algorithm 签名算法 + * @param key 密钥 + * @return Base64字符串 + */ + fun HMacBase64( + data: String, + algorithm: String, + key: String + ): String { + return Base64.encodeToString(DigestUtils.getHMac(algorithm, key.toByteArray(), data.toByteArray()), Base64.NO_WRAP) + } + fun md5Encode(str: String): String { return MD5Utils.md5Encode(str) }