作爲學習php的一部分,我想嘗試一個註冊和登錄頁面,但是,我正在關注如何使用MySQLI存儲密碼,但我沒有使用該密碼:在PHP中比較密碼的哈希值
散列密碼
$password1 = 'hello123';
// A higher "cost" is more secure but consumes more processing power
$cost = 10;
// Create a random salt
$salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
// Prefix information about the hash so PHP knows how to verify it later.
// "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
$salt = sprintf("$2a$%02d$", $cost) . $salt;
// Value:
// $2a$10$eImiTXuWVxfM37uY4JANjQ==
// Hash the password with the salt
$hash = crypt($password1, $salt);
我卡上。但是取回密碼,這裏是該網站的代碼吧:
$username = 'Admin';
$password = 'gf45_gdf#4hg';
$sth = $dbh->prepare('
SELECT
hash
FROM users
WHERE
username = :username
LIMIT 1
');
$sth->bindParam(':username', $username);
$sth->execute();
$user = $sth->fetch(PDO::FETCH_OBJ);
// Hashing the password with its hash as the salt returns the same hash
if (crypt($password, $user->hash) === $user->hash) {
// Ok!
}
從我所看到的,他抓取數據庫中用戶的密碼的哈希值,並比較使用散列傳遞的密碼,並與數據庫中的密碼進行覈對。
我一直想這一點,但結果哈希是絕不相同的原始之一:
$pwdtocheck = 'hello123';
// no call do DB yet, doing this on the same page after hashing, the $hash is the same as above
$pwdhash = crypt($pwdtocheck, $hash);
// if I echo $pwdhash it's never exactly the same as the $hash.
if ($pwdhash === $hash) {
echo "same pwd";
}
那[站點](http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/)不加載,所以我不能看到什麼內容與之相比。 –
從PHP 5.5開始,您可以使用['password_hash()'](http://php.net/password_hash)。 –
另請參閱Openwall的[PHP密碼哈希框架](http://www.openwall.com/phpass/)(PHPass)。它的可移植性和強化針對許多用戶密碼的常見攻擊。 – jww