4
* This file is part of SwiftMailer.
5
* (c) 2004-2009 Chris Corbyn
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
11
//@require 'Swift/Transport/Esmtp/Authenticator.php';
12
//@require 'Swift/Transport/SmtpAgent.php';
13
//@require 'Swift/TransportException.php';
16
* Handles CRAM-MD5 authentication.
18
* @subpackage Transport
19
* @author Chris Corbyn
21
class Swift_Transport_Esmtp_Auth_CramMd5Authenticator
22
implements Swift_Transport_Esmtp_Authenticator
26
* Get the name of the AUTH mechanism this Authenticator handles.
29
public function getAuthKeyword()
35
* Try to authenticate the user with $username and $password.
36
* @param Swift_Transport_SmtpAgent $agent
37
* @param string $username
38
* @param string $password
41
public function authenticate(Swift_Transport_SmtpAgent $agent,
46
$challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", array(334));
47
$challenge = base64_decode(substr($challenge, 4));
48
$message = base64_encode(
49
$username . ' ' . $this->_getResponse($password, $challenge)
51
$agent->executeCommand(sprintf("%s\r\n", $message), array(235));
54
catch (Swift_TransportException $e)
56
$agent->executeCommand("RSET\r\n", array(250));
62
* Generate a CRAM-MD5 response from a server challenge.
63
* @param string $secret
64
* @param string $challenge
67
private function _getResponse($secret, $challenge)
69
if (strlen($secret) > 64)
71
$secret = pack('H32', md5($secret));
74
if (strlen($secret) < 64)
76
$secret = str_pad($secret, 64, chr(0));
79
$k_ipad = substr($secret, 0, 64) ^ str_repeat(chr(0x36), 64);
80
$k_opad = substr($secret, 0, 64) ^ str_repeat(chr(0x5C), 64);
82
$inner = pack('H32', md5($k_ipad . $challenge));
83
$digest = md5($k_opad . $inner);