這幾乎是做到這一點的方法。在大多數情況下,最好省略salt參數。如果沒有這個參數,該函數將從操作系統的隨機源生成一個加密安全的鹽。
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT, array("cost" => 11));
// 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($password, $existingHashFromDb);
有點更詳細:你的方法生成鹽,將需要超過必要的隨機字節,因爲BCrypt預計編碼鹽,不是你創建一個二進制鹽。據我所知,函數password_hash會正確編碼它,但它只會使用必要的字節。
注意:在這種情況下,您可以像使用$ _POST ['password']一樣直接使用(不需要轉義),因爲散列函數可以處理所有類型的輸入並生成一個「安全」字符串,不容易SQL注入。
使用單向加密,如sha1()或md5() – Sundar
只是不直接放置$ _POST。其餘的看起來不錯imo –
@Sundar - 推薦不安全的散列(甚至沒有提到鹽漬)當OP詢問推薦的解決方案在PHP中的密碼處理並不是一個非常明智的建議 –