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.
Add this line to your application's Gemfile:
gem 'lite-uxid'
And then execute:
$ bundle
Or install it yourself as:
$ gem install lite-uxid
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 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
config.hashid_salt = 1_369_136
config.hashid_size = 16
config.nanoid_charset = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
config.nanoid_size = 21
config.ulid_charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
config.ulid_size = 26
end
coder = Lite::Uxid::Hashid.new(10, size: 12)
coder.encode #=> '67wGI0'
Lite::Uxid::Hashid.decode('67wGI0', size: 12) #=> 10
Lite::Uxid::Hashid.encode(10) #=> '1zWr1m0'
Lite::Uxid::Hashid.decode('1zWr1m0') #=> 10
Lite::Uxid::Nanoid.encode #=> 'sMuNUa3Cegn6r5GRQ4Ij2'
Lite::Uxid::Ulid.encode #=> '01GJAY9KGR539EZF4QWYEJGSN7'
Implements v4 of the specification. More information
Lite::Uxid::Uuid.encode #=> '4376a67e-1189-44b3-a599-7f7566bf105b'
Local options can be passed to override global options.
Lite::Uxid::Ulid.encode(chars: 'abc123', size: 12) #=> 'a3b12c12c3ca'
Table
Add the following attribute to all corresponding tables.
t.string :uxid, index: { unique: true }
Setup
uxid
attribute will be automatically generated and applied when the record is created.
class User < ActiveRecord::Base
include Lite::Uxid::Record::Hashid
end
class User < ActiveRecord::Base
include Lite::Uxid::Record::Nanoid
end
class User < ActiveRecord::Base
include Lite::Uxid::Record::Ulid
end
class User < ActiveRecord::Base
include Lite::Uxid::Record::Uuid
end
Usage
Using one of the mixins above provides a handy method to find records by uxid.
user = User.new
user.id_to_uxid #=> Encodes the records id to uxid
user.uxid_to_id #=> Decodes the records uxid to id
User.find_by_uxid('x123') #=> Find record by uxid
User.find_by_uxid!('x123') #=> Raises an ActiveRecord::RecordNotFound error if not found
The classes ranked from fastest to slowest are UUID
, Hashid
, Nanoid
, and Ulid
.
View how each compares by running the benchmarks.
Learn more about alternative functions and more advance hashing setups: hashids.org
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.
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.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Lite::Uxid project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.