Class SharedPrefKeysetWriter
- java.lang.Object
-
- com.google.crypto.tink.integration.android.SharedPrefKeysetWriter
-
- All Implemented Interfaces:
KeysetWriter
public final class SharedPrefKeysetWriter extends java.lang.Object implements KeysetWriter
AKeysetWriterthat 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 Summary
Constructors Constructor Description SharedPrefKeysetWriter(android.content.Context context, java.lang.String keysetName, java.lang.String prefFileName)Creates aKeysetReaderthat hex-encodes and writes keysets to the preference namekeysetNamein the private shared preferences fileprefFileName.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidwrite(com.google.crypto.tink.proto.EncryptedKeyset keyset)Tries to write anEncryptedKeysetto some storage system.voidwrite(com.google.crypto.tink.proto.Keyset keyset)Tries to write aKeysetto some storage system.
-
-
-
Constructor Detail
-
SharedPrefKeysetWriter
public SharedPrefKeysetWriter(android.content.Context context, java.lang.String keysetName, java.lang.String prefFileName)Creates aKeysetReaderthat hex-encodes and writes keysets to the preference namekeysetNamein the private shared preferences fileprefFileName.If
prefFileNameis null, uses the default shared preferences file.- Throws:
java.io.IOException- if cannot write the keysetjava.lang.IllegalArgumentException- ifkeysetNameis null
-
-
Method Detail
-
write
public void write(com.google.crypto.tink.proto.Keyset keyset) throws java.io.IOExceptionDescription copied from interface:KeysetWriterTries to write aKeysetto some storage system.- Specified by:
writein interfaceKeysetWriter- Throws:
java.io.IOException
-
write
public void write(com.google.crypto.tink.proto.EncryptedKeyset keyset) throws java.io.IOExceptionDescription copied from interface:KeysetWriterTries to write anEncryptedKeysetto some storage system.- Specified by:
writein interfaceKeysetWriter- Throws:
java.io.IOException
-
-