gpt4 book ai didi

java - Google SafetyNet API JwsResult 是一个散列字符串而不是 JSON

转载 作者:行者123 更新时间:2023-12-01 14:31:53 26 4
gpt4 key购买 nike

我已经成功实现了 Google SafetyNet API,甚至得到了成功的响应。问题是 JWSResult来自 AttestationResponse是一个散列字符串,而我的期望是得到一个 JSON 作为响应。
请问我需要先从哪里找问题?
这是attest()的代码叫做:

    fun callSafetyNetAttentationApi(context: Activity, callback: SafetyNetCallback) {

if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {

val nonce: ByteArray? = getRequestNonce("Safety Net Sample: " + System.currentTimeMillis())
val client = SafetyNet.getClient(context)

nonce?.let {
val task: Task<AttestationResponse> = client.attest(it, BuildConfig.SAFETY_NET_KEY)

task.addOnSuccessListener { response -> safetyNetSuccess(response, callback) }
.addOnFailureListener { callback.isDeviceTrusted(false) }

} ?: kotlin.run {
callback.isDeviceTrusted(false)
}

} else {
MaterialDialog.Builder(context)
.title("The app cannot be used")
.content("Please update Google Play Services and try again.")
.cancelable(false)
.positiveText("Dismiss")
.onPositive { dialog, which -> context.finish() }
.show()
}
}

最佳答案

这是执行 safetyNetClient.attest(nonce, apiKey) 后您将收到的典型 JSON 响应。 :

{
"jwsResult": "foo.bar.zar",
"uid": "11426643",
"authCode": "H3o28i\/ViJUPRAW\/q4IUe1AMAbD-2jYp82os9v",
"app": 1,
"androidId": "vece15a43449afa9"
}
这里 foo.bar.zar是一个 base64 编码的字符串,类似于 aisnfaksdf.8439hundf.ghbadsjn ,其中每个部分对应于:
<Base64 encoded header>.<Base64 encoded JSON data>.<Base64 encoded signature>
你需要采取 bar和 Base64 解码以获得 SafetyNet 结果 JSON:
    private fun extractJwsData(jws: String?): ByteArray? {
val parts = jws?.split("[.]".toRegex())?.dropLastWhile { it.isEmpty() }?.toTypedArray()
if (parts?.size != 3) {
System.err.println("Failure: Illegal JWS signature format. The JWS consists of "
+ parts?.size + " parts instead of 3.")
return null
}
return Base64.decode(parts[1], Base64.DEFAULT)
}
然后使用您喜欢的 JSON 库构造 java 对象,例如GSON:
val model = Gson().fromJson(extractJwsData(jws).toString(), SafetyNetApiModel::class.java)
在哪里 SafetyNetApiModel是:
class SafetyNetApiModel {
@SerializedName("nonce")
var nonce: String? = null

@SerializedName("timestampMs")
var timestampMs: Long? = null

@SerializedName("apkPackageName")
var apkPackageName: String? = null

@SerializedName("apkCertificateDigestSha256")
var apkCertificateDigestSha256: List<String>? = null

@SerializedName("apkDigestSha256")
var apkDigestSha256: String? = null

@SerializedName("ctsProfileMatch")
var ctsProfileMatch: Boolean? = null

@SerializedName("basicIntegrity")
var basicIntegrity: Boolean? = null
}
看看 this以供引用。

关于java - Google SafetyNet API JwsResult 是一个散列字符串而不是 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62428525/

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