AeadSerializer


public final class AeadSerializer<T extends Object>


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 ERROR CLASS: Symbol not found for Aead aead,
    @NonNull ERROR CLASS: Symbol not found for Serializer<T> wrappedSerializer,
    @NonNull byte[] associatedData
)

Public methods

@NonNull T

Returns the default value for the data type T.

@NonNull T
@Throws(exceptionClasses = [CorruptionException<ERROR CLASS>])
readFrom(@NonNull ERROR CLASS: Symbol not found for 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.

void
writeTo(
    @NonNull T t,
    @NonNull ERROR CLASS: Symbol not found for 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.

Public constructors

AeadSerializer

public <T extends Object> AeadSerializer(
    @NonNull ERROR CLASS: Symbol not found for Aead aead,
    @NonNull ERROR CLASS: Symbol not found for Serializer<T> wrappedSerializer,
    @NonNull byte[] associatedData
)
Parameters
@NonNull ERROR CLASS: Symbol not found for Aead aead

The Aead instance used for encryption and decryption.

@NonNull ERROR CLASS: Symbol not found for Serializer<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-alpha07
public @NonNullgetDefaultValue()

Returns the default value for the data type T.

readFrom

@Throws(exceptionClasses = [CorruptionException<ERROR CLASS>])
public @NonNullreadFrom(@NonNull ERROR CLASS: Symbol not found for 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
java.security.GeneralSecurityException

if the data cannot be decrypted

androidx.datastore.core.CorruptionException

if the wrappedSerializer fails to read the data.

writeTo

public void writeTo(
    @NonNull T t,
    @NonNull ERROR CLASS: Symbol not found for 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.