2013-04-16 175 views
0

我似乎已經嘗試了各種方式讓我的日誌工作,但是當我嘗試登錄時,它停在用戶密碼處並且從不提取它。我在我的註冊表中使用了sha1,在插入數據庫時​​很好。用戶登錄的php md5密碼

$username = $_POST['username']; 
$password = md5($_POST['password']); 

$result = mysql_query("SELECT * FROM users WHERE username = '$username' "); 



while ($row = mysql_fetch_array($result)) 
{ 
    if($username == $row['username']) 
    { 
     if($password == $row['password']) 
     { 
      header("location: profile.php"); 
     } 
     else  
      echo 'pass problem'; 
    } 
    else 

     echo 'username issue '; 


} 
+2

請請請不要使用MD5使用https://github.com/ircmaxell/password_compat – iConnor

+0

您可能需要閱讀:http://stackoverflow.com/問題/ 401656/secure-hash-and-salt-for-php-passwords – Baba

+5

所以你'sha1()'在註冊時輸入密碼,而在登錄時輸入'md5()'密碼? – Wiseguy

回答

2

這麼多的問題...

  1. md5是不安全的。使用鹽+鍵+密碼。
  2. mysql_功能已棄用。使用mysqli_PDO
  3. 沒有衛生設施。使用預準備語句和綁定參數。
  4. 不要SELECT *。只獲取你需要的列。
  5. 你想要多少成績來回報
  6. 無需一個while循環的結果添加LIMIT
  7. 密碼是如何存儲在數據庫中的?很明顯,md5的密碼與您的列不匹配。

實際問題

如果用戶的密碼被存儲爲sha1再對比md5不能混淆加密類型。

+0

我也想過一樣..從哪裏開始? – Jonast92

+1

'salt + key + password.'仍然是錯誤的 – Baba

+1

Salting +哈希+拉伸。 – Jonast92

1

我建議使用PHPass庫來保護您的密碼,因爲md5和shai以及其他加密算法並不安全。

PHPass需要

  1. 加密的照顧。
  2. 醃製。
  3. 拉伸。

而且該方法尚未破解(暴力強制不能被視爲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?

+1

一些注意事項:1.它是SHA,而不是shai。 2.有序列表中的項目順序不正確:先是鹽,然後是散列。 3.加密和散列之間有區別,你不想加密密碼,你想散列它。如果暴力強制不能被認爲是這個類的破解,不要使用它!使用當前可用的雲和GPU,暴力強制應被視爲嚴重威脅。我沒有看過這個類,但適當的拉伸量完全取決於你需要的哈希值。例如,我想散列一個密碼需要100毫秒。 –

+0

我知道散列和加密之間的區別,但是您指出它是爲了讓其他人閱讀。不過實話說? @PelletenCate從哪裏開始......如果用戶創建了一個錯誤的密碼,並且沒有哈希可以改變這個密碼,那麼你總是可以強制密碼,所以不要說如果一個庫不能保證你不受惡作劇的攻擊,那麼就不要說它是壞的,沒有辦法可以讓你免於暴力破壞爲了一個好的密碼。此外,圖書館的設計使你永遠不會超過72次,但你可以定製它,我認爲12是標準的。無論如何,我認爲你應該閱讀更多關於這個話題。 – Jonast92

+0

@ Jonast92:true。字典密碼永遠不會安全。但隨着sha1 + salt甚至8個字符的隨機密碼(a-zA-Z0-9 + chars,總共94個字符)將快速下降。但是,對於BCrypt,同一個品牌的6個字符*隨機密碼將會持續相當長的時間。另外,我不確定你從哪裏得到* 72次*,但這根本不是真的。 phpass(你鏈接的那個)使用bcrypt。成本是2^n的數字。所以12的成本表示2^12或4096的成本。另外,你作爲例子顯示的API(phphass等)不是來自phpass。我不知道他們是什麼...... – ircmaxell

0

如果您使用sha1插入新密碼,您也必須使用sha1進行密碼比較。如果您將md5字​​符串與sha1字符串進行比較,則不起作用。所以,你必須這樣做:

$password = sha1($_POST['password']);