最近,我一直在嘗試在互聯(lián)網(wǎng)上偶然發(fā)現(xiàn)的登錄腳本上實(shí)現(xiàn)自己的安全性。在努力學(xué)習(xí)如何制作自己的腳本為每個(gè)用戶生成鹽之后,我偶然發(fā)現(xiàn)了 password_hash
。
據(jù)我了解(基于本頁的閱讀),當(dāng)您使用 password_hash
時(shí),鹽已經(jīng)在行中生成。這是真的?
我的另一個(gè)問題是,有 2 種鹽不是明智的嗎?一種直接在文件中,一種在數(shù)據(jù)庫中?這樣,如果有人破壞了數(shù)據(jù)庫中的鹽,您仍然可以直接在文件中保存鹽嗎?我在這里讀到,儲存鹽從來都不是一個(gè)聰明的主意,但它總是讓我困惑人們的意思。
是的,您理解正確,函數(shù)password_hash()將自行生成鹽,并將其包含在生成的哈希值中。將鹽存儲在數(shù)據(jù)庫中是絕對正確的,即使已知它也能完成其工作。
// Hash a new password for storing in the database. // The function automatically generates a cryptographically safe salt. $hashToStoreInDb = password_hash($_POST['password'], PASSWORD_DEFAULT); // Check if the hash of the entered login password, matches the stored hash. // The salt and the cost factor will be extracted from $existingHashFromDb. $isPasswordCorrect = password_verify($_POST['password'], $existingHashFromDb);
您提到的第二個(gè)鹽(存儲在文件中的鹽)實(shí)際上是胡椒或服務(wù)器端密鑰。如果你在散列之前添加它(就像鹽一樣),那么你就添加了胡椒粉。不過,有一種更好的方法,您可以首先計(jì)算哈希值,然后使用服務(wù)器端密鑰加密(雙向)哈希值。這使您可以在必要時(shí)更改密鑰。
與鹽相反,這個(gè)密鑰應(yīng)該保密。人們經(jīng)常混淆它并試圖隱藏鹽,但最好讓鹽發(fā)揮其作用并用密鑰添加秘密。
建議使用password_hash
來存儲密碼。不要將它們分成數(shù)據(jù)庫和文件。
假設(shè)我們有以下輸入:
$password = $_POST['password'];
您首先通過執(zhí)行以下操作對密碼進(jìn)行哈希處理:
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
然后查看輸出:
var_dump($hashed_password);
正如你所看到的,它是經(jīng)過哈希處理的。 (我假設(shè)您執(zhí)行了這些步驟)。
現(xiàn)在,您將此散列密碼存儲在數(shù)據(jù)庫中,確保您的密碼列足夠大以容納散列值(至少 60 個(gè)字符或更長)。當(dāng)用戶要求登錄時(shí),您可以使用數(shù)據(jù)庫中的哈希值檢查輸入的密碼,方法如下:
// Query the database for username and password // ... if(password_verify($password, $hashed_password)) { // If the password inputs matched the hashed password in the database // Do something, you know... log them in. } // Else, Redirect them back to the login page.