Class SharedPrefKeysetWriter

  • All Implemented Interfaces:
    KeysetWriter

    public final class SharedPrefKeysetWriter
    extends java.lang.Object
    implements KeysetWriter
    A 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 Summary

      Constructors 
      Constructor Description
      SharedPrefKeysetWriter​(android.content.Context context, java.lang.String keysetName, java.lang.String prefFileName)
      Creates a KeysetReader that hex-encodes and writes keysets to the preference name keysetName in the private shared preferences file prefFileName.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void write​(com.google.crypto.tink.proto.EncryptedKeyset keyset)
      Tries to write an EncryptedKeyset to some storage system.
      void write​(com.google.crypto.tink.proto.Keyset keyset)
      Tries to write a Keyset to some storage system.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SharedPrefKeysetWriter

        public SharedPrefKeysetWriter​(android.content.Context context,
                                      java.lang.String keysetName,
                                      java.lang.String prefFileName)
        Creates a 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.

        Throws:
        java.io.IOException - if cannot write the keyset
        java.lang.IllegalArgumentException - if keysetName is null
    • Method Detail

      • write

        public void write​(com.google.crypto.tink.proto.Keyset keyset)
                   throws java.io.IOException
        Description copied from interface: KeysetWriter
        Tries to write a Keyset to some storage system.
        Specified by:
        write in interface KeysetWriter
        Throws:
        java.io.IOException
      • write

        public void write​(com.google.crypto.tink.proto.EncryptedKeyset keyset)
                   throws java.io.IOException
        Description copied from interface: KeysetWriter
        Tries to write an EncryptedKeyset to some storage system.
        Specified by:
        write in interface KeysetWriter
        Throws:
        java.io.IOException