AeadSerializer


class AeadSerializer<T : Any?> : 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 : Any?> AeadSerializer(
    aead: Aead,
    wrappedSerializer: Serializer<T>,
    associatedData: ByteArray
)

Public functions

open suspend T

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

open suspend Unit
writeTo(t: T, output: OutputStream)

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

Public properties

open T

Returns the default value for the data type T.

Public constructors

AeadSerializer

<T : Any?> AeadSerializer(
    aead: Aead,
    wrappedSerializer: Serializer<T>,
    associatedData: ByteArray = byteArrayOf()
)
Parameters
aead: Aead

The Aead instance used for encryption and decryption.

wrappedSerializer: Serializer<T>

The underlying serializer for the data type T.

associatedData: ByteArray = byteArrayOf()

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 functions

readFrom

open suspend fun readFrom(input: InputStream): T

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
open suspend fun writeTo(t: T, output: OutputStream): Unit

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

Public properties

defaultValue

Added in 1.3.0-alpha10
open val defaultValue: T

Returns the default value for the data type T.