# `ExWapp.Media.Crypto`
[🔗](https://github.com/elchemista/ex_wapp/blob/v0.1.5/lib/ex_wapp/media/crypto.ex#L1)

WhatsApp media encryption and decryption helpers.

This follows the same media-key expansion used by popular WhatsApp Web
clients: HKDF-SHA256 expands the 32-byte media key into IV, cipher, MAC, and
reference keys. The payload is then encrypted with AES-256-CBC and protected
with the first 10 bytes of an HMAC-SHA256 digest over `IV || ciphertext`.

# `media_keys`

```elixir
@type media_keys() :: %{
  iv: binary(),
  cipher_key: binary(),
  mac_key: binary(),
  ref_key: binary()
}
```

# `decrypt_download`

```elixir
@spec decrypt_download(binary(), ExWapp.Media.Ref.t()) ::
  {:ok, binary()} | {:error, term()}
```

Decrypts an encrypted media body, or accepts a server-declared unencrypted
body when no encrypted SHA-256 is present, and validates all available
integrity checks.

# `derive_keys`

```elixir
@spec derive_keys(binary(), ExWapp.Media.download_type()) :: media_keys()
```

Derives WhatsApp media keys for the given media type.

# `encrypt_upload`

```elixir
@spec encrypt_upload(binary(), ExWapp.Media.media_type()) ::
  {:ok, ExWapp.Media.Upload.t(), binary()}
```

Encrypts plaintext media bytes and returns upload metadata plus encrypted body.

# `media_info`

```elixir
@spec media_info(ExWapp.Media.download_type()) :: binary()
```

Returns the WhatsApp HKDF context string for a media type.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
