views:

211

answers:

5

Hey i would like do have your input on this

I use this to generate unique salts to each of my users when they register (random letters and numbers). how big is the chance that salts will colide?

uniqid(mt_rand());

I then use md5 to hash salt, password and email(in that order) together as password and rehash when they log-in.

md5($salt . $password . $email);

How much safer than just md5 is this? Something i can improve?

CREATE TABLE IF NOT EXISTS `users` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(24) CHARACTER SET utf8 NOT NULL,
`password` varchar(32) CHARACTER SET utf8 NOT NULL,
`email` varchar(255) CHARACTER SET utf8 NOT NULL,
`salt` varchar(255) CHARACTER SET utf8 NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `username` (`username`),
 UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
A: 

They will never colide.

....maybe once in 10000000000000000.

powtac
That somehow doesn't seem mathematically correct. Can I see some statistics that support that number?
mrduclaw
am i the only one who read that number as binary? >.<
Pondidum
maybe? For 500000000 salts taken from the range 0-2^32, the chance of collision is more than 30%.
xtofl
+12  A: 

It does not matter if they collide. The purpose of the salt is that if you hash the same value twice but with different salts, the result will be different.If attacker aquires you databases of hashes, salt will renders ineffective attack with pre-calculated database of hashes of known passwords. The salt itself is not a secret and collisions of salts is not a problem.

Alex Reitbort
Salt collisions are a very minor problem in that they allow you to work in parallel towards brute-forcing the passwords which share the same salt. Personally, though, I do agree that this isn't something that you really need to worry about. The increased exposure it creates (given at least a two-character alphanumeric salt) is so small that your time is better spent increasing some other piece of the security architecture.
Dave Sherohman
+4  A: 

I wouldn't use the email address in the password hash. If a person changes their email address it would invalidate the hashed password and thus you'd have to have the user change their password every time they change their email address. I typically use a salt per user and a salt per application (fixed for all users). This way an attacker would need access to both your application and your user database to gain access.

$hashed = md5( $per_user_salt . $password . $app_salt );
tvanfosson
I wouldn't use MD5
Jacco
Neither would I but other answers had already mentioned that issue so I addressed a different one.
tvanfosson
+3  A: 

getrandmax seems to return a rather big number (2147483647), depending on your platform. The chance that you encounter any given N is hence 1/2147483647.

The chance that you don't encounter N is 1-1/2147483647.

So the chance that you don't encounter the first, secondly, thirdly, ... Pth N becomes the Pth power of (1-1/2147483647).

So the chance you do encounter one of P distributed salts is 1 - (chance you don't encounter any of P salts)

= 1 - (1-1/max)**P

This means a curve going steeply down from about a quarter of a gig salts. (a table from excel):

                        max
                          2,147,483,647
            P = number/salts        ( 1 - 1/max ) ^ P       collission chance
               16777216                    0                   1%
               33554432                    0                   2%
               67108864                    0                   3%
              134217728                    0                   6%
              268435456                    0                  12%
              536870912                    0                  22%
             1073741824                    0                  39%
             2147483648                    0                  63%
             4294967296                    0                  86%
             8589934592                    0                  98%
            17179869184                    0                 100%
xtofl
A: 

You might also consider using SHA256 we are seeing more and more exploits with MD5. SHA256 will require additional storage space due to the length of the hash results, but I think it is worth it.

$hashed = hash('sha256', $per_user_salt . $password . $app_salt );

Note: this does require PHP 5.1.2 or greater.

meme