AF
Ash Frameworkβ€’2mo ago
Jechol

πŸ” AshFeistelCipher - Encrypted Integer IDs for Ash Resources

Sequential IDs (1, 2, 3...) leak business information. AshFeistelCipher provides a declarative DSL to encrypt integer IDs using Feistel cipher, transforming sequential integers into non-sequential, unpredictable values automatically via database triggers. Key Features βœ… Secure IDs without UUIDs - Hide sequential patterns while keeping efficient integer IDs (bigint) βœ… Automatic encryption - Database triggers handle encryption transparently
βœ… Collision-free - Deterministic one-to-one mapping βœ… Declarative DSL - Clean, Ash-idiomatic syntax Quick Example
defmodule MyApp.Post do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshFeistelCipher]

postgres do
table "posts"
repo MyApp.Repo
end

attributes do
integer_sequence :seq
encrypted_integer_primary_key :id, from: :seq

attribute :title, :string, allow_nil?: false
end
end
defmodule MyApp.Post do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshFeistelCipher]

postgres do
table "posts"
repo MyApp.Repo
end

attributes do
integer_sequence :seq
encrypted_integer_primary_key :id, from: :seq

attribute :title, :string, allow_nil?: false
end
end
# Create posts - seq and id are auto-generated
post = MyApp.Post.create!(%{title: "Hello World"})
# => %MyApp.Post{seq: 1, id: 3_141_592_653, title: "Hello World"}

post2 = MyApp.Post.create!(%{title: "Second Post"})
# => %MyApp.Post{seq: 2, id: 2_718_281_828, title: "Second Post"}

# Query by encrypted id
MyApp.Post.get!(3_141_592_653)
# Create posts - seq and id are auto-generated
post = MyApp.Post.create!(%{title: "Hello World"})
# => %MyApp.Post{seq: 1, id: 3_141_592_653, title: "Hello World"}

post2 = MyApp.Post.create!(%{title: "Second Post"})
# => %MyApp.Post{seq: 2, id: 2_718_281_828, title: "Second Post"}

# Query by encrypted id
MyApp.Post.get!(3_141_592_653)
Installation
mix igniter.install ash_feistel_cipher
mix ash.codegen create_post
mix igniter.install ash_feistel_cipher
mix ash.codegen create_post
πŸ“¦ Hex: https://hex.pm/packages/ash_feistel_cipher
πŸ™ GitHub: https://github.com/devall-org/ash_feistel_cipher Built on top of feistel_cipher library.
1 Reply
jart
jartβ€’2mo ago
Okay wow that’s super cool!

Did you find this page helpful?