Project

encoded_id

0.0
No release in over a year
Encode your numerical IDs (eg record primary keys) into obfuscated strings that can be used in URLs. The obfuscated strings are reversible, so you can decode them back into the original numerical IDs. Supports encoding multiple IDs at once, and generating IDs with custom alphabets and separators to make the IDs easier to read or share.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

~> 1.0
 Project Readme

EncodedId and EncodedId::Rails

Coverage RubyCritic

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

Quick Example

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"

Key Features

  • 🔄 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 and Sqids 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 to hashids gem) for better performance and less memory usage, and have pushed performance improvements to Sqids as well
  • 🤬 Profanity blocking - Built-in word blocklist support and optional default lists

Rails Integration Features

  • 🏷️ 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

Standalone Gem

# Add to Gemfile
bundle add encoded_id

# Or install directly
gem install encoded_id

See the EncodedId API documentation for more details.

Rails Integration Gem

# 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.

Security Note

Encoded IDs are not secure. They are meant to provide obfuscation, not encryption. Do not use them as a security mechanism.

Compare to Alternate Gems

For a detailed comparison, see the Compared to Other Gems documentation page.

Documentation

Visit encoded-id.onrender.com for comprehensive documentation including:

Development

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.

Documentation

Run bundle exec rake website:build to build or bundle exec rake website:serve to preview locally.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/stevegeek/encoded_id.

License

The gem is available as open source under the terms of the MIT License.