增加防篡改控制

This commit is contained in:
2026-09-11 18:16:38 +08:00
parent 26cca6a454
commit f2f68e1e6d
21 changed files with 180 additions and 102 deletions

View File

@@ -44,6 +44,7 @@ import androidx.annotation.StringDef
MMKVKey.UserGpsLat,
MMKVKey.UserGpsLon,
MMKVKey.UserGpsSpeed,
MMKVKey.isCheckLegalApp,
MMKVKey.isFirstCheckBleOpen,
MMKVKey.UUID,
MMKVKey.Shared
@@ -66,6 +67,7 @@ annotation class MMKVKey {
const val Gender = "gender"
const val BirthdayDate = "birthdayDate"
const val CountryCode = "countryCode"
const val isCheckLegalApp = "isCheckLegalApp"
//首次绑定流程
const val FirstDeviceId = "firstDeviceId"

View File

@@ -75,8 +75,8 @@ class CommonRequestInterceptor : Interceptor {
}
}
val uuid = MMKVUtil.getString(MMKVKey.UUID)
paramList.add("uuid$uuid")
val uniqueId = MMKVUtil.getString(MMKVKey.UUID)
paramList.add("uuid$uniqueId")
val sign = if (paramList.isNotEmpty()) {
//按key的字母排序
paramList.sort()
@@ -87,7 +87,7 @@ class CommonRequestInterceptor : Interceptor {
val token = MMKVUtil.getString(MMKVKey.Token)
builder.addHeader("token", token)
builder.addHeader("uuid", uuid)
builder.addHeader("uuid", uniqueId)
builder.addHeader("sign", sign)
return chain.proceed(builder.build())

View File

@@ -6,9 +6,11 @@ import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import com.abbidot.baselibrary.constant.MMKVKey
import java.security.MessageDigest
import java.util.Locale
import java.util.UUID
@@ -115,19 +117,87 @@ class AppUtils {
*/
@SuppressLint("HardwareIds")
fun saveUniqueId(context: Context) {
var uuid = MMKVUtil.getString(MMKVKey.UUID)
if (TextUtils.isEmpty(uuid)) {
var uniqueId = MMKVUtil.getString(MMKVKey.UUID)
if (TextUtils.isEmpty(uniqueId)) {
val androidId =
Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
?: ""
uuid = if (TextUtils.isEmpty(androidId))
UUID.randomUUID().toString()
else
androidId
MMKVUtil.putString(MMKVKey.UUID, uuid)
uniqueId = if (TextUtils.isEmpty(androidId)) UUID.randomUUID().toString()
else androidId
MMKVUtil.putString(MMKVKey.UUID, uniqueId)
}
}
/**
*获取apk签名Sha256
*/
fun getApkSignSha2561(context: Context): String {
return try {
val pm = context.packageManager
val pkgName = context.packageName
val pkgInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// API33+ Android13,直接用GET_SIGNING_CERTIFICATES,不需要if判断
val flagLong = PackageManager.GET_SIGNING_CERTIFICATES.toLong()
pm.getPackageInfo(pkgName, PackageManager.PackageInfoFlags.of(flagLong))
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// API28 ~ API32
pm.getPackageInfo(pkgName, PackageManager.GET_SIGNING_CERTIFICATES)
} else {
// API24 ~ API27
pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES)
}
val cert = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && pkgInfo.signingInfo != null -> pkgInfo.signingInfo.signingCertificateHistory?.firstOrNull()
else -> pkgInfo.signatures?.firstOrNull()
} ?: return ""
val digest = MessageDigest.getInstance("SHA-256")
val hashBytes = digest.digest(cert.toByteArray())
hashBytes.joinToString("") { "%02x".format(Locale.US, it) }
} catch (_: Exception) {
""
}
}
/**
* 获取apk签名Sha256
*/
fun getApkSignSha256(context: Context): String {
return try {
val pm = context.packageManager
val pkgName = context.packageName
val pkgInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// API33+ Android13
val flagLong = PackageManager.GET_SIGNING_CERTIFICATES.toLong()
pm.getPackageInfo(pkgName, PackageManager.PackageInfoFlags.of(flagLong))
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// API28 ~ API32
pm.getPackageInfo(pkgName, PackageManager.GET_SIGNING_CERTIFICATES)
} else {
// API24 ~ API27
pm.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES)
}
val certBytes = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && pkgInfo.signingInfo != null -> {
// 读取【当前生效签名】apkContentsSigners,不是历史证书
val activeCert =
pkgInfo.signingInfo.apkContentsSigners?.firstOrNull() ?: return ""
activeCert.toByteArray()
}
else -> {
val signature = pkgInfo.signatures?.firstOrNull() ?: return ""
signature.toByteArray()
}
}
val digest = MessageDigest.getInstance("SHA-256")
val hashBytes = digest.digest(certBytes)
hashBytes.joinToString(separator = "") { "%02x".format(Locale.US, it) }
} catch (_: Exception) {
""
}
}
/**
* 获取系统国家地区
*/