Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lite-uxid

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lite-uxid

  • 2.0.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Lite::Uxid

Gem Version Build Status

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|
  # HashID
  config.hashid_charset = Lite::Uxid::ALPHANUMERIC
  config.hashid_salt = 1_369_136

  # NanoID
  config.nanoid_charset = Lite::Uxid::WEB_SAFE
  config.nanoid_size = 21

  # ObfuscatedID
  config.obfuscateid_spin = 0

  # ULID
  config.ulid_charset = Lite::Uxid::COCKFORDS_32
  config.ulid_size = 26

  # UUID
  config.uuid_version = 4
end

Usage

Instance call

coder = Lite::Uxid::Reversible::Hashid.new(10, size: 12)
coder.encode #=> '67wGI0'

Class call

Lite::Uxid::Reversible::Hashid.decode('67wGI0', size: 12) #=> 10

Options

Local options can be passed to override global options.

Lite::Uxid::Irreversible::Ulid.encode(charset: 'abc123', size: 12) #=> 'a3b12c12c3ca'

Passable options are:

{
  charset: 'string',  # Available for: hashid, nanoid, ulid
  salt:    'string',  # Available for: hashid
  size:    'integer', # Available for: nanoid, ulid
  spin:    'integer', # Available for: obfuscateid
  version: 'integer', # Available for: uuid
  prefix:  'string'   # Available for: hashid, nanoid, ulid, uuid
}

Reversible

HashID

Lite::Uxid::Reversible::Hashid.encode(10)             #=> '7pau2oXSklq0'
Lite::Uxid::Reversible::Hashid.decode('7pau2oXSklq0') #=> 10

NanoID

Lite::Uxid::Irreversible::Nanoid.encode #=> 'sMuNUa3Cegn6r5GRQ4Ij2'

Irreversible

ObfuscateID

Lite::Uxid::Reversible::Obfuscateid.encode(10)         #=> 2056964183
Lite::Uxid::Reversible::Obfuscateid.decode(2056964183) #=> 10

ULID

Lite::Uxid::Irreversible::Ulid.encode #=> '01GJAY9KGR539EZF4QWYEJGSN7'

UUID

Implements v4 and v7 of the specification.

Lite::Uxid::Irreversible::Uuid.encode #=> '4376a67e-1189-44b3-a599-7f7566bf105b'

ActiveRecord

Table

Add the following attribute to all corresponding tables.

# NOTE: null: true has to be set for HashID's
# since an ID must exist before it gets created.
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
  # Pick one:
  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 #=> 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

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.

FAQs

Package last updated on 24 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc