我建議使用PHPass庫來保護您的密碼,因爲md5和shai以及其他加密算法並不安全。
PHPass需要
- 加密的照顧。
- 醃製。
- 拉伸。
而且該方法尚未破解(暴力強制不能被視爲PHPass的破解)。
你可以從這裏得到它:http://www.openwall.com/phpass/
這很容易湊一旦你設置代碼:
$hash = PasswordHash($iteration_count_log2, $portable_hashes)
然後你就可以從數據庫讀取存儲的哈希,和通過功能相匹配的登錄密碼哈希與現有的哈希
$auth = CheckPassword($incomingPasswordNotHashed, $hashFromDatabase);
我喜歡更簡單的做這樣的事情做出一個哈希:
// 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;
}
而且這樣做是爲了匹配散列和非散列密碼:
// 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;
}
此外,像新鮮提到:
- 不要使用mysql_ *,因爲它的棄用,我建議使用PDO或mysqli。
- 使用預準備語句來防止sql注入。
- 不要以爲張貼變量張貼,檢查它們是否與isset()函數
你提到shai1存在,你肯定是在數據庫中的密碼不SHA1,但你對匹配MD5?
請請請不要使用MD5使用https://github.com/ircmaxell/password_compat – iConnor
您可能需要閱讀:http://stackoverflow.com/問題/ 401656/secure-hash-and-salt-for-php-passwords – Baba
所以你'sha1()'在註冊時輸入密碼,而在登錄時輸入'md5()'密碼? – Wiseguy