views:

165

answers:

3

What's the best way to generate a cryptographically secure 32 bytes salt in PHP, without depending on libraries seldom included in typical PHP installations?

After some googling I discovered that mt_rand is not considered secure enough, but I haven't found a suggestion for a replacement. One article suggested reading from /dev/random but not only this won't work on windows; it is also very slow.

I want a reasonable balance between security and speed (ie, it shouldn't take 20 seconds to generate 512 bytes, like /dev/random usually does)

A: 

i think microtime() is secure enough

Col. Shrapnel
Not really. Not only it's very very easy to predict (it's the current time), I also can't get 32 bytes out of it.
qster
@qster you can use it as random number generator. And get any number of bytes you want. And it is not the current time, you messed it up with time(). Microseconds do change pretty fast.And it's only salt, you don't need too much security on salt.
Col. Shrapnel
+3  A: 

You might want to take a look at the documentation (and comments) for mcrypt_create_iv().

Amber
+1  A: 

uniqid() should be fine for this purpose.

Crozin
I use uniqid() with some fast computable seed value (like the suggested microtime), too. Haven't done a cryptographic analysis, but seems to fit the bill.
Boldewyn