views:

116

answers:

3

I'm generating a verification code to be used for account activation. You've probably seen this sort of thing before.

My question: if I were to generate this code with a complex formula like this:

md5(md5(time().'helloguys'.rand(0,9999)));

Is it really any better than generating just a random string of 32 characters and numbers like gj3dI3OGwo5Enf...?

+5  A: 

No, using the hash is not better. It would be more secure (less predictable) to pick 32 random characters. (Digits are characters.) Use a good ("cryptographic") random number generator, with a good seed (some bytes from /dev/random). Don't use time as a seed.

erickson
I was planning to generate the 32 random characters with a for loop. What's wrong with this method? And wouldn't it be fine for a normal site for account verification purposes. (I mean even if I were twitter or something, wouldn't that still be fine for the purpose at hand)
dave
+1 totally agree.
Rook
Yes, I think picking the letters one at a time in a `for` loop would be fine, as long as the RNG is good it won't matter. This would be good for any site (might want a few more characters).
erickson
Also, avoid the use of characters that look similar. Don't include 0, O, 1, l or I as people trying to type the code in will make mistakes.
Andrew Kennan
@Andrew: No one should be typing this code... they'll either click the link, or if they can't, they ought to be copying and pasting. If they can't figure that out.. I'm surprised they made it into their email account.
Mark
+2  A: 

First of all you should never use rand() for security, period. mt_rand() is a lot better. Also the use of a message digest does not increase the distribution of the random value, it obscures how the value was generated. I agree with erickson, use a random number generator to choose characters to build the string.

Also base16 which is produced by md5 has a terrible entropy/size ratio when compared to base64 or base256 (full byte).

Rook
+1  A: 

Agree with erickson, just may advise you to use

pwgen -1 -s

command on *nix which will the job muich better of any procedure you may invent.

If you want to generate some string programmatically you may take a look at

<?php    
$better_token = md5(uniqid(rand(),1));
?>

this gives very good level of randomness and prior to collisions.

If you need even higher level of security you may consider to generate random sequences on http://www.random.org/

Igor