Generate a random string (email verification etc), need hash

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Re: Generate a random string (email verification etc), need hash

Post by batfastad »

Perfect, thanks pickle :lol:
Thought I better get that confirmed before I started hammering through this project

Cheers, B
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Generate a random string (email verification etc), need hash

Post by kaisellgren »

microtime() is not an always incrementing value and it gives us a strength of 19.93-bits.

The two suggestions "hashing user's email" and "using microtime" are bad. batfastad's threat model here is hardly a collision. What he wants is to ensure that no one can predict the secret hex number he sent to the user's email. The best way would be to gather strong random data and encode it.
batfastad wrote:If I'm getting strong random from /dev/urandom (way stronger than uniqid(mt_rand()), then I shouldn't need to hash it.
Right?
Yes. You can just encode it with bin2hex(), for instance. Hashing here does not make any sense at all. It's only useful in situations like when you use mt_rand(), because otherwise you would leak the state of your random number generator. In case of /dev/urandom, the generator is clearly seeded with unpredictable data (with information that requires at least hardware and system access in order to be possible to predict, although still laborious) and thus you will not leak the state.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Generate a random string (email verification etc), need hash

Post by Apollo »

One reason I would still prefer something like sha1(time().uniqid(mt_rand(),true)) is that it's platform independent. The /dev/urandom method won't work on Windows machines.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Generate a random string (email verification etc), need hash

Post by kaisellgren »

That is right. I'm developing a PHP security library for these kinds of reasons. Fortunately, the library is pretty much platform independent. :)
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Re: Generate a random string (email verification etc), need hash

Post by batfastad »

Apollo wrote:One reason I would still prefer something like sha1(time().uniqid(mt_rand(),true)) is that it's platform independent. The /dev/urandom method won't work on Windows machines.
Yeah that's true. I've written a function which uses that if /dev/urandom can't be opened.
However our websites would never be hosted on Windows for variety of other reasons. Always best to do things cross-platform when possible though.

Thanks for the further info Kai ;)
Look forward to checking out a security library if you release it :lol:
Post Reply