$creation_salt = sha1($username.':'.time());
This salt is way to easy to guess. Considering time() is a number of seconds, there are only 60*60 in an hour, and 60*60*24 in an hour: 86400 possible salts in one your.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
Since time() is a timestamp (number of seconds since Epoch), there are only 60*60 different values of time in one hour.
Here is an example of generating a more random value:
/**
* Generate a random hex based token
* @return String
* @param $length Int[optional]
*/
public static function generateToken($length = 40)
{
$token = array();
for( $i = 0; $i < $length; ++$i )
{
$token[] = dechex( mt_rand(0, 15) );
}
return implode('', $token);
}
That generates a hex of 40 digits. See: http://www.bucabay.com/web-development/secure-password-hashing-storage-ph/ for the full code.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
Rkeast, no problem.
If you read the post on the page I linked, you'll see that using key based encryption such as:
XORDecrypt($_COOKIE['password'], $row['creation_salt']);
is also not advisable.
XORDecrypt also has a few problems in implementation. The main being the key is repeated over the length of the data being encrypted.
$rPos = $i % $KeyPhraseLength;
The modulo is being taken, which means passwords twice as long as the key can be deduced directly by comparing bits flipped in each corresponding byte of modulo $KeyPhraseLength.
It is possible to make a simplistic encryption as such suitable, but it would require generating a one time pad, which is a random key that is as long as the password. I believe one time pads are proven mathematically to be unbreakable with cryptanalysis.
Saving the salted MD5 in the cookie would be a lot better then using XORDecrypt.
However, it is best to use sessions. That way only the session ID is saved in a cookie. The session ID is generated randomly, when the user logs in, and is used to identify the user, for the rest of the session. That way you never need to save a password, even in encrypted form, on the client.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101