AeadSerializer


public final class AeadSerializer<T extends Object> implements Serializer


A Serializer implementation that provides transparent encryption and decryption using Tink's Aead (Authenticated Encryption with Associated Data).

This class wraps another Serializer, encrypting the data before it is written to the OutputStream and decrypting it after it is read from the InputStream.

Summary

Public constructors

<T extends Object> AeadSerializer(
    @NonNull Aead aead,
    @NonNull Serializer<@NonNull T> wrappedSerializer,
    @NonNull byte[] associatedData
)

Public methods

@NonNull T

Returns the default value for the data type T.

@NonNull T

Reads the data from the input, decrypts it using the provided Aead instance, and delegates to the wrappedSerializer to deserialize the decrypted bytes.

void

Delegates to the wrappedSerializer to serialize the data, encrypts it using the provided Aead instance, and writes the encrypted bytes to the output.

Public constructors

AeadSerializer

public <T extends Object> AeadSerializer(
    @NonNull Aead aead,
    @NonNull Serializer<@NonNull T> wrappedSerializer,
    @NonNull byte[] associatedData
)
Parameters
@NonNull Aead aead

The Aead instance used for encryption and decryption.

@NonNull Serializer<@NonNull T> wrappedSerializer

The underlying serializer for the data type T.

@NonNull byte[] associatedData

The associated data to be used for encryption and decryption. It is very important to use a unique associatedData value so that if the same key is used to encrypt multiple sets of data, it will still be safe from a ciphertext swapping/substitution attack. Users should use something unique like the filename as the associated data. If they are sure that the Aead instance is not reused across multiple data stores, they can set a default associated data value of byteArrayOf().

Public methods

getDefaultValue

Added in 1.3.0-alpha10
public @NonNullgetDefaultValue()

Returns the default value for the data type T.

readFrom

public @NonNullreadFrom(@NonNull InputStream input)

Reads the data from the input, decrypts it using the provided Aead instance, and delegates to the wrappedSerializer to deserialize the decrypted bytes.

Throws
androidx.datastore.core.CorruptionException

if the data cannot be decrypted or if the wrappedSerializer fails to read the data.

writeTo

Added in 1.3.0-alpha10
public void writeTo(@NonNull T t, @NonNull OutputStream output)

Delegates to the wrappedSerializer to serialize the data, encrypts it using the provided Aead instance, and writes the encrypted bytes to the output.