# `Strongbox`
[🔗](https://github.com/hattoriai/strongbox/blob/v0.1.0-dev/lib/strongbox.ex#L1)

Encrypted fields for Ecto.

A vault is a module of your app that holds the keys:

    defmodule MyApp.Vault do
      use Strongbox

      @impl true
      def keys do
        [%{tag: "k1", key: Application.fetch_env!(:my_app, :strongbox_key_k1)}]
      end

      @impl true
      def index_keys do
        [%{tag: "i1", key: Application.fetch_env!(:my_app, :strongbox_index_key_i1)}]
      end
    end

Then a schema names it for its encrypted fields:

    schema "users" do
      field :ssn, Strongbox.Encrypted, vault: MyApp.Vault, redact: true
      field :email, Strongbox.Encrypted, vault: MyApp.Vault, redact: true
      field :email_index, Strongbox.Index, vault: MyApp.Vault, of: :email, normalize: [:trim, :downcase]
    end

or, with `Strongbox.Schema`, which never forgets `redact: true`:

    use Strongbox.Schema, vault: MyApp.Vault

    schema "users" do
      encrypted_field :ssn
      encrypted_field :email
      blind_index :email_index, of: :email, normalize: [:trim, :downcase]
    end

A vault isn't a process: it starts nothing and holds no state. Its
callbacks are called for every value, so they must be cheap: read keys
that `config/runtime.exs` decoded once, never decode them in the callback.

## The vault's functions

`use Strongbox` defines, in the vault:

  * `encrypt(plaintext, opts)` and `decrypt(ciphertext, opts)` (and
    `decrypt!/2`), for binaries outside Ecto. Options: `:context`, a
    binary the value is bound to, which decrypting must give again; and
    `:pad`, a block size the value is padded to.
  * `hash(value, opts)`, a blind index of a binary with the first index
    key, and `hashes(value, opts)`, one with each index key, for lookups
    while index keys rotate. Options as `Strongbox.Index`'s:
    `:context`, `:bytes`, `:iterations` and `:compat`.
  * `stale?(ciphertext)`, whether a value was written with a key, or in a
    format, other than the first key's, so rotating would write it again.

# `index_keys`
*optional* 

```elixir
@callback index_keys() :: [Strongbox.Key.t() | map(), ...]
```

The keys of blind indexes. The first writes, all are looked up.

Each is a map (or a `Strongbox.Key`) with `:tag` and `:key`, and
optionally `:algorithm`. Never one of `c:keys/0`. Only needed for
`Strongbox.Index`.

# `keys`

```elixir
@callback keys() :: [Strongbox.Key.t() | map(), ...]
```

The keys that encrypt and decrypt. The first encrypts, all decrypt.

Each is a map (or a `Strongbox.Key`) with `:tag` and `:key`, and
optionally `:cipher` and `:format`. See `Strongbox.Key`.

# `decrypt`

```elixir
@spec decrypt(module(), binary(), keyword()) :: {:ok, binary()} | :error
```

Decrypts `ciphertext` with the key of `vault` it names.

Returns `:error` when no key has its tag, when it was changed, or when
the `:context` isn't the one it was encrypted with.

# `decrypt!`

```elixir
@spec decrypt!(module(), binary(), keyword()) :: binary()
```

Decrypts `ciphertext`, or raises `Strongbox.DecryptionError`.

# `encrypt`

```elixir
@spec encrypt(module(), binary(), keyword()) :: binary()
```

Encrypts `plaintext` with the first key of `vault`.

# `hash`

```elixir
@spec hash(module(), binary(), keyword()) :: binary()
```

The blind index of `value` with the first index key of `vault`.

# `hashes`

```elixir
@spec hashes(module(), binary(), keyword()) :: [binary(), ...]
```

The blind indexes of `value`, one with each index key of `vault`, without
repeats: what a lookup asks for while index keys rotate.

# `stale?`

```elixir
@spec stale?(module(), binary()) :: boolean()
```

Whether `ciphertext` was written with a key or in a format other than
the first key's, so that rotating would write it again.

---

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