# AeadSerializer

Artifact: [androidx.datastore:datastore-tink](https://developer.android.com/jetpack/androidx/releases/datastore) [View Source](https://cs.android.com/search?q=file:androidx/datastore/tink/AeadSerializer.kt+class:androidx.datastore.tink.AeadSerializer) Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.3.0)

*** ** * ** ***


```
public final class AeadSerializer<T extends Object> implements Serializer<@NonNull T>
```

<br />

*** ** * ** ***

A `https://developer.android.com/reference/androidx/datastore/core/Serializer` implementation that provides transparent encryption and decryption using Tink's `https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html` (Authenticated Encryption with Associated Data).

This class wraps another `https://developer.android.com/reference/androidx/datastore/core/Serializer`, encrypting the data before it is written to the `https://developer.android.com/reference/java/io/OutputStream.html` and decrypting it after it is read from the `https://developer.android.com/reference/java/io/InputStream.html`.

## Summary

| ### Public constructors |
|---|
| `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#AeadSerializer(com.google.crypto.tink.Aead,androidx.datastore.core.Serializer,kotlin.ByteArray)( @https://developer.android.com/reference/androidx/annotation/NonNull https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html aead, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/Serializer<@https://developer.android.com/reference/androidx/annotation/NonNull T> wrappedSerializer, @https://developer.android.com/reference/androidx/annotation/NonNull byte[] associatedData )` |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#getDefaultValue()()` Returns the default value for the data type `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull T` | `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#readFrom(java.io.InputStream)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/InputStream.html input)` Reads the data from the `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#readFrom(java.io.InputStream)`, decrypts it using the provided `https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html` instance, and delegates to the `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#wrappedSerializer()` to deserialize the decrypted bytes. |
| `void` | `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#writeTo(kotlin.Any,java.io.OutputStream)(@https://developer.android.com/reference/androidx/annotation/NonNull T t, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/OutputStream.html output)` Delegates to the `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#wrappedSerializer()` to serialize the data, encrypts it using the provided `https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html` instance, and writes the encrypted bytes to the `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#writeTo(kotlin.Any,java.io.OutputStream)`. |

## Public constructors

### AeadSerializer

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html aead` | The `https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html` instance used for encryption and decryption. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/Serializer<@https://developer.android.com/reference/androidx/annotation/NonNull T> wrappedSerializer` | The underlying serializer for the data type `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull byte[] associatedData` | The associated data to be used for encryption and decryption. It is very important to use a unique `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#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 `https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html` 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](https://developer.android.com/jetpack/androidx/releases/datastore#1.3.0)

```
public @NonNull T getDefaultValue()
```

Returns the default value for the data type `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer`.

### readFrom

```
public @NonNull T readFrom(@NonNull InputStream input)
```

Reads the data from the `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#readFrom(java.io.InputStream)`, decrypts it using the provided `https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html` instance, and delegates to the `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#wrappedSerializer()` to deserialize the decrypted bytes.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/datastore/core/CorruptionException` | if the data cannot be decrypted or if the `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#wrappedSerializer()` fails to read the data. |

### writeTo

Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.3.0)

```
public void writeTo(@NonNull T t, @NonNull OutputStream output)
```

Delegates to the `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#wrappedSerializer()` to serialize the data, encrypts it using the provided `https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html` instance, and writes the encrypted bytes to the `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer#writeTo(kotlin.Any,java.io.OutputStream)`.