Algorithm to generate unique (possibly auto incremented) ids

此生再无相见时 提交于 2019-12-14 03:42:53

问题


I need to generate unique ids for my application and I am looking for suitable algorithms. I would prefer something like this --

YYYY + MM + DD + HH + MM + SS + <random salt> + <something derived from the preceding values>

F.ex. -

20100128184544ewbhk4h3b45fdg544

I was thinking about using SHA-256 or something but the resultant string should not be too long. I could use UUID but again, they are too long and they are guaranteed to be unique on only one machine.

I would welcome suggestions, ideas. My programming language is Java.

Edit: The ids need not be cryptographically secure. I am looking at simpler hashing algos like the one by Dan Bernstein, etc.


回答1:


You could use that SHA-256 and then only take the first 10 bytes from the result (or however many you like, balancing length and uniqueness however you like).




回答2:


So I have finally settled for this -

d = YYYYMMDDHHMMSS
hash = d + sha256(d + random_salt)[:10]

Thank you all for the response.




回答3:


Try this:

java.security.messageDigest()



回答4:


I think if you use SHA1(MD5(YYYYMMDDHHMMSS + YourSystemName + ClientName)) u'll be fine with 40 chars.. ;)



来源:https://stackoverflow.com/questions/2154722/algorithm-to-generate-unique-possibly-auto-incremented-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!