rodloboz

rodloboz

Strongbox — Encrypted fields for Ecto

Strongbox encrypts the sensitive fields of your Ecto schemas in your app, before they reach the database: a national ID, a phone number, a token for another service, notes about someone’s health. A leaked backup, a read replica or a query run by hand shows only ciphertext, and your app reads them as it always did. It runs in production in our apps. It’s at 0.1, so before 1.0 a minor version may still change the API.

It started from the Cloak / Cloak Ecto alternatives thread. Ecto 3.15 now brings Ecto.Encrypted and Ecto.HMAC, and if they are all you need, they’re a fine choice. Strongbox is for what comes around them, and it reads and writes their format, so you can start with one and move to the other.

What it does

  • A secret stays in its field. Each value is bound to its field’s name, so a value copied into another column (one that is shown, say) fails to decrypt.
  • The right key, or nothing. Each value names its key and carries a commitment to it, checked before decrypting. AES-GCM, AES-CCM or ChaCha20-Poly1305 from :crypto, with a key per value from a random salt.
  • Finding values again. Blind indexes with a key of their own each, normalizers, unique constraints, short hashes that tell less about which rows are equal, slow ones for values with few possibilities, and lookups that still match while index keys rotate.
  • Rotation you can run. Put a new key first and deploy. Strongbox.Rotation.run/3 rewrites only what is under an old key, by compare-and-swap, so never over a write made meanwhile, and report/3 says what is left. PostgreSQL and SQLite.
  • Nothing to write yourself. Strings, numbers, dates, maps and lists are options of the field (as: :date), and so are padding (pad: 64) and Base64 for text columns and embeds. Fields declared with Strongbox.Schema are always redacted.
  • A way in. It reads what Cloak (AES.GCM and AES.CTR) and Ecto.Encrypted wrote, as it is, and keeps Cloak.Ecto.HMAC’s and Ecto.HMAC’s hashes. A rotation writes them again in its own format, bound to their field.
  • No process. A vault is a module: there is nothing to start. The format is documented byte by byte.

A taste

A vault 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

Encrypted fields, and a blind index to find one, in :binary columns:

use Strongbox.Schema, vault: MyApp.Vault

schema "patients" do
  field :name, :string
  encrypted_field :national_id
  encrypted_field :born_on, as: :date
  encrypted_field :email, as: :string
  blind_index :email_index, of: :email, normalize: [:trim, :downcase]
end

def changeset(patient, attrs) do
  patient
  |> cast(attrs, [:name, :national_id, :born_on, :email])
  |> Strongbox.Changeset.put_indexes()
  |> unique_constraint(:email_index)
end
Patient
|> Strongbox.Query.where_index(:email_index, "Ana@Example.com")
|> Repo.one()

A new key, and the old values written again under it:

# keys/0: [%{tag: "k2", key: k2}, %{tag: "k1", key: k1}]

Strongbox.Rotation.report(Repo, Patient)
#=> %{national_id: %{"k2" => 1_204, "k1" => 88_311, nil => 12}, ...}

Strongbox.Rotation.run(Repo, Patient)
#=> {:ok, %{rewritten: 88_311, reindexed: 0, conflicts: 0, unreadable: 0}}

Coming from Cloak, the Cloak key goes after a Strongbox one, and the fields change type, not their data:

[
  %{tag: "k1", key: k1},
  %{tag: "AES.GCM.V1", key: cloak_key, format: :cloak, iv_length: 12}
]

Feedback very welcome.

Where Next? Top

Trending in Announcing Top

RobertDober
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
241 15527 141
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
alisinabh
Hello Elixir community! I am excited to announce the release of a new Elixir library for interacting with the Ethereum blockchain: Ether...
New
Yyeger
Continuum is an OTP-native durable execution engine for Elixir, backed by Postgres. IIt’s an Elixir-native answer to Temporal in other ec...
New

Other Trending Topics Top

PJUllrich
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
mat-hek
Popcorn, the library that runs Elixir code in the browser, is now stable :popcorn: This is possible because it now builds on a very stab...
New
bartblast
If you haven’t tried Hologram yet, what’s stopped you? Here’s why I’m asking. Building a framework turns out to be two jobs, the framewo...
New
sevenseacat
Well some of us wanted a difficulty spike - and today we got one :sweat_smile: https://github.com/sevenseacat/advent_of_code/blob/main/l...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews