views:

184

answers:

7

what is a way in php to make a random, variable length salt for use in hashing. let's say i want to make a 16-character long salt - how would i do it?

A: 

to generate a random string, try md5()

so you may: md5(time())

joetsuihk
and so this is appropriate for use with a hashing alg like sha256?
hatorade
@hatorade, an md5 collision doesn't matter, so sha256 won't help. However time() is not a random number, and thats why you got -1. Don't give people secuirty advice when you have no idea what you are doing.
Rook
what is an md5 collision...?
hatorade
@hatorade: An MD5 collision is when two values generate the same MD5 checksum.
Johannes Gorset
A: 

There are two prerequisites for a good salt: It must be long, and it must be random. There are many ways to accomplish this. You could use a combination of microtime and rand, for example, but you might go to even greater lengths to ensure that your salt is unique.

While the chance of a collision is neglible, keep in mind that hashing your salt with MD5 or other collision-prone algorithms will reduce the chance that your salt is unique for no reason.

EDIT: Substitute rand() for mt_rand(). As Michael noted, it's better than rand.

Johannes Gorset
@FRKT: do i need to put a salt for crypt()? the syntax is crypt(password, salt) where salt is optional but it seems to determine which variant of crypt is used and i don't know how much that affects security. http://php.net/manual/en/function.crypt.php
hatorade
rand() is not good. mt_rand() is better.
Rook
@hatorade: The manual says about the salt parameter: "An optional salt string to base the encryption on. If not provided, one will be randomly generated by PHP each time you call this function."
VolkerK
A: 

Not my answer, exactly, but having read the kerfuffle over your last question as well about MD5 vs. SHA, try this article, it could be what you're after.

Kevin Jones
+2  A: 

My solution:

function unique_md5() {
    mt_srand(microtime(true)*100000 + memory_get_usage(true));
    return md5(uniqid(mt_rand(), true));
}
XUE Can
+1 because its better than the guy that won.
Rook
if i understood why mt_rand > rand, that would be helpful
hatorade
It's faster and more random (php.net/mt_rand).
Johannes Gorset
A: 

you can find some good explanation over at nettuts.com check out this link

jd291
A: 

If the mcrypt extension is available you could simply use mcrypt_create_iv(size, source) to create a salt.

$iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
var_dump($iv);

Since each byte of the "string" can be in the range between 0-255 you need a binary-safe function to save/retrieve it.

VolkerK
A: 

depending on your OS, something like:

$fh=fopen('/dev/urandom','rb');
$salt=fgets($fh,16);
fclose($fh);

Do read up on the behaviour of random and urandom.

While others have correctly pointed out that there some issues with md5 and repeated hashing, for passwords (i.e. relatively short strings) brute force attacks take the same amount of time regardless of how sophisticated the hashing algorithm is.

C.

symcbean