# `Countersign.Crypto`
[🔗](https://github.com/mkreyman/countersign/blob/v0.1.0/lib/countersign/crypto.ex#L1)

Encryption and digests under keys derived from the host's
`secret_key_base` (`Countersign.Config`).

* Addresses and tokens are encrypted with `Plug.Crypto.MessageEncryptor`
  (AES-256-GCM), each purpose under its own derived key pair, so a token
  ciphertext can never be decrypted as an address or the other way round.
* An address digest is HMAC-SHA256 of the normalised address under its own
  derived key: equal addresses give equal digests, and the digest reveals
  nothing without the secret.

# `purpose`

```elixir
@type purpose() :: :address | :token | :code
```

# `decrypt`

```elixir
@spec decrypt(Countersign.Config.t(), purpose(), binary() | nil) ::
  {:ok, String.t()} | :error
```

Decrypts what `encrypt/3` produced for the same `purpose`.

# `digest`

```elixir
@spec digest(Countersign.Config.t(), String.t(), String.t()) :: binary()
```

HMAC-SHA256 (32 bytes) of the normalised `address` on `channel`.

An email is trimmed and lowercased; a phone number keeps its digits only,
with a leading US country code `1` dropped from an 11-digit number.

# `encrypt`

```elixir
@spec encrypt(Countersign.Config.t(), purpose(), String.t()) :: binary()
```

Encrypts `plaintext` for `purpose`.

# `normalize`

```elixir
@spec normalize(String.t(), String.t()) :: String.t()
```

The normalised form of `address` that `digest/3` hashes.

---

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