g-andrade
ex_fpe - Format-preserving encryption for Elixir (FF1, FF3-1)
ExFPE encrypts a numerical string into another of the same length over the same alphabet.
Use cases
- credit-card numbers - encrypt them in a field or database column that only accepts credit-card -shaped values;
- sequential integers - encrypt them while preserving their domain:
- auto-incremented IDs in SQL databases;
- short, temporary sharing codes that look random but are the result of encrypting the outputs of a cycling counter;
- generate a 1-to-1 map from (sequence of consecutive integers) => (collection of seemingly random integers), in the same domain, no repeated elements.
And any other similar problems.
Example
Base 10
key = :crypto.strong_rand_bytes(32)
radix = 10
ctx = ExFPE.new!(key, radix)
tweak = "dev.env"
plaintext = "34436524"
ciphertext = ExFPE.encrypt!(ctx, tweak, plaintext)
^plaintext = ExFPE.decrypt!(ctx, tweak, ciphertext)
^ The encrypted ciphertext is always of the same length, and over the same alphabet, as plaintext.
Base 16
key = :crypto.strong_rand_bytes(32)
radix = 16
ctx = ExFPE.new!(key, radix)
tweak = "prod.env"
plaintext = "ABE19F00E22333445C"
ciphertext = ExFPE.encrypt!(ctx, tweak, plaintext)
^plaintext = ExFPE.decrypt!(ctx, tweak, ciphertext)
Custom alphabet
key = :crypto.strong_rand_bytes(32)
alphabet = "abcdeぜそぞ🦄🕞🌖"
ctx = ExFPE.new!(key, alphabet)
tweak = "tweak45"
plaintext = "🕞aebe🕞ぜそ🌖"
ciphertext = ExFPE.encrypt!(ctx, tweak, plaintext)
^plaintext = ExFPE.decrypt!(ctx, tweak, ciphertext)
Capabilities
- any base/radix from 2 up to 65535;
- optimized for bases between 2 and 36 if the alphabet matches the output of
Integer.to_string/2(upcase or downcase); - can otherwise use custom Unicode alphabets, with restrictions on accepted symbols;
use ExFPEto store context under your supervision tree, and access the API through generated functions that don’t require you to pass context every time;- can also operate in raw mode, encrypting and decrypting integer values directly.
Modes
By default, ExFPE uses FF1 (ExFPE.FF1), the only mode approved by NIST in SP 800-38Gr1 2pd. The other available mode is FF3-1 (ExFPE.FF3_1), which NIST no longer recommends.
I implemented the FF3-1 mode back in 2023, before the latest recommendation - which is why it’s part of this release.
Links
Popular in Announcing
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps:
bureaucrat - which contains a bunch ...
New
What is ContEx?
A pure Elixir server-side data plotting/charting library outputting SVG.
It has nice barcharts in particular and works g...
New
Here is my first stab at this. README pasted below.
https://github.com/Hal9000/elixir_random
Comments and critiques are welcome.
Thank...
New
Hello all,
I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New
Hey everyone :waving_hand:
Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-s...
New
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
New
Introducing assertions, the library that helps you write really great test assertions!
GitHub: GitHub - devonestes/assertions: Helpful a...
New
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New
Other popular topics
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









