Thursday, 8 August 2013

how could I do a login form with crypt and blowfish?

how could I do a login form with crypt and blowfish?

I use the next code to to register a user: the name and the password, to
store password I use crypt() and blowfish, it works, but I don't know how
could descrypt the password in a login form
$user = $_POST['user'];
$password = $_POST['pass'];
function cryptPass($input, $rounds = 5){
$salt = "";
$saltChars = array_merge(range('A','Z'), range('a','z'), range(0,9));
for($i=0; $i < 22; $i++) {
$salt .= $saltChars[array_rand($saltChars)];
}
return crypt($input, sprintf('$2y$%02d$', $rounds) . $salt);
}
$password_hash = cryptPass($password);
$cSQL = "INSERT INTO USERDB (NAME, PASS) VALUES(?,?)";
$stmt = $db->prepare($cSQL);
$stmt->bind_param('ss', $name, $password_hash);
$stmt->execute();
$stmt->close();
And this is the function to descrypt the password, but it doesn't work,
where's my mistake?
$user = $_POST['user'];
$passcrypt= $_POST['pass'];
function cryptPass($input, $rounds = 6)
{
$salt = "";
$saltChars = array_merge(range('A','Z'), range('a','z'), range(0,9));
for($i=0; $i < 22; $i++) {
$salt .= $saltChars[array_rand($saltChars)];
}
return crypt($input, sprintf('$2x$%02d$', $rounds) . $salt);
}
$inputPass = $password;
$pass = $password;
$hashedPass = cryptPass($pass);
$passcrypt = crypt($inputPass, $hashedPass);
$stmt = $db->prepare("SELECT EMAIL, PASS
FROM USERDB
WHERE EMAIL = ? AND PASS =?");
$stmt->bind_param('ss', $user, $passcrypt);
$stmt->execute();
$stmt->close();

No comments:

Post a Comment