2011-11-03 66 views
0

我編寫了一個類來使用HTTP身份驗證摘要方式對用戶進行身份驗證。我閱讀了一些文章,並使其工作。現在,我想讓它使用Md5密碼,但我似乎無法得到它的工作,這是驗證用戶的功能。使用MD5進行PHP摘要式身份驗證

public function authenticate() { 

// In case the user is not logged in already. 
if (empty($_SERVER['PHP_AUTH_DIGEST'])) { 

    // Return the headers. 
    $this->show_auth(); 

} else { 

    // Parse the given Digest-data. 
    $data = $this->parse_request($_SERVER['PHP_AUTH_DIGEST']); 

    // Check the data. 
    if (!$data) { 

     // Display an error message. 
     die($this->unauthorized); 

    } else { 

     // Based on the given information, generate the valid response. 
     $usr_password = "test"; 

     // Generate the response partly. 
     $A1 = md5($data['username'].":".$this->get_realm().":".$usr_password); 
     $A2 = md5($_SERVER['REQUEST_METHOD'].":".$data['uri']); 

     // Generate the valid response. 
     $val_response = md5($A1.":".$data['nonce'].":".$data['nc'].":".$data['cnonce'].":".$data['qop'].":".$A2); 

     // Compare the valid response with the given response. 
     if ($data['response'] != $val_response) { 

      // Display the login again. 
      $this->show_auth(); 

     } else { 

      // Return true. 
      return true; 

     } 

    } 

} 

}

所以可以想象$ usr_password = 「測試」 將是$ usr_password = MD5( 「測試」);

如何比較密碼呢?

謝謝。

回答

1

MD5功能是散列函數,以產生對於相同的輸入相同的結果單向方法。

因此,比較$password1$password2不透露(直接比較)他們都應該是足夠的,比較他們的哈希值:

$hash1 = md5($password1); // hash for pass 1 
$hash2 = md5($password2); // hash for pass 2 

if ($hash1 === $hash2) { 
    // here goes the code to support case of passwords being identical 
} else { 
    // here goes the code to support case of passwords not being identical 
} 

是否足夠清晰?讓我知道。

+0

我知道如何工作的,只是沒怎麼密碼的比較在HTTP認證過程的處理。 – Roel

相關問題