peterhellberg / hashids.rb

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.
http://www.hashids.org/ruby/
MIT License
977 stars 46 forks source link

MIN_ALPHABET_LENGTH is incorrect #4

Closed tzvetkoff closed 9 years ago

tzvetkoff commented 9 years ago

The following piece of code:

hashids = Hashids.new('this is my salt', 0, '0123456789abcdef')
id = hashids.encode(18446744073709551615)
numbers = hashids.decode(id)

raises a Hashids::AlphabetError with message Alphabet must contain at least 16 unique characters.

The validate_alphabet call needs to be moved after @alphabet assignment.

peterhellberg commented 9 years ago

Yes, the reason for this is that the characters cfhistuCFHISTU is removed from any alphabet before it is used. (Turns your example into: 0123456789abde)

The error message is a bit misleading.

I’ll update the code when I have verified the expected behaviour with @ivanakimov

peterhellberg commented 9 years ago

I have now made the change (d48b8b650) and pushed 1.0.1 to RubyGems.

Thank you for reporting the issue.