Is there some ready-made algorithm to generate invitation code by ruby? I can think at first MD5, but its output is too long for 32, so I want the output length is less than 16.
Thank you in advance.
Is there some ready-made algorithm to generate invitation code by ruby? I can think at first MD5, but its output is too long for 32, so I want the output length is less than 16.
Thank you in advance.
Assuming you are storing the code (otherwise, what's the point?) just make a random string, and check to see if it exists before saving, and try a new string if it does. No major algorithm needed.
SecureRandom.uuid
This will produce out put like the following.
2.1.0 :005 > SecureRandom.uuid
=> "b2a8ed4c-f71f-4c7d-a0fb-a66de58d37cc"
2.1.0 :006 >
Short enough and extremely unlikely to cause a collision.
How "secure" do you want your invitation code? You could generate an MD5 hash and simply take the first or last 16 characters, or any 16-character combination from the hash code (e.g. every other position in the hash). That should be good enough for an invitation-code.