gpt4 book ai didi

android - 如何在 API 28 中使用 PackageInfo.GET_SIGNING_CERTIFICATES?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:57:41 28 4
gpt4 key购买 nike

PackageManager.GET_SIGNATURES 的文档说“此常量在 API 级别 28 中已弃用。请改用 GET_SIGNING_CERTIFICATES”。

不幸的是,它并不安全,很容易被黑客入侵。

如何使用 Android P 引入的新“GET_SIGNING_CERTIFICATES”?

最佳答案

API28 或更高版本 中,您还应该检查 multipleSigners

这个函数将完成这项工作:
(适用于 Android 9.0 及更低版本)

fun getApplicationSignature(packageName: String = context.packageName): List<String> {
val signatureList: List<String>
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// New signature
val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNING_CERTIFICATES).signingInfo
signatureList = if (sig.hasMultipleSigners()) {
// Send all with apkContentsSigners
sig.apkContentsSigners.map {
val digest = MessageDigest.getInstance("SHA")
digest.update(it.toByteArray())
bytesToHex(digest.digest())
}
} else {
// Send one with signingCertificateHistory
sig.signingCertificateHistory.map {
val digest = MessageDigest.getInstance("SHA")
digest.update(it.toByteArray())
bytesToHex(digest.digest())
}
}
} else {
val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures
signatureList = sig.map {
val digest = MessageDigest.getInstance("SHA")
digest.update(it.toByteArray())
bytesToHex(digest.digest())
}
}

return signatureList
} catch (e: Exception) {
// Handle error
}
return emptyList()
}

byteToHex 是:

fun bytesToHex(bytes: ByteArray): String {
val hexArray = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
val hexChars = CharArray(bytes.size * 2)
var v: Int
for (j in bytes.indices) {
v = bytes[j].toInt() and 0xFF
hexChars[j * 2] = hexArray[v.ushr(4)]
hexChars[j * 2 + 1] = hexArray[v and 0x0F]
}
return String(hexChars)
}

关于android - 如何在 API 28 中使用 PackageInfo.GET_SIGNING_CERTIFICATES?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52041805/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com