# 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)

*** ** * ** ***


```
class AeadSerializer<T : Any?> : Serializer<T>
```

<br />

*** ** * ** ***

A `https://developer.android.com/reference/kotlin/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/kotlin/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 : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/tink/AeadSerializer#AeadSerializer(com.google.crypto.tink.Aead,androidx.datastore.core.Serializer,kotlin.ByteArray)( aead: https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html, wrappedSerializer: https://developer.android.com/reference/kotlin/androidx/datastore/core/Serializer<T>, associatedData: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html )` |

| ### Public functions |
|---|---|
| `open suspend T` | `https://developer.android.com/reference/kotlin/androidx/datastore/tink/AeadSerializer#readFrom(java.io.InputStream)(input: https://developer.android.com/reference/java/io/InputStream.html)` Reads the data from the `https://developer.android.com/reference/kotlin/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/kotlin/androidx/datastore/tink/AeadSerializer#wrappedSerializer()` to deserialize the decrypted bytes. |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/tink/AeadSerializer#writeTo(kotlin.Any,java.io.OutputStream)(t: T, output: https://developer.android.com/reference/java/io/OutputStream.html)` Delegates to the `https://developer.android.com/reference/kotlin/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/kotlin/androidx/datastore/tink/AeadSerializer#writeTo(kotlin.Any,java.io.OutputStream)`. |

| ### Public properties |
|---|---|
| `open T` | `https://developer.android.com/reference/kotlin/androidx/datastore/tink/AeadSerializer#defaultValue()` Returns the default value for the data type `https://developer.android.com/reference/kotlin/androidx/datastore/tink/AeadSerializer`. |

## Public constructors

### AeadSerializer

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

| Parameters |
|---|---|
| `aead: https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html` | The `https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html` instance used for encryption and decryption. |
| `wrappedSerializer: https://developer.android.com/reference/kotlin/androidx/datastore/core/Serializer<T>` | The underlying serializer for the data type `https://developer.android.com/reference/kotlin/androidx/datastore/tink/AeadSerializer`. |
| `associatedData: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html = byteArrayOf()` | The associated data to be used for encryption and decryption. It is very important to use a unique `https://developer.android.com/reference/kotlin/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 functions

### readFrom

```
open suspend fun readFrom(input: InputStream): T
```

Reads the data from the `https://developer.android.com/reference/kotlin/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/kotlin/androidx/datastore/tink/AeadSerializer#wrappedSerializer()` to deserialize the decrypted bytes.

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/datastore/core/CorruptionException` | if the data cannot be decrypted or if the `https://developer.android.com/reference/kotlin/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)

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

Delegates to the `https://developer.android.com/reference/kotlin/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/kotlin/androidx/datastore/tink/AeadSerializer#writeTo(kotlin.Any,java.io.OutputStream)`.

## Public properties

### defaultValue

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

```
open val defaultValue: T
```

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