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

An encrypted field.

    field :ssn, Strongbox.Encrypted, vault: MyApp.Vault, redact: true
    field :notes, Strongbox.Encrypted, vault: MyApp.Vault, as: :map, redact: true
    field :born_on, Strongbox.Encrypted, vault: MyApp.Vault, as: :date, redact: true

The column is `:binary` in migrations (`bytea` in PostgreSQL), or
`:text` with `encoding: :base64`.

Pass `redact: true`, or use `Strongbox.Schema`, which always does: else
the decrypted value shows when the struct is inspected, in logs and in
error reports.

## Options

  * `:vault` - the vault, a module that uses `Strongbox`. Required.
  * `:as` - what the field holds: `:binary` (the default, any binary),
    `:string` (valid UTF-8), `:integer`, `:float`, `:decimal`, `:boolean`,
    `:date`, `:time`, `:naive_datetime`, `:utc_datetime`, `:map`,
    `{:array, :string}` or `{:array, :integer}`. Maps and lists are kept
    as JSON, so a map's keys load as strings, as with Ecto's `:map`.
  * `:context` - the binary each value is bound to. By default, the
    field's name: a value copied into another field (`ssn` into `notes`)
    no longer decrypts there. Give it to keep values readable when the
    field is renamed: `context: "ssn"`. `false` binds values to nothing,
    as `Ecto.Encrypted` and Cloak do.
  * `:pad` - a block size, such as `16` or `64`: each value is padded to
    a multiple of it before it is encrypted, so its length shows only to
    the block. Use it for fields with few values (`"yes"`, `"no"`) or
    whose length tells something.
  * `:encoding` - `:raw` (the default) keeps the encrypted bytes as they
    are, in a binary column. `:base64` keeps them as text: for a `:text`
    column, and for fields of embedded schemas kept as JSON.

## Querying

Each value is encrypted with a random salt, so the same value never
encrypts twice the same way, and `where: u.ssn == ^ssn` matches nothing.
To look a value up, keep a blind index of it beside it:
`Strongbox.Index`.

---

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