1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16:
17: class Swift_Transport_Esmtp_Auth_CramMd5Authenticator implements Swift_Transport_Esmtp_Authenticator
18: {
19: 20: 21: 22:
23: public function getAuthKeyword()
24: {
25: return 'CRAM-MD5';
26: }
27:
28: 29: 30: 31: 32: 33: 34:
35: public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password)
36: {
37: try {
38: $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", array(334));
39: $challenge = base64_decode(substr($challenge, 4));
40: $message = base64_encode(
41: $username . ' ' . $this->_getResponse($password, $challenge)
42: );
43: $agent->executeCommand(sprintf("%s\r\n", $message), array(235));
44:
45: return true;
46: } catch (Swift_TransportException $e) {
47: $agent->executeCommand("RSET\r\n", array(250));
48:
49: return false;
50: }
51: }
52:
53: 54: 55: 56: 57: 58:
59: private function _getResponse($secret, $challenge)
60: {
61: if (strlen($secret) > 64) {
62: $secret = pack('H32', md5($secret));
63: }
64:
65: if (strlen($secret) < 64) {
66: $secret = str_pad($secret, 64, chr(0));
67: }
68:
69: $k_ipad = substr($secret, 0, 64) ^ str_repeat(chr(0x36), 64);
70: $k_opad = substr($secret, 0, 64) ^ str_repeat(chr(0x5C), 64);
71:
72: $inner = pack('H32', md5($k_ipad . $challenge));
73: $digest = md5($k_opad . $inner);
74:
75: return $digest;
76: }
77: }
78: