• Stars
    star
    52
  • Rank 542,088 (Top 11 %)
  • Language
    Crystal
  • License
    MIT License
  • Created almost 8 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Crystal shard 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.

Hashids

Build Status Shard version

A small Crystal shard 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.

https://hashids.org/crystal/

What is it?

hashids (Hash IDs) creates short, unique, decodable hashes from unsigned integers.

See https://hashids.org for more information.

The is a port of Ruby's implementation. The tests were adopted from the original JavaScript implementation.

Installation

Add this to your application's shard.yml:

dependencies:
  hashids:
    github: splattael/hashids.cr
    version: 0.3.0

Usage

require "hashids"

hashids = Hashids.new
hashids.encode([1]) # => "jR"

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(salt: "this is my salt")
hash = hashids.encode([12345]) # => NkK9

Decoding

Notice during decoding, same salt value is used:

hashids = Hashids.new(salt: "this is my salt")
numbers = hashids.decode("NkK9") # => [12345]

Decoding with different salt

Decoding will not work if salt is changed:

hashids = Hashids.new(salt: "this is my pepper")
numbers = hashids.decode("NkK9") # => []

Encoding several numbers

hashids = Hashids.new(salt: "this is my salt")
hash = hashids.encode([683, 94108, 123, 5]) # => aBMswoO2UB3Sj

Decoding is done the same way

hashids = Hashids.new(salt: "this is my salt")
numbers = hashids.decode("aBMswoO2UB3Sj") # => [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(salt: "this is my salt", min_length: 8)
hash = hashids.encode([1]) # => gB0NV05e

Decoding with minimum hash length

hashids = Hashids.new(salt: "this is my salt", min_length: 8)
numbers = hashids.decode("gB0NV05e") # => [1]

Specifying custom hash alphabet

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

hashids = Hashids.new(salt: "this is my salt", alphabet: "abcdefghijkABCDEFGHIJK12345")
hash = hashids.encode([1, 2, 3, 4, 5]) # => 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

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

hashids = Hashids.new(salt: "this is my salt")
hash = hashids.encode([5, 5, 5, 5]) # => 1Wc8cwcE

Same with incremented numbers:

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

Incrementing number ids:

hashids = Hashids.new(salt: "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(salt: "this is my salt")
hash = hashids.encode_hex("DEADBEEF") # => kRNrpKlJ

Decoding to a HEX string

hashids = Hashids.new(salt: "this is my salt")
hex_str = hashids.decode_hex("kRNrpKlJ") # => "deadbeef"

Development

make spec

Contributing

  1. Fork it ( https://github.com/splattael/hashids/fork )
  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 a new Pull Request

Release

  • Make sure make spec is green
  • Commit all changes
  • Bump version in src/hashids/version.cr
  • Adjust version in README.md and shard.yml
  • Add entries in CHANGELOG.md
  • make release

Contributors

More Repositories

1

libnotify

Ruby bindings for libnotify using FFI.
Ruby
135
star
2

minitest-around

Around block for minitest.
Gherkin
41
star
3

hotch

Profile helper
Ruby
28
star
4

libnotify.cr

Libnotify for Crystal
Crystal
26
star
5

dind-example

Docker in docker example
9
star
6

docker-phpmyadmin

Dockerized phpMyAdmin on Alpine
PHP
7
star
7

bigbrother

Server overseer.
Crystal
6
star
8

dotfiles

Personal dotfiles
Python
6
star
9

chrome-feedly-tabs

Open Feedly articles in new tabs.
JavaScript
6
star
10

erl_nif.cr

WIP: Erlang NIF in Crystal
Crystal
6
star
11

ruml

Ruby mailing list software
Ruby
5
star
12

phpmyadmin-backup

Backup DB from phpmyadmin via mechanize
Ruby
5
star
13

crystal-ctags

Support for the Crystal language in ctags
4
star
14

shoutcast_api

Simple API for shoutcast.com
Ruby
4
star
15

ruby-live-for-speed

A Ruby parser for racing game Live for Speed
Ruby
3
star
16

minitest-libnotify

Test notifier for minitest via libnotify.
Ruby
3
star
17

structr

Structure plain text with regexp.
Ruby
2
star
18

firefox-feedly-tabs

Open Feedly articles in new tabs. For Firefox.
JavaScript
2
star
19

docker-firefox

Dockerized Firefox
Makefile
2
star
20

services

Ruby
2
star
21

leetplate

Number plate generator (with l337 support)
JavaScript
2
star
22

valuedate

Validates a schema
Ruby
2
star
23

elm-connect-four

Connect four in elm
Elm
1
star
24

riot_notifier

Notifier for testing framework riot.
Ruby
1
star
25

rouge-lexers-crystal

[DEPRECATED] Rouge's lexer for crystal
Ruby
1
star
26

smstrade_de

Gem for sending SMS via smstrade.de Gateway.
Ruby
1
star
27

dns_checker

Compares domains with root nameserver and your nameserver.
Ruby
1
star
28

optcarrot.cr

Ruby
1
star
29

sloono

Simple SMS API for sloono.de
Ruby
1
star
30

meta_code_commenter

Comments your meta code.
Ruby
1
star