Last active
March 2, 2024 12:17
-
-
Save abicky/438bd177d52bba72ef7cd67e59579437 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Default alphabet length: 62
Alphabet: xaX6zvqDORgL9NPw42AnlmWk8JrG1o0bQBEVpdKe53yM
Alphabet length: 44
Seps: iIfHhcUSCFustT
Guards: 7ZYj