# `AgeEx`
[🔗](https://github.com/Sorc96/age_ex/blob/v0.1.0/lib/age_ex.ex#L1)

The [age v1](https://age-encryption.org/v1) file format with X25519 keys, in pure Elixir.

It uses only OTP `:crypto`. Files work with the `age` and `rage` tools in both
directions, binary or ASCII-armored.

Two key types work: X25519 (`age1...`) and the hybrid post-quantum
ML-KEM-768 + X25519 type (`age1pq1...`). The post-quantum type needs OpenSSL
3.5 or later in OTP. Without it, the operations that need ML-KEM return
`{:error, :unsupported}`.

`encrypt/3` also accepts YubiKey recipients (`age1yubikey1...`) from
[age-plugin-yubikey](https://github.com/str4d/age-plugin-yubikey). Only the
YubiKey can decrypt them, with the `age` CLI and the plugin.

Not supported: passphrases (scrypt), SSH keys, other plugins. All data stays
in memory.

## Example

    %{identity: identity, recipient: recipient} = AgeEx.generate_identity()
    {:ok, ciphertext} = AgeEx.encrypt("secret", recipient)
    {:ok, "secret"} = AgeEx.decrypt(ciphertext, identity)

# `error`

```elixir
@type error() ::
  :invalid_identity
  | :invalid_recipient
  | :no_recipients
  | :incompatible_recipients
  | :unsupported
  | :armor_failure
  | :header_failure
  | :no_match
  | :hmac_failure
  | :payload_failure
```

Why an operation failed. The decrypt reasons match the classes of the
[C2SP age test vectors](https://github.com/C2SP/CCTV/tree/main/age).

# `identity`

```elixir
@type identity() :: String.t()
```

A private key: `AGE-SECRET-KEY-1...` or `AGE-SECRET-KEY-PQ-1...`.

# `recipient`

```elixir
@type recipient() :: String.t()
```

A public key: `age1...`, `age1pq1...` or `age1yubikey1...`.

# `decrypt`

```elixir
@spec decrypt(binary(), identity() | [identity()]) ::
  {:ok, binary()} | {:error, error()}
```

Decrypts an age file with one identity or a list of them.

Accepts binary and ASCII-armored files. Nothing is returned unless the whole
file decrypts and authenticates.

# `encrypt`

```elixir
@spec encrypt(iodata(), recipient() | [recipient()], keyword()) ::
  {:ok, binary()} | {:error, error()}
```

Encrypts `plaintext` to one recipient or a list of them.

Post-quantum recipients cannot be mixed with X25519 or YubiKey recipients,
because the file would then have no protection against quantum computers.
The `age` CLI refuses this too.

## Options

  * `:armor` - when `true`, returns ASCII-armored text. Defaults to `false`.

# `generate_identity`

```elixir
@spec generate_identity(keyword()) :: %{identity: identity(), recipient: recipient()}
```

Generates a new key pair.

## Options

  * `:post_quantum` - when `true`, makes a hybrid ML-KEM-768 + X25519 key, as
    `age-keygen -pq` does. Defaults to `false`.

# `parse_identities`

```elixir
@spec parse_identities(String.t()) ::
  {:ok, [identity()]} | {:error, :invalid_identity}
```

Reads the identities from the text of a key file, as `age-keygen` writes it.

Empty lines and lines that start with `#` are skipped. Every other line must
be an identity: X25519 (`age-keygen`) or post-quantum (`age-keygen -pq`).

# `recipient`

```elixir
@spec recipient(identity()) :: {:ok, recipient()} | {:error, :invalid_identity}
```

Returns the recipient (public key) of an identity.

---

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