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.
By default, it implements websafe variants of each type.
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 in your application root:
config/initalizers/lite_uxid.rb
Lite::Uxid.configure do |config|
config.hashid_charset = Lite::Uxid::ALPHANUMERIC
config.hashid_salt = 1_369_136
config.nanoid_charset = Lite::Uxid::WEB_SAFE
config.nanoid_size = 21
config.obfuscateid_spin = 0
config.ulid_charset = Lite::Uxid::COCKFORDS_32
config.ulid_size = 26
config.uuid_version = 4
end
Usage
Instance call
coder = Lite::Uxid::Reversible::Hashid.new(10, size: 12)
coder.encode
Class call
Lite::Uxid::Reversible::Hashid.decode('67wGI0', size: 12)
Options
Local options can be passed to override global options.
Lite::Uxid::Irreversible::Ulid.encode(charset: 'abc123', size: 12)
Passable options are:
{
charset: 'string',
salt: 'string',
size: 'integer',
spin: 'integer',
version: 'integer',
prefix: 'string'
}
Reversible
HashID
Lite::Uxid::Reversible::Hashid.encode(10)
Lite::Uxid::Reversible::Hashid.decode('7pau2oXSklq0')
NanoID
Lite::Uxid::Irreversible::Nanoid.encode
Irreversible
ObfuscateID
Lite::Uxid::Reversible::Obfuscateid.encode(10)
Lite::Uxid::Reversible::Obfuscateid.decode(2056964183)
ULID
Lite::Uxid::Irreversible::Ulid.encode
UUID
Implements v4
and v7
of the specification.
Lite::Uxid::Irreversible::Uuid.encode
ActiveRecord
Table
Add the following attribute to all corresponding tables.
t.string :uxid, null: false, index: { unique: true }
If using UUID or ULID and your database supports it:
t.uuid :uxid, null: false, index: { unique: true }
Setup
uxid
attribute will be automatically generated and applied when the record is created.
Mixin
class User < ActiveRecord::Base
include Lite::Uxid::Record::Hashid
include Lite::Uxid::Record::Nanoid
include Lite::Uxid::Record::Obfuscateid
include Lite::Uxid::Record::Ulid
include Lite::Uxid::Record::Uuid
end
HashID, NanoID, ULID, and UUID modules allow prefixing via the uxid_prefix
method.
class User < ActiveRecord::Base
include Lite::Uxid::Record::Hashid
def uxid_prefix
"usr_"
end
end
Using the hashid
and nanoid
above provide handy methods to find records by uxid.
user = User.new
user.id_to_uxid
user.uxid_to_id
User.find_by_uxid('x123')
User.find_by_uxid!('x123')
Benchmarks
The classes ranked from fastest to slowest are UUID
, HashID
, NanoID
, ULID
, and ObfuscateID
. Here are the latest results:
Calculating -------------------------------------
Hashid 135.993k (± 2.9%) i/s (7.35 μs/i) - 681.588k in 5.016413s
Obfuscateid 30.702k (± 2.2%) i/s (32.57 μs/i) - 155.907k in 5.080592s
NanoID 99.327k (± 1.5%) i/s (10.07 μs/i) - 504.135k in 5.076630s
ULID 82.211k (± 2.3%) i/s (12.16 μs/i) - 418.455k in 5.092823s
UUID v4 237.629k (± 6.8%) i/s (4.21 μs/i) - 1.190M in 5.040477s
UUID v7 234.956k (±13.8%) i/s (4.26 μs/i) - 1.153M in 5.051057s
Comparison:
UUID v4: 237629.0 i/s
UUID v7: 234955.9 i/s - same-ish: difference falls within error
Hashid: 135993.5 i/s - 1.75x slower
NanoID: 99327.3 i/s - 2.39x slower
ULID: 82210.7 i/s - 2.89x slower
Obfuscateid: 30702.0 i/s - 7.74x slower
View how each compares by running the benchmarks.
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/drexed/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.