为了使用,EncryptedSharedPreferences
我添加了这个依赖
implementation "androidx.security:security-crypto:1.0.0"
我这样使用它
...
val sharedPref = EncryptedSharedPreferences.create(
PREF_NAME,
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
...
然后我遇到了 off doc - https://developer.android.com/reference/androidx/security/crypto/MasterKeys其中说这个类deprecated
,这里有一些关于如何正确执行它的建议 - https://stackoverflow。 com/a/62504831/5098477
但是,为什么当我在代码中打开一个类或方法的实现时,没有提到它是什么deprecated
?
/**
* Convenient methods to create and obtain master keys in Android Keystore.
*
* <p>The master keys are used to encrypt data encryption keys for encrypting files and preferences.
*/
public final class MasterKeys {
private MasterKeys() {
}
或者在这里
/**
* Creates or gets the master key provided
*
* The encryption scheme is required fields to ensure that the type of
* encryption used is clear to developers.
*
* @param keyGenParameterSpec The key encryption scheme
* @return The key alias for the master key
*/
@NonNull
public static String getOrCreate(
@NonNull KeyGenParameterSpec keyGenParameterSpec)
throws GeneralSecurityException, IOException {
validate(keyGenParameterSpec);
if (!MasterKeys.keyExists(keyGenParameterSpec.getKeystoreAlias())) {
generateKey(keyGenParameterSpec);
}
return keyGenParameterSpec.getKeystoreAlias();
}
我不明白为什么不显示这些方法deprecated
?
您包括库版本1.0.0,当时该类
MasterKeys
未标记为已弃用并且没有类MasterKey.Builder
(建议使用而不是MasterKeys
)。在Release Notes中,您可以看到该类
MasterKeys
已被标记为 Deprecated since version 1.1.0-alpha01。您可以尝试使用不稳定版本1.1.0-alpha01,或者等到1.1.0发布,这样您就可以使用新类
MasterKey.Builder
而不是MasterKeys
.