From a57eb82236f0a7532b32d7f6e101eb9f4a671e0e Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 17 Oct 2022 09:12:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BBjs=E5=8A=A0=E8=A7=A3=E5=AF=86?= =?UTF-8?q?=E6=96=87=E4=BB=B6,=E4=BF=9D=E7=95=99=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95,=E6=A0=87=E8=AE=B0=E4=B8=BA?= =?UTF-8?q?=E5=BC=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/JsEncodeUtils.kt | 474 ++++++++++++++++++ .../java/io/legado/app/help/JsExtensions.kt | 128 +---- 2 files changed, 475 insertions(+), 127 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/JsEncodeUtils.kt diff --git a/app/src/main/java/io/legado/app/help/JsEncodeUtils.kt b/app/src/main/java/io/legado/app/help/JsEncodeUtils.kt new file mode 100644 index 000000000..33860abc9 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/JsEncodeUtils.kt @@ -0,0 +1,474 @@ +package io.legado.app.help + +import android.util.Base64 +import cn.hutool.crypto.digest.DigestUtil +import cn.hutool.crypto.digest.HMac +import cn.hutool.crypto.symmetric.SymmetricCrypto +import io.legado.app.utils.MD5Utils + +interface JsEncodeUtils { + + fun md5Encode(str: String): String { + return MD5Utils.md5Encode(str) + } + + fun md5Encode16(str: String): String { + return MD5Utils.md5Encode16(str) + } + + + //******************对称加密解密************************// + + /** + * 在js中这样使用 + * java.createSymmetricCrypto(transformation, key, iv).decrypt(data) + * java.createSymmetricCrypto(transformation, key, iv).decryptStr(data) + + * java.createSymmetricCrypto(transformation, key, iv).encrypt(data) + * java.createSymmetricCrypto(transformation, key, iv).encryptBase64(data) + * java.createSymmetricCrypto(transformation, key, iv).encryptHex(data) + */ + + /* 调用SymmetricCrypto key为null时使用随机密钥*/ + fun createSymmetricCrypto( + transformation: String, + key: ByteArray?, + iv: ByteArray? + ): SymmetricCrypto { + val symmetricCrypto = SymmetricCrypto(transformation, key) + return if (iv != null && iv.isNotEmpty()) symmetricCrypto.setIv(iv) else symmetricCrypto + } + + fun createSymmetricCrypto( + transformation: String, + key: ByteArray + ): SymmetricCrypto { + return createSymmetricCrypto(transformation, key, null) + } + + fun createSymmetricCrypto( + transformation: String, + key: String + ): SymmetricCrypto { + return createSymmetricCrypto(transformation, key, null) + } + + fun createSymmetricCrypto( + transformation: String, + key: String, + iv: String? + ): SymmetricCrypto { + return createSymmetricCrypto( + transformation, key.encodeToByteArray(), iv?.encodeToByteArray() + ) + } + + //******************对称加密解密old************************// + + /////AES + /** + * AES 解码为 ByteArray + * @param str 传入的AES加密的数据 + * @param key AES 解密的key + * @param transformation AES加密的方式 + * @param iv ECB模式的偏移向量 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decrypt(str)") + ) + fun aesDecodeToByteArray( + str: String, key: String, transformation: String, iv: String + ): ByteArray? { + return createSymmetricCrypto(transformation, key, iv).decrypt(str) + } + + /** + * AES 解码为 String + * @param str 传入的AES加密的数据 + * @param key AES 解密的key + * @param transformation AES加密的方式 + * @param iv ECB模式的偏移向量 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(str)") + ) + fun aesDecodeToString( + str: String, key: String, transformation: String, iv: String + ): String? { + return createSymmetricCrypto(transformation, key, iv).decryptStr(str) + } + + /** + * AES解码为String,算法参数经过Base64加密 + * + * @param data 加密的字符串 + * @param key Base64后的密钥 + * @param mode 模式 + * @param padding 补码方式 + * @param iv Base64后的加盐 + * @return 解密后的字符串 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)") + ) + fun aesDecodeArgsBase64Str( + data: String, + key: String, + mode: String, + padding: String, + iv: String + ): String? { + return createSymmetricCrypto( + "AES/${mode}/${padding}", + Base64.decode(key, Base64.NO_WRAP), + Base64.decode(iv, Base64.NO_WRAP) + ).decryptStr(data) + } + + /** + * 已经base64的AES 解码为 ByteArray + * @param str 传入的AES Base64加密的数据 + * @param key AES 解密的key + * @param transformation AES加密的方式 + * @param iv ECB模式的偏移向量 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decrypt(str)") + ) + fun aesBase64DecodeToByteArray( + str: String, key: String, transformation: String, iv: String + ): ByteArray? { + return createSymmetricCrypto(transformation, key, iv).decrypt(str) + } + + /** + * 已经base64的AES 解码为 String + * @param str 传入的AES Base64加密的数据 + * @param key AES 解密的key + * @param transformation AES加密的方式 + * @param iv ECB模式的偏移向量 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(str)") + ) + fun aesBase64DecodeToString( + str: String, key: String, transformation: String, iv: String + ): String? { + return createSymmetricCrypto(transformation, key, iv).decryptStr(str) + } + + /** + * 加密aes为ByteArray + * @param data 传入的原始数据 + * @param key AES加密的key + * @param transformation AES加密的方式 + * @param iv ECB模式的偏移向量 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decrypt(data)") + ) + fun aesEncodeToByteArray( + data: String, key: String, transformation: String, iv: String + ): ByteArray? { + return createSymmetricCrypto(transformation, key, iv).decrypt(data) + } + + /** + * 加密aes为String + * @param data 传入的原始数据 + * @param key AES加密的key + * @param transformation AES加密的方式 + * @param iv ECB模式的偏移向量 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)") + ) + fun aesEncodeToString( + data: String, key: String, transformation: String, iv: String + ): String? { + return createSymmetricCrypto(transformation, key, iv).decryptStr(data) + } + + /** + * 加密aes后Base64化的ByteArray + * @param data 传入的原始数据 + * @param key AES加密的key + * @param transformation AES加密的方式 + * @param iv ECB模式的偏移向量 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data).toByteArray()") + ) + fun aesEncodeToBase64ByteArray( + data: String, key: String, transformation: String, iv: String + ): ByteArray? { + return createSymmetricCrypto(transformation, key, iv).encryptBase64(data).toByteArray() + } + + /** + * 加密aes后Base64化的String + * @param data 传入的原始数据 + * @param key AES加密的key + * @param transformation AES加密的方式 + * @param iv ECB模式的偏移向量 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data)") + ) + fun aesEncodeToBase64String( + data: String, key: String, transformation: String, iv: String + ): String? { + return createSymmetricCrypto(transformation, key, iv).encryptBase64(data) + } + + + /** + * AES加密并转为Base64,算法参数经过Base64加密 + * + * @param data 被加密的字符串 + * @param key Base64后的密钥 + * @param mode 模式 + * @param padding 补码方式 + * @param iv Base64后的加盐 + * @return 加密后的Base64 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data)") + ) + fun aesEncodeArgsBase64Str( + data: String, + key: String, + mode: String, + padding: String, + iv: String + ): String? { + return createSymmetricCrypto("AES/${mode}/${padding}", key, iv).encryptBase64(data) + } + + /////DES + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)") + ) + fun desDecodeToString( + data: String, key: String, transformation: String, iv: String + ): String? { + return createSymmetricCrypto(transformation, key, iv).decryptStr(data) + } + + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)") + ) + fun desBase64DecodeToString( + data: String, key: String, transformation: String, iv: String + ): String? { + return createSymmetricCrypto(transformation, key, iv).decryptStr(data) + } + + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).encrypt(data)") + ) + fun desEncodeToString( + data: String, key: String, transformation: String, iv: String + ): String? { + return String(createSymmetricCrypto(transformation, key, iv).encrypt(data)) + } + + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data)") + ) + fun desEncodeToBase64String( + data: String, key: String, transformation: String, iv: String + ): String? { + return createSymmetricCrypto(transformation, key, iv).encryptBase64(data) + } + + //////3DES + /** + * 3DES解密 + * + * @param data 加密的字符串 + * @param key 密钥 + * @param mode 模式 + * @param padding 补码方式 + * @param iv 加盐 + * @return 解密后的字符串 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)") + ) + fun tripleDESDecodeStr( + data: String, + key: String, + mode: String, + padding: String, + iv: String + ): String? { + return createSymmetricCrypto("DESede/${mode}/${padding}", key, iv).decryptStr(data) + } + + /** + * 3DES解密,算法参数经过Base64加密 + * + * @param data 加密的字符串 + * @param key Base64后的密钥 + * @param mode 模式 + * @param padding 补码方式 + * @param iv Base64后的加盐 + * @return 解密后的字符串 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)") + ) + fun tripleDESDecodeArgsBase64Str( + data: String, + key: String, + mode: String, + padding: String, + iv: String + ): String? { + return createSymmetricCrypto( + "DESede/${mode}/${padding}", + Base64.decode(key, Base64.NO_WRAP), + iv.encodeToByteArray() + ).decryptStr(data) + } + + + /** + * 3DES加密并转为Base64 + * + * @param data 被加密的字符串 + * @param key 密钥 + * @param mode 模式 + * @param padding 补码方式 + * @param iv 加盐 + * @return 加密后的Base64 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data)") + ) + fun tripleDESEncodeBase64Str( + data: String, + key: String, + mode: String, + padding: String, + iv: String + ): String? { + return createSymmetricCrypto("DESede/${mode}/${padding}", key, iv) + .encryptBase64(data) + } + + /** + * 3DES加密并转为Base64,算法参数经过Base64加密 + * + * @param data 被加密的字符串 + * @param key Base64后的密钥 + * @param mode 模式 + * @param padding 补码方式 + * @param iv Base64后的加盐 + * @return 加密后的Base64 + */ + @Deprecated( + "过于繁琐弃用", + ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data)") + ) + fun tripleDESEncodeArgsBase64Str( + data: String, + key: String, + mode: String, + padding: String, + iv: String + ): String? { + return createSymmetricCrypto( + "DESede/${mode}/${padding}", + Base64.decode(key, Base64.NO_WRAP), + iv.encodeToByteArray() + ).encryptBase64(data) + } + +//******************消息摘要/散列消息鉴别码************************// + + /** + * 生成摘要,并转为16进制字符串 + * + * @param data 被摘要数据 + * @param algorithm 签名算法 + * @return 16进制字符串 + */ + fun digestHex( + data: String, + algorithm: String, + ): String { + return DigestUtil.digester(algorithm).digestHex(data) + } + + /** + * 生成摘要,并转为Base64字符串 + * + * @param data 被摘要数据 + * @param algorithm 签名算法 + * @return Base64字符串 + */ + fun digestBase64Str( + data: String, + algorithm: String, + ): String { + return Base64.encodeToString(DigestUtil.digester(algorithm).digest(data), Base64.NO_WRAP) + } + + /** + * 生成散列消息鉴别码,并转为16进制字符串 + * + * @param data 被摘要数据 + * @param algorithm 签名算法 + * @param key 密钥 + * @return 16进制字符串 + */ + @Suppress("FunctionName") + fun HMacHex( + data: String, + algorithm: String, + key: String + ): String { + return HMac(algorithm, key.toByteArray()).digestHex(data) + } + + /** + * 生成散列消息鉴别码,并转为Base64字符串 + * + * @param data 被摘要数据 + * @param algorithm 签名算法 + * @param key 密钥 + * @return Base64字符串 + */ + @Suppress("FunctionName") + fun HMacBase64( + data: String, + algorithm: String, + key: String + ): String { + return Base64.encodeToString( + HMac(algorithm, key.toByteArray()).digest(data), + Base64.NO_WRAP + ) + } + + +} \ No newline at end of file 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 d7924c236..4f9d91343 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -4,10 +4,6 @@ import android.net.Uri import android.util.Base64 import androidx.annotation.Keep import cn.hutool.core.util.HexUtil -import cn.hutool.crypto.SecureUtil -import cn.hutool.crypto.digest.DigestUtil -import cn.hutool.crypto.digest.HMac -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 @@ -47,7 +43,7 @@ import java.util.zip.ZipInputStream */ @Keep @Suppress("unused") -interface JsExtensions { +interface JsExtensions : JsEncodeUtils { fun getSource(): BaseSource? @@ -667,126 +663,4 @@ interface JsExtensions { return AppConst.androidId } -//******************对称加密解密************************// - - /** - * 在js中这样使用 - * java.createSymmetricCrypto(transformation, key, iv).decrypt(data) - * java.createSymmetricCrypto(transformation, key, iv).decryptStr(data) - - * java.createSymmetricCrypto(transformation, key, iv).encrypt(data) - * java.createSymmetricCrypto(transformation, key, iv).encryptBase64(data) - * java.createSymmetricCrypto(transformation, key, iv).encryptHex(data) - */ - - /* 调用SymmetricCrypto key为null时使用随机密钥*/ - fun createSymmetricCrypto( - transformation: String, - key: ByteArray?, - iv: ByteArray? - ): SymmetricCrypto { - val symmetricCrypto = SymmetricCrypto(transformation, key) - return if (iv != null && iv.isNotEmpty()) symmetricCrypto.setIv(iv) else symmetricCrypto - } - - fun createSymmetricCrypto( - transformation: String, - key: ByteArray - ): SymmetricCrypto { - return createSymmetricCrypto(transformation, key, null) - } - - fun createSymmetricCrypto( - transformation: String, - key: String - ): SymmetricCrypto { - return createSymmetricCrypto(transformation, key, null) - } - - fun createSymmetricCrypto( - transformation: String, - key: String, - iv: String? - ): SymmetricCrypto { - return createSymmetricCrypto( - transformation, key.encodeToByteArray(), iv?.encodeToByteArray() - ) - } - - -//******************消息摘要/散列消息鉴别码************************// - - /** - * 生成摘要,并转为16进制字符串 - * - * @param data 被摘要数据 - * @param algorithm 签名算法 - * @return 16进制字符串 - */ - fun digestHex( - data: String, - algorithm: String, - ): String { - return DigestUtil.digester(algorithm).digestHex(data) - } - - /** - * 生成摘要,并转为Base64字符串 - * - * @param data 被摘要数据 - * @param algorithm 签名算法 - * @return Base64字符串 - */ - fun digestBase64Str( - data: String, - algorithm: String, - ): String { - return Base64.encodeToString(DigestUtil.digester(algorithm).digest(data), Base64.NO_WRAP) - } - - /** - * 生成散列消息鉴别码,并转为16进制字符串 - * - * @param data 被摘要数据 - * @param algorithm 签名算法 - * @param key 密钥 - * @return 16进制字符串 - */ - @Suppress("FunctionName") - fun HMacHex( - data: String, - algorithm: String, - key: String - ): String { - return HMac(algorithm, key.toByteArray()).digestHex(data) - } - - /** - * 生成散列消息鉴别码,并转为Base64字符串 - * - * @param data 被摘要数据 - * @param algorithm 签名算法 - * @param key 密钥 - * @return Base64字符串 - */ - @Suppress("FunctionName") - fun HMacBase64( - data: String, - algorithm: String, - key: String - ): String { - return Base64.encodeToString( - HMac(algorithm, key.toByteArray()).digest(data), - Base64.NO_WRAP - ) - } - - fun md5Encode(str: String): String { - return MD5Utils.md5Encode(str) - } - - fun md5Encode16(str: String): String { - return MD5Utils.md5Encode16(str) - } - }