I recommend using the PHPass library to secure your passwords, since md5 and shai and other encryption algorithms are NOT safe.
PHPass takes care of
- Encrypting.
- Salting.
- Stretching.
And that method has not yet been cracked (brute forcing can not be considered as an crack for PHPass).
You can get it from here: http://www.openwall.com/phpass/
IT's very easy to hash once you've set up the code:
$hash = PasswordHash($iteration_count_log2, $portable_hashes)
And then you can fetch the stored hash from the database, and match the login-password-hash with the existing hash via the function
$auth = CheckPassword($incomingPasswordNotHashed, $hashFromDatabase);
I like to make it simpler by doing something like this to make an hash:
// Usage: $hash = PHPhass($password);
// Pre: $password is of type string,
// indicating the password which
// the user want's to hash.
// Post: $hash is the hashed password.
function PHPhass($password)
{
$unHashed = $password;
require '../phpass/PasswordHash.php';
header('Content-type: text/plain');
$t_hasher = new PasswordHash(12, FALSE); // Define the iterations once.
$hash = $t_hasher->HashPassword($unHashed);
unset($t_hasher);
return $hash;
}
And doing this to match a hash and a non-hashed password:
// Usage: $check = PHPhassMatch($password, $hash)
// Pre: $password is of type string,
// indicating a user's passwordd
// $hash is a hashed password.
// Post: $check is true if $password's hash
// value is equal to $hash.
function PHPhassMatch($password, $hash)
{
$unHashed = $password;
$theHashed = $hash;
require '../phpass/PasswordHash.php';
header('Content-type: text/plain');
$t_hasher = new PasswordHash(8, FALSE);
$check = $t_hasher->CheckPassword($unHashed, $theHashed);
unset($t_hasher);
return $check;
}
Also, like Fresh mentioned:
- Don't use the mysql_* since It's deprecated, I recommend using PDO or mysqli.
- Use prepared statements to prevent sql injection.
- Don't assume that the posted variables are posted, check if they exist with isset()
You mentioned shai1, are you sure that the passwords in the database are not sha1 but you're matching against md5?