diff --git a/app/src/main/java/com/novel/read/constant/AppConst.kt b/app/src/main/java/com/novel/read/constant/AppConst.kt index c7f4876..8f27394 100644 --- a/app/src/main/java/com/novel/read/constant/AppConst.kt +++ b/app/src/main/java/com/novel/read/constant/AppConst.kt @@ -8,13 +8,11 @@ import java.text.SimpleDateFormat object AppConst { const val APP_TAG = "TuZi" + const val AppId = "20210306161" + const val AppSecret = "c14f6a2b3dcah893a31b9i4f40f5cc08" const val channelIdDownload = "channel_download" const val channelIdReadAloud = "channel_read_aloud" - const val channelIdWeb = "channel_web" - - const val UA_NAME = "User-Agent" - const val CONCEAL = "http://yijianda8.com/conceal/" val timeFormat: SimpleDateFormat by lazy { SimpleDateFormat("HH:mm") diff --git a/app/src/main/java/com/novel/read/data/model/CheckSumDTO.kt b/app/src/main/java/com/novel/read/data/model/CheckSumDTO.kt new file mode 100644 index 0000000..4dbb324 --- /dev/null +++ b/app/src/main/java/com/novel/read/data/model/CheckSumDTO.kt @@ -0,0 +1,26 @@ +package com.novel.read.data.model + +/** + * @description: 参数校验DTO + * @author: zlj + * @date: 2020/9/25 9:50 + */ +class CheckSumDTO( + /** + * 对外接口接入方 appid + */ + private var appid: String, + /** + * 随机数,最大128位 + */ + private var nonce: String, + /** + * 当前UTC时间戳,从1970年1月1日0点0 分0 秒开始到现在的秒数(String),接口调用有效期为该时间戳起5分钟内 + */ + private var curtime: String, + /** + * SHA1(AppSecret+Nonce+curTime),三个参数拼接的字符串,进行SHA1哈希计算, + * 转化成16进制字符(String, 小写)。其中AppSecret为平台分配的Appid对应的AppSecret密钥 + */ + private var checksum: String +) \ No newline at end of file diff --git a/app/src/main/java/com/novel/read/network/ServiceCreator.kt b/app/src/main/java/com/novel/read/network/ServiceCreator.kt index 97a8828..3a9b62a 100644 --- a/app/src/main/java/com/novel/read/network/ServiceCreator.kt +++ b/app/src/main/java/com/novel/read/network/ServiceCreator.kt @@ -1,7 +1,11 @@ package com.novel.read.network import android.util.Log +import com.novel.read.constant.AppConst +import com.novel.read.data.model.CheckSumDTO import com.novel.read.network.api.BookService +import com.novel.read.utils.CheckSumBuilder +import com.novel.read.utils.ext.MYGSON import okhttp3.Interceptor import okhttp3.OkHttpClient import okhttp3.Request @@ -131,8 +135,17 @@ object ServiceCreator { class HeaderInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val original = chain.request() + val time = System.currentTimeMillis().toString() + val nonce = CheckSumBuilder.getRandomString(8) + val checkSum = CheckSumBuilder.getCheckSum( + AppConst.AppSecret, + nonce, + time + ) + val checkSha = MYGSON.toJson(CheckSumDTO(AppConst.AppId,nonce,time,checkSum)) val request = original.newBuilder().apply { header("model", "Android") +// header("checkSumDTO", checkSha) header("If-Modified-Since", URLEncoder.encode("${Date()}", "utf-8")) header("User-Agent", System.getProperty("http.agent") ?: "unknown") }.build() diff --git a/app/src/main/java/com/novel/read/utils/CheckSumBuilder.java b/app/src/main/java/com/novel/read/utils/CheckSumBuilder.java new file mode 100644 index 0000000..cc91ba8 --- /dev/null +++ b/app/src/main/java/com/novel/read/utils/CheckSumBuilder.java @@ -0,0 +1,94 @@ +package com.novel.read.utils; + +import java.security.MessageDigest; +import java.util.Random; + +/** + * 功能说明:验证码生成工具类 + * 修改说明: + * @author hu + * @date 2021年4月6日 下午1:33:26 + * @version 0.1 + */ +public class CheckSumBuilder { + /** + * 功能说明:计算并获取CheckSum + * 修改说明: + * @author hu + * @date 2021年4月6日 下午1:33:26 + * @param appSecret 密码 + * @param nonce 随机串 + * @param curTime 当前时间戳 + * @return 返回生成的验证码 + */ + public static String getCheckSum(String appSecret, String nonce, String curTime) { + return encode("sha1", appSecret + nonce + curTime); + } + + /** + * 功能说明:对参数进行MD5加密 + * 修改说明: + * @author hu + * @date 2021年4月6日 下午1:33:26 + * @param requestBody 要加密的内容 + * @return 返回MD5加密后的字符串 + */ + public static String getMD5(String requestBody) { + return encode("md5", requestBody); + } + + /** + * 功能说明:使用指定加密算法对字符串进行加密 + * 修改说明: + * @author hu + * @date 2021年4月6日 下午1:33:26 + * @param algorithm 加密算法 + * @param value 要加密的字符串 + * @return 返回加密后的字符串 + */ + private static String encode(String algorithm, String value) { + if (value == null) { + return null; + } + try { + MessageDigest messageDigest = MessageDigest.getInstance(algorithm); + messageDigest.update(value.getBytes()); + return getFormattedText(messageDigest.digest()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * 功能说明:把字节数组格式化为16进制字符串 + * 修改说明: + * @author hu + * @date 2021年4月6日 下午1:33:26 + * @param bytes 字节数组 + * @return 返回格式化后的字符串 + */ + private static String getFormattedText(byte[] bytes) { + int len = bytes.length; + StringBuilder buf = new StringBuilder(len * 2); + for (int j = 0; j < len; j++) { + buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]); + buf.append(HEX_DIGITS[bytes[j] & 0x0f]); + } + return buf.toString(); + } + + private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + + + public static String getRandomString(int length){ + String str="abcdefghijklmnopqrstuvwxyz0123456789"; + Random random=new Random(); + StringBuilder sb=new StringBuilder(); + for(int i=0;i?>() {}.type, + MapDeserializerDoubleAsIntFix() + ) + .registerTypeAdapter(Int::class.java, IntJsonDeserializer()) + .disableHtmlEscaping() + .create() +} + inline fun genericType(): Type = object : TypeToken() {}.type inline fun Gson.fromJsonObject(json: String?): T? {//可转成任意类型