All Projects → peterhellberg → Hashids.rb

peterhellberg / Hashids.rb

Licence: mit
A small Ruby gem to generate YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user.

Programming Languages

ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Hashids.rb

Hashid Rails
Use Hashids (http://hashids.org/ruby/) in your Rails app ActiveRecord models.
Stars: ✭ 225 (-73.28%)
Mutual labels:  hashids
id-mask
IDMask is a Java library for masking internal ids (e.g. from your DB) when they need to be published to hide their actual value and to prevent forging. It has support optional randomisation has a wide support for various Java types including long, UUID and BigInteger. This library bases its security on strong cryptographic primitives.
Stars: ✭ 39 (-95.37%)
Mutual labels:  hashids
Hashids.js
A small JavaScript library to generate YouTube-like ids from numbers.
Stars: ✭ 3,525 (+318.65%)
Mutual labels:  hashids
django-hashids
Non-intrusive hashids library for Django
Stars: ✭ 26 (-96.91%)
Mutual labels:  hashids
eloquent-hashids
Automatically generate and persist Hashids for newly created Eloquent models.
Stars: ✭ 17 (-97.98%)
Mutual labels:  hashids
laravel-hashid
HashId Implementation on Laravel Eloquent ORM
Stars: ✭ 23 (-97.27%)
Mutual labels:  hashids
Django Rest Framework Serializer Extensions
Extensions to help DRY up Django Rest Framework serializers
Stars: ✭ 180 (-78.62%)
Mutual labels:  hashids
Hashids
A small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user.
Stars: ✭ 4,596 (+445.84%)
Mutual labels:  hashids
objection-hashid
Objection plugin to automatically obfuscate model ids using hashids!
Stars: ✭ 13 (-98.46%)
Mutual labels:  hashids
Django Hashid Field
Django Model Field that uses Hashids to obscure the value
Stars: ✭ 256 (-69.6%)
Mutual labels:  hashids
hashids.sql
PL/pgSQL implementation of hashids library
Stars: ✭ 40 (-95.25%)
Mutual labels:  hashids
hashids.pm
Hashids, ported for Perl
Stars: ✭ 15 (-98.22%)
Mutual labels:  hashids
idy
👓 An ID obfuscator for ActiveRecord
Stars: ✭ 15 (-98.22%)
Mutual labels:  hashids
laravel-hashids
Laravel package for Hashids
Stars: ✭ 52 (-93.82%)
Mutual labels:  hashids
Laravel Hashid
Obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.
Stars: ✭ 354 (-57.96%)
Mutual labels:  hashids
Eloquent Hashids
On-the-fly hashids for Laravel Eloquent models. (🍰 Easy & ⚡ Fast)
Stars: ✭ 196 (-76.72%)
Mutual labels:  hashids
harsh
Hashids implementation in Rust
Stars: ✭ 48 (-94.3%)
Mutual labels:  hashids
Go Hashids
Go (golang) implementation of http://www.hashids.org
Stars: ✭ 830 (-1.43%)
Mutual labels:  hashids
Hashids.net
A small .NET package to generate YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user.
Stars: ✭ 470 (-44.18%)
Mutual labels:  hashids
bashids
Pure Bash implementation of the hashid algorithm from http://hashids.org/
Stars: ✭ 38 (-95.49%)
Mutual labels:  hashids

Hashids

A small Ruby gem to generate YouTube-like ids from one or many numbers. Use hashids when you do not want to expose your database ids to the user.

http://hashids.org/ruby/

Build Status (2.6.2, 2.5.5, 2.4.5, 2.3.8, jruby-9.2.6.0)

What is it?

hashids (Hash ID's) creates short, unique, decodable hashes from unsigned integers.

(NOTE: This is NOT a true cryptographic hash, since it is reversible)

It was designed for websites to use in URL shortening, tracking stuff, or making pages private (or at least unguessable).

This algorithm tries to satisfy the following requirements:

  1. Hashes must be unique and decodable.
  2. They should be able to contain more than one integer (so you can use them in complex or clustered systems).
  3. You should be able to specify minimum hash length.
  4. Hashes should not contain basic English curse words (since they are meant to appear in public places - like the URL).

Instead of showing items as 1, 2, or 3, you could show them as jR, k5, and l5. You don't have to store these hashes in the database, but can encode + decode on the fly.

All integers need to be greater than or equal to zero.

Installation

Add this line to your application's Gemfile:

gem 'hashids'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hashids

Usage

Encoding one number

You can pass a unique salt value so your hashes differ from everyone else's. I use this is my salt as an example.

hashids = Hashids.new("this is my salt")
hash = hashids.encode(12345)

hash is now going to be:

NkK9

Decoding

Notice during decoding, same salt value is used:

hashids = Hashids.new("this is my salt")
numbers = hashids.decode("NkK9")

numbers is now going to be:

[ 12345 ]

Decoding with different salt

Decoding will not work if salt is changed:

hashids = Hashids.new("this is my pepper")
numbers = hashids.decode("NkK9")

numbers is now going to be:

[]

Encoding several numbers

hashids = Hashids.new("this is my salt")
hash = hashids.encode(683, 94108, 123, 5)

hash is now going to be:

aBMswoO2UB3Sj

Decoding is done the same way

hashids = Hashids.new("this is my salt")
numbers = hashids.decode("aBMswoO2UB3Sj")

numbers is now going to be:

[ 683, 94108, 123, 5 ]

Encoding and specifying minimum hash length

Here we encode integer 1, and set the minimum hash length to 8 (by default it's 0 -- meaning hashes will be the shortest possible length).

hashids = Hashids.new("this is my salt", 8)
hash = hashids.encode(1)

hash is now going to be:

gB0NV05e

Decoding with minimum hash length

hashids = Hashids.new("this is my salt", 8)
numbers = hashids.decode("gB0NV05e")

numbers is now going to be:

[ 1 ]

Specifying custom hash alphabet

Here we set the alphabet to consist of: "abcdefghijkABCDEFGHIJK12345"

hashids = Hashids.new("this is my salt", 0, "abcdefghijkABCDEFGHIJK12345")
hash = hashids.encode(1, 2, 3, 4, 5)

hash is now going to be:

dEc4iEHeF3

Randomness

The primary purpose of hashids is to obfuscate ids. It's not meant or tested to be used for security purposes or compression. Having said that, this algorithm does try to make these hashes unguessable and unpredictable:

Repeating numbers

hashids = Hashids.new("this is my salt")
hash = hashids.encode(5, 5, 5, 5)

You don't see any repeating patterns that might show there's 4 identical numbers in the hash:

1Wc8cwcE

Same with incremented numbers:

hashids = Hashids.new("this is my salt")
hash = hashids.encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

hash is now going to be:

kRHnurhptKcjIDTWC3sx

Incrementing number ids:

hashids = Hashids.new("this is my salt")

hashids.encode 1 #=> NV
hashids.encode 2 #=> 6m
hashids.encode 3 #=> yD
hashids.encode 4 #=> 2l
hashids.encode 5 #=> rD

Encoding using a HEX string

hashids = Hashids.new("this is my salt")
hash = hashids.encode_hex('DEADBEEF')

hash is now going to be:

kRNrpKlJ

Decoding to a HEX string

hashids = Hashids.new("this is my salt")
hex_str = hashids.decode_hex("kRNrpKlJ")

hex_str is now going to be:

DEADBEEF

Changelog

1.0.5

  • Improve shuffle performance
  • Update rubies used by Travis-CI

1.0.4

  • Improved encode/decode performance

1.0.3

  • Support for Ruby 2.4.0

1.0.2

  • Handle invalid input by raising InputError

1.0.1

  • Final alphabet length can now be shorter than the minimum alphabet length
  • validate_alphabet now run before setting up seps & guards

1.0.0

  • Public functions renamed to be more appropriate:
  • encrypt changed to encode
  • encrypt_hex changed to encode_hex
  • decrypt changed to decode
  • decrypt_hex changed to decode_hex

0.3.0

  • Bumped the version number since hashids.rb now support the new algorithm
  • Support for encrypt_hex and decrypt_hex

0.0.3

  • Default salt (Allows for Hashids.new.encrypt(91) #=> "kBy")
  • Further tweaking of the private methods (tr/delete over gsub, scan over split)

0.0.2

  • Minitest required if RUBY_VERSION < 1.9.3
  • Using scan over split where appropriate

0.0.1

Contact

Follow me @peterhellberg

Or http://c7.se/

License

MIT License. See the LICENSE.txt file.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].