public final class SharedPrefKeysetWriter extends Object implements KeysetWriter
KeysetWriter that can write keysets to private shared preferences on Android.
We do not recommend new uses of this class. Instead, if you want to store a Tink keyset in shared preferences, serialize the keyset using TinkProtoKeysetFormat, and write it hex-encoded into your SharedPreferences manually.
For example, to write an encrypted keyset to the shared preferences, you can replace this:
keysetHandle.write(
new SharedPrefKeysetWriter(
ApplicationProvider.getApplicationContext(), keysetName, null),
keysetEncryptionAead);
with this:
byte[] encryptedKeyset =
TinkProtoKeysetFormat.serializeEncryptedKeyset(
handle, keysetEncryptionAead, new byte[0]);
SharedPreferences.Editor editor =
PreferenceManager.getDefaultSharedPreferences(
ApplicationProvider.getApplicationContext().getApplicationContext())
.edit();
boolean success = editor.putString(keysetName, Hex.encode(encryptedKeyset)).commit();
and to write an unencrypted keyset to the shared preferences, you can replace this:
CleartextKeysetHandle.write(
handle,
new SharedPrefKeysetWriter(
ApplicationProvider.getApplicationContext(), keysetName, null));
with this:
byte[] serializedKeyset =
TinkProtoKeysetFormat.serializeKeyset(handle, InsecureSecretKeyAccess.get());
SharedPreferences.Editor editor =
PreferenceManager.getDefaultSharedPreferences(
ApplicationProvider.getApplicationContext().getApplicationContext())
.edit();
boolean success = editor.putString(keysetName, Hex.encode(serializedKeyset)).commit();
| Constructor and Description |
|---|
SharedPrefKeysetWriter(Context context,
String keysetName,
String prefFileName)
Creates a
KeysetReader that hex-encodes and writes keysets to the preference
name keysetName in the private shared preferences file prefFileName. |
| Modifier and Type | Method and Description |
|---|---|
void |
write(EncryptedKeyset keyset)
Tries to write an
EncryptedKeyset to some storage system. |
void |
write(Keyset keyset)
Tries to write a
Keyset to some storage system. |
public SharedPrefKeysetWriter(Context context, String keysetName, String prefFileName)
KeysetReader that hex-encodes and writes keysets to the preference
name keysetName in the private shared preferences file prefFileName.
If prefFileName is null, uses the default shared preferences file.
IOException - if cannot write the keysetIllegalArgumentException - if keysetName is nullpublic void write(Keyset keyset) throws IOException
KeysetWriterKeyset to some storage system.write in interface KeysetWriterIOExceptionpublic void write(EncryptedKeyset keyset) throws IOException
KeysetWriterEncryptedKeyset to some storage system.write in interface KeysetWriterIOException