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

A blind index: a keyed hash of an encrypted field's value, kept beside it
so the value can be looked up.

    field :email, Strongbox.Encrypted, vault: MyApp.Vault, redact: true
    field :email_index, Strongbox.Index, vault: MyApp.Vault, of: :email,
      normalize: [:trim, :downcase], redact: true

The column is `:binary` in migrations, and can have a unique index.

The index field holds the value it indexes until the record is saved,
and its hash once saved or loaded; so pass `redact: true`, or use
`Strongbox.Schema`. `Strongbox.Changeset.put_indexes/1` copies each
field's value into its indexes when it changes.

Look a value up with `Strongbox.Query.where_index/3`, which asks for its
hash under each index key, so lookups work while index keys rotate:

    User |> Strongbox.Query.where_index(:email_index, "jose@example.com") |> Repo.one()

`where: u.email_index == ^"jose@example.com"` works too, under the first
index key only.

## Options

  * `:vault` - the vault, whose `c:Strongbox.index_keys/0` hash. Required.
  * `:of` - the field this indexes, for `Strongbox.Changeset.put_indexes/1`
    and `Strongbox.Rotation`.
  * `:as` - what the value is, as in `Strongbox.Encrypted`: `:binary`
    (the default), `:string`, `:integer`, `:date`… It is hashed as it is
    encrypted.
  * `:normalize` - what lookups may ignore, a list applied in order:
    `:trim`, `:squish` (runs of whitespace as one space), `:downcase`,
    `:upcase`, `:nfc` and `:nfkc` (Unicode normalization), `:digits`
    (only the digits: phone numbers), or `{module, function}`, called with
    the binary. Writing and querying normalize the same way. The
    encrypted field keeps the value as it was given.
  * `:context` - what this index's own key is derived from. By default
    the index field's name, so the same value gives unrelated hashes in
    two indexes. Give it to keep lookups working when the field is
    renamed.
  * `:bytes` - keep only the first bytes of each hash. Fewer bytes match
    some other values too (false positives, which the lookup's caller
    checks against the decrypted value), and so tell less about which
    rows share a value. Leave it out for unique lookups.
  * `:iterations` - hash with PBKDF2 of so many iterations instead of one
    HMAC, for values with few possibilities (phone numbers, dates of
    birth), so that guessing them is slow even with the index key.
    Each lookup and each write pays for it: 10_000 takes a few
    milliseconds.
  * `:compat` - `:ecto` or `:cloak`, to keep the hashes `Ecto.HMAC` or
    `Cloak.Ecto.HMAC` wrote, with their key as the first index key: no
    hash is computed again. See the [from Cloak](from_cloak.md) guide.

Hashes don't hide which rows share a value, nor how often each value
appears. Index only the fields you look up.

# `hashes`

```elixir
@spec hashes(module(), atom(), term()) :: [binary()]
```

The hashes of `value` in the index `field` of `schema`, one under each
index key: what a lookup asks for. Raises if `field` isn't an index.

---

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