# `BSV.SymmetricKey`
[🔗](https://github.com/Bittoku/bsv_sdk_elixir/blob/main/lib/bsv/symmetric_key.ex#L1)

AES-256-GCM symmetric encryption.

## Migration Notice (v0.2)

The IV size was changed from 32 bytes to the standard 12 bytes for GCM.
Decryption automatically detects the IV size of the ciphertext:

- New ciphertexts use 12-byte IVs (standard, secure)
- Legacy ciphertexts with 32-byte IVs are still decryptable

Encryption always uses the new 12-byte IV format. To migrate legacy data,
decrypt with the old key and re-encrypt — the new format is used automatically.

# `t`

```elixir
@type t() :: %BSV.SymmetricKey{raw: &lt;&lt;_::256&gt;&gt;}
```

# `decrypt`

```elixir
@spec decrypt(&lt;&lt;_::256&gt;&gt; | t(), binary()) ::
  {:ok, binary()} | {:error, :decrypt_failed}
```

Decrypt ciphertext with a 32-byte key using AES-256-GCM.

Automatically detects IV size: tries 12-byte (current) first, then
falls back to 32-byte (legacy) for backward compatibility.

# `decrypt`

```elixir
@spec decrypt(&lt;&lt;_::256&gt;&gt; | t(), binary(), binary()) ::
  {:ok, binary()} | {:error, :decrypt_failed}
```

Decrypt with explicit Additional Authenticated Data (AAD).

# `encrypt`

```elixir
@spec encrypt(&lt;&lt;_::256&gt;&gt; | t(), binary()) :: {:ok, binary()}
```

Encrypt plaintext with a 32-byte key using AES-256-GCM (12-byte IV).

The 2-arity version passes empty AAD (`<<>>`) for backward compatibility with
BRC-78 and other implementations. Use `encrypt/3` to supply custom Additional
Authenticated Data for extra context binding.

# `encrypt`

```elixir
@spec encrypt(&lt;&lt;_::256&gt;&gt; | t(), binary(), binary()) :: {:ok, binary()}
```

Encrypt with explicit Additional Authenticated Data (AAD).

# `new`

```elixir
@spec new(&lt;&lt;_::256&gt;&gt;) :: t()
```

Create a new SymmetricKey from a 32-byte binary.

# `to_bytes`

```elixir
@spec to_bytes(t()) :: &lt;&lt;_::256&gt;&gt;
```

Get the raw key bytes.

---

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