Skip to main content

.iex.exs

alias Kryptex.Cipher
alias Kryptex.EncryptedField
alias Kryptex.Keyring

encrypt = fn value ->
  value
  |> to_string()
  |> Cipher.encrypt()
end

decrypt = fn ciphertext ->
  Cipher.decrypt(ciphertext)
end

roundtrip = fn value ->
  with {:ok, ciphertext} <- Cipher.encrypt(to_string(value)),
       {:ok, plaintext} <- Cipher.decrypt(ciphertext) do
    %{ciphertext: ciphertext, plaintext: plaintext}
  end
end

field_roundtrip = fn value, source_type ->
  params = EncryptedField.init(source_type: source_type)

  with {:ok, dumped} <- EncryptedField.dump(value, &Ecto.Type.dump/2, params),
       {:ok, loaded} <- EncryptedField.load(dumped, &Ecto.Type.load/2, params) do
    %{dumped: dumped, loaded: loaded}
  end
end

current_key = fn ->
  Keyring.current_key()
end

IO.puts("")
IO.puts("Kryptex helpers loaded: encrypt, decrypt, roundtrip, field_roundtrip, current_key")
IO.puts("")