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

A key of a vault, with the tag that names it in each value it encrypts.

A vault's `c:Strongbox.keys/0` and `c:Strongbox.index_keys/0` may return
maps or these structs; Strongbox turns maps into structs and checks them.
Inspecting a key never shows its bytes, so a key in a log or an error
report gives away only its tag.

## Fields

  * `:tag` - a binary of 1 to 127 bytes, written in front of each value
    the key encrypts, so the right key is found to decrypt it.
  * `:key` - the key itself, as a binary.
  * `:cipher` - for encryption keys, an AEAD cipher of `:crypto`:
    `:aes_256_gcm` (the default), `:aes_128_gcm`, `:aes_256_ccm` or
    `:chacha20_poly1305`.
  * `:format` - how values under this key are written and read:
    `:strongbox` (the default), `:ecto` (as `Ecto.Encrypted`), or, to read
    values Cloak wrote, `:cloak` (Cloak's AES.GCM) or `:cloak_ctr`
    (Cloak's AES.CTR). See the [format](format.md) guide.
  * `:iv_length` - for `:cloak` keys only, the length of Cloak's IVs:
    16 unless the Cloak vault was given another `:iv_length`.
  * `:algorithm` - for index keys, the hash of the HMAC: `:sha256` (the
    default), `:sha384`, `:sha512`, `:sha3_256` and so on.

## Making a key

    $ mix strongbox.gen.key

prints a new random key in Base64. Keep it in an environment variable
and read it in `config/runtime.exs` with `decode!/1`.

# `format`

```elixir
@type format() :: :strongbox | :ecto | :cloak | :cloak_ctr
```

# `t`

```elixir
@type t() :: %Strongbox.Key{
  algorithm: atom(),
  cipher: atom(),
  format: format(),
  iv_length: pos_integer(),
  key: binary(),
  tag: binary()
}
```

# `decode!`

```elixir
@spec decode!(binary()) :: binary()
```

Decodes a key kept in Base64, as `mix strongbox.gen.key` prints it.

Raises `ArgumentError` without the value if it isn't Base64.

# `generate`

```elixir
@spec generate(pos_integer()) :: binary()
```

Returns `bytes` random bytes, 32 by default: a new key.

---

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