gpt4 book ai didi

android - EncryptedSharedPreferences isUserAuthenticationRequired 无法正常工作

转载 作者:行者123 更新时间:2023-12-04 09:45:18 25 4
gpt4 key购买 nike

我正在使用 EncryptedSharedPreferences 来存储加密数据。

val biometricManager = BiometricManager.from(this)
val hasFingerprint = biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS

val advanceSpec = KeyGenParameterSpec.Builder(
"master_key",
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
).apply {
setBlockModes(KeyProperties.BLOCK_MODE_GCM)
setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
setKeySize(256)
if(hasFingerprint){
setUserAuthenticationRequired(true)
setUserAuthenticationValidityDurationSeconds(1)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
setInvalidatedByBiometricEnrollment(false)
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
setIsStrongBoxBacked(true)
setUserConfirmationRequired(true)
}
}
}.build()

val masterKey = MasterKeys.getOrCreate(advanceSpec)
val preferences = EncryptedSharedPreferences.create(
"TestPreferences",
masterKey,
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)

实际上,我错过了 BiometricPrompt 部分。我以为打电话 setUserAuthenticationRequired(true)将自动处理验证用户。但我们必须自己展示 BiometricPrompt。 isUserAuthenticationRequired only 确保只有在用户获得授权时才会激活 key 。
val biometricPrompt = BiometricPrompt(
activity,
ContextCompat.getMainExecutor(activity),
object: BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
createSharedPreferences()
}
}
)

biometricPrompt.authenticate(promptInfo)

但有个问题。它只在创建 EncryptedSharedPreferences 时抛出 UserNotAuthenticatedException .之后,我们可以根据需要执行读写操作。它没有考虑 setUserAuthenticationValidityDurationSeconds(1) .

最佳答案

我找到了 1 秒后不需要身份验证的原因。这是因为,一旦启动了加密共享首选项,它就会加载用于加密和解密内存中数据的 key ,然后这些 key 用于访问文件中的数据。你可以阅读里面的代码EncryptedSharedPreference类(class)。这是很明显的。

KeysetHandle daeadKeysetHandle = new AndroidKeysetManager.Builder()
.withKeyTemplate(prefKeyEncryptionScheme.getKeyTemplate())
.withSharedPref(context, KEY_KEYSET_ALIAS, fileName)
.withMasterKeyUri(KEYSTORE_PATH_URI + masterKeyAlias)
.build().getKeysetHandle();
KeysetHandle aeadKeysetHandle = new AndroidKeysetManager.Builder()
.withKeyTemplate(prefValueEncryptionScheme.getKeyTemplate())
.withSharedPref(context, VALUE_KEYSET_ALIAS, fileName)
.withMasterKeyUri(KEYSTORE_PATH_URI + masterKeyAlias)
.build().getKeysetHandle();

DeterministicAead daead = daeadKeysetHandle.getPrimitive(DeterministicAead.class);
Aead aead = aeadKeysetHandle.getPrimitive(Aead.class);

return new EncryptedSharedPreferences(fileName, masterKeyAlias,
context.getSharedPreferences(fileName, Context.MODE_PRIVATE), aead, daead);

关于android - EncryptedSharedPreferences isUserAuthenticationRequired 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62153438/

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