开发者

Is there some ready-made algorithm to generate invitation code by ruby

Is there some ready-made algorithm to generate invitation code by ruby? I can think at first MD5, 开发者_StackOverflow社区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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜