I'm pretty sure there is a number of options to achieve this, so I'm asking here. I often need to generate small unique tokens (like for invitation codes or unimportant authentication token).
In the past I've been using Digest::SHA1.hexdigest(some string) which gives us something like "d2b7882fa08bb6de8dca62f16298683bfe4f0738". It's not very user friendly though, and definitely not small.
More recently I've been using ActiveSupport::SecureRandom.base64(6) which generates "Xst7JGF6" or "zXxWY/Y/", but sometimes it contains non-url friendly characters like "/".
What do you use ? Is there a specific gem to achieve this in a robust fashion (= making collisions unlikely, and specify the characters to be used, like mixture of numbers and characters for instance ?)
On Dec 29, 4:44 am, Thibaut Barrère <thibaut.barr...@gmail.com> wrote:
> Hi,
> I'm pretty sure there is a number of options to achieve this, so I'm > asking here. I often need to generate small unique tokens (like for > invitation codes or unimportant authentication token).
> In the past I've been using Digest::SHA1.hexdigest(some string) which > gives us something like "d2b7882fa08bb6de8dca62f16298683bfe4f0738". > It's not very user friendly though, and definitely not small.
I've mostly just used a substring on SHA1 - hexdigest[8...16] is small enough to be usable, but still pseudo-random enough for temporary tokens to not be guessable.
> More recently I've been using ActiveSupport::SecureRandom.base64(6) > which generates "Xst7JGF6" or "zXxWY/Y/", but sometimes it contains > non-url friendly characters like "/".
Base64 is A-Za-z0-9/-. Gsub / for _ and you're pretty url friendly.
> What do you use ? Is there a specific gem to achieve this in a robust > fashion (= making collisions unlikely, and specify the characters to > be used, like mixture of numbers and characters for instance ?)
On Dec 29, 2008, at 01:44 , Thibaut Barrère wrote:
> In the past I've been using Digest::SHA1.hexdigest(some string) which > gives us something like "d2b7882fa08bb6de8dca62f16298683bfe4f0738". > It's not very user friendly though, and definitely not small.
I personally think the following is really cute:
> words = File.read("/usr/share/dict/words").split > max = words.size > puts "#{words[rand(max)]}-#{words[rand(max)]}"
and can generate 55194924096 different very readable combinations. I'll leave it to you to actually make them unique rather than random picks.
Eric pointed me to "bubble babble" which ships in digest and makes the hash values more readable. It converts your "Xst7JGF6" into "xikal- fytif-ladog-locef-kixox".
> I'm pretty sure there is a number of options to achieve this, so I'm > asking here. I often need to generate small unique tokens (like for > invitation codes or unimportant authentication token).
> In the past I've been using Digest::SHA1.hexdigest(some string) which > gives us something like "d2b7882fa08bb6de8dca62f16298683bfe4f0738". > It's not very user friendly though, and definitely not small.
Hi Thibaut,
I have this rufus-mnemo that turns integers into "user friendly" strings.