views:

242

answers:

5

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.

A: 

Use Microtime....

Pentium10
A: 

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

TiuTalk
`SHA1(MD5(...))` is useless; if you were to go down this route just use SHA1.
Alex Barrett
A: 

java.security.messageDigest()?

Trent Davies
WHich is another way to run MD5 or SHA-1, no?
bmargulies
A: 

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).

sth
A: 

So I have finally settled for this -

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

Thank you all for the response.

Baishampayan Ghose
You would be better off removing the date prefix and adding another 14 characters to the sha substring - each character of SHA256 output adds substantially more entropy than a character of datetime.
Nick Johnson