encoded_id
lets you encode numerical or hex IDs into obfuscated strings that can be used in URLs.
encoded_id-rails
is a Rails integration that provides additional features for using encoded_id
with ActiveRecord models.
👉 Full documentation available at encoded-id.onrender.com
coder = ::EncodedId::ReversibleId.new(salt: "my-salt")
coder.encode(123)
# => "p5w9-z27j"
# The encoded strings are reversible
coder.decode("p5w9-z27j")
# => [123]
# Supports encoding multiple IDs at once
coder.encode([78, 45])
# => "z2j7-0dmw"
# Can also be used with ActiveRecord models
class User < ApplicationRecord
include EncodedId::Rails::Model
# Optional slug for the encoded ID
def name_for_encoded_id_slug
full_name
end
end
# Find by encoded ID
user = User.find_by_encoded_id("p5w9-z27j") # => #<User id: 78>
user.encoded_id # => "user_p5w9-z27j"
user.slugged_encoded_id # => "bob-smith--user_p5w9-z27j"
- 🔄 Reversible - Encoded IDs can be decoded back to the original values
- 👥 Multiple IDs - Encode multiple numeric IDs in one string
- 🚀 Choose your encoding - Supports
Hashids
andSqids
out of the box, or use your own custom encoder - 👓 Human-readable - Character grouping & character mappings of easily confused characters for better readability
- 🔡 Custom alphabets - Use your preferred character set, or a provided default
- 🚗 Performance - Uses an optimized
Hashids
encoder (compared tohashids
gem) for better performance and less memory usage, and have pushed performance improvements toSqids
as well - 🤬 Profanity blocking - Built-in word blocklist support and optional default lists
- 🏷️ ActiveRecord integration - Use with ActiveRecord models
- 🔑 Per-model salt - Use a custom salt for encoding per model
- 💅 Slugged IDs - URL-friendly slugs like
my-product--p5w9-z27j
- 🔖 Annotated IDs - Model type indicators like
user_p5w9-z27j
- 🔍 Finder methods - Find records using encoded IDs
- 🛣️ URL params -
to_param
with encoded IDs - 🔒 Safe defaults: Limits on encoded ID lengths to prevent CPU and memory-intensive encode/decodes eg when used in URLs
- 💾 Persistence - Optional database persistence for efficient lookups
# Add to Gemfile
bundle add encoded_id
# Or install directly
gem install encoded_id
See the EncodedId API documentation for more details.
# Add to Gemfile
bundle add encoded_id-rails
# Then run the generator
rails g encoded_id:rails:install
See the Rails Integration documentation for more details.
Encoded IDs are not secure. They are meant to provide obfuscation, not encryption. Do not use them as a security mechanism.
For a detailed comparison, see the Compared to Other Gems documentation page.
Visit encoded-id.onrender.com for comprehensive documentation including:
After checking out the repo, run bin/setup
to install dependencies. Run bundle exec rake test
to run the tests.
Run benchmarks with bin/benchmark <type>
where type is one of: ips
, memory
, comparison
, profile
, flamegraph
, or stress_decode
.
Run bundle exec rake website:build
to build or bundle exec rake website:serve
to preview locally.
Bug reports and pull requests are welcome on GitHub at https://github.com/stevegeek/encoded_id.
The gem is available as open source under the terms of the MIT License.