Skip to content

Instantly share code, notes, and snippets.

@abicky
Last active March 2, 2024 12:17
Show Gist options
  • Save abicky/438bd177d52bba72ef7cd67e59579437 to your computer and use it in GitHub Desktop.
Save abicky/438bd177d52bba72ef7cd67e59579437 to your computer and use it in GitHub Desktop.
# cf. https://html-preview.github.io/?url=https://github.com/hashids/hashids.github.io/blob/master/index.html
require "hashids"
puts "Default alphabet length: #{Hashids::DEFAULT_ALPHABET.length}"
hashids = Hashids.new("This is salt", 10)
puts <<MSG
Alphabet: #{hashids.alphabet}
Alphabet length: #{hashids.alphabet.length}
Seps: #{hashids.seps}
Guards: #{hashids.guards}
Number | ID | ID without padding or lottery | Lottery
-------|----|--------------------|---------
MSG
(1..8).each do |power|
base = hashids.alphabet.length ** power
[-1, 0].each do |delta|
n = base + delta
id = hashids.encode(n)
parts = id.tr(hashids.guards, " ").split(" ")
id_without_pad = parts.size == 1 ? parts[0] : parts[1]
lottery = id_without_pad.slice!(0)
puts [n, id, id_without_pad, lottery].join(" | ")
end
end
@abicky
Copy link
Author

abicky commented Mar 2, 2024

Default alphabet length: 62
Alphabet: xaX6zvqDORgL9NPw42AnlmWk8JrG1o0bQBEVpdKe53yM
Alphabet length: 44
Seps: iIfHhcUSCFustT
Guards: 7ZYj

Number ID ID without padding or lottery Lottery
43 oOE7MJZ8MN J M
44 5Ge7xnEYq6 nE x
1935 me9ZVxxjEq xx V
1936 8X7pyOOZQz yOO p
85183 8V7e000jkn 000 e
85184 BdZ5y666ZA y666 5
3748095 lRjDDDDDjz DDDD D
3748096 njOW9999j3 W9999 O
164916223 4Ykyyyyy7D yyyyy k
164916224 y78Dqqqqq7 Dqqqqq 8
7256313855 KjLkkkkkkY kkkkkk L
7256313856 Z9yOOOOOOZ yOOOOOO 9
319277809663 ZnvvvvvvvZ vvvvvvv n
319277809664 7lKOOOOOOO KOOOOOOO l
14048223625215 Ywkkkkkkkk kkkkkkkk w
14048223625216 4X99999999 X99999999 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment