Lite::Uxid
Lite::Uxid is a library for generating or obfuscating Id's based on different patterns. It's very useful to hide the number of resources in your database and protect against enumeration attacks.
NOTE: If you are coming from ActiveUxid
, please read the port section.
Installation
Add this line to your application's Gemfile:
gem 'lite-uxid'
And then execute:
$ bundle
Or install it yourself as:
$ gem install lite-uxid
Table of Contents
Configuration
rails g lite:uxid:install
will generate the following file:
../config/initalizers/lite_uxid.rb
Lite::Uxid.configure do |config|
config.encoding_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
config.encoding_length = 26
config.encoding_salt = 1_369_136
end
Hashid
Hashid's are reversible and is the most performant generator.
Lite::Uxid::Hashid.encode(10) #=> 'q5D8inm0'
Lite::Uxid::Hashid.decode('q5D8inm0') #=> 10
NanoID
NanoID are not reversible and are the second fastest ID generator but while unlikely can produce collisions.
Lite::Uxid::Nanoid.encode #=> '0bmHjB5Gx8FTBqJekX6dS6XIXf'
ULID
ULID are not reversible but provide information outside of just randomness.
Lite::Uxid::Ulid.encode #=> '1mqfg9qa96s8s5f02o1ucf8lcc'
ActiveRecord
Table
Add the following attribute to all corresponding tables.
# omitted
t.binary :uxid, index: { unique: true }
# omitted
Setup
All nanoid
and uxid
attributes will be automatically generated and applied when the record is created.
class User < ActiveRecord::Base
include Lite::Uxid::Record::Hashid
# - or -
include Lite::Uxid::Record::Nanoid
# - or -
include Lite::Uxid::Record::Ulid
end
Usage
Using one of the mixins above provides a handy method to find records by uxid.
User.find_by_uxid('x123') #=> Find record by uxid
User.find_by_uxid!('x123') #=> Raises an ActiveRecord::RecordNotFound error if not found
# The following method is for Hashid based Uxid's.
user = User.new
user.uxid_to_id #=> Decodes the records uxid to id (only for Hashid based Id's)
Benchmarks
The classes ranked from fastest to slowest are Hashid
, Nanoid
, and Ulid
.
View how each compares by running the benchmarks.
Port
Lite::Uxid
is a compatible port of ActiveUxid.
Switching is as easy as renaming ActiveUxid
to Lite::Uxid
.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lite-uxid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Lite::Uxid project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.