PHP Digest аутентификация с MD5 - PullRequest
       19

PHP Digest аутентификация с MD5

0 голосов
/ 03 ноября 2011

Я написал класс для аутентификации пользователя с использованием HTTP-аутентификации в режиме дайджеста.Я прочитал несколько статей, и я получил это работает.Теперь я хотел бы позволить ему использовать пароли Md5, но я не могу заставить его работать, это функция аутентификации пользователей.

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 = "test" будет $ usr_password = md5 ("test");

Как мне тогда сравнивать пароли?

Спасибо.

1 Ответ

0 голосов
/ 03 ноября 2011

Функция 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
}

Достаточно ли ясно?Дайте мне знать.

...