如何在 PHP 中生成正确的 CRAM-MD5 响应?

How can I generate a correct CRAM-MD5 response in PHP?

我希望为 SMTP 服务器生成 CRAM-MD5 响应。

我可以在此处查看如何执行此操作的理论细节:CRAM-MD5 Implementation,但我正在寻找具体的 PHP 代码以实现它。

如何在 PHP 中实现它?

以下 PHP 代码将为 CRAM-MD5 生成有效响应:

$username = "username goes here";
$password = "password goes here";

$challenge = "challenge from server goes here";

$challenge = base64_decode($challenge);
$digest = hash_hmac("md5", $challenge, $password, false);
var_dump(base64_encode($username . ' ' . $digest));

在 telnet 会话中,您可以这样使用它:

telnet your.smtp.server.com 25
Trying 123.123.123.123...
Connected to your.smtp.server.com.
Escape character is '^]'.
220 your.smtp.server.com
EHLO your.server.com
...
250-AUTH PLAIN LOGIN DIGEST-MD5 CRAM-MD5
...
AUTH CRAM-MD5
334 <challenge from server>
<output from var_dump goes here>
235 2.7.0 Authentication successful