TYPO3  7.6
CramMd5Authenticator.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
17 {
23  public function getAuthKeyword()
24  {
25  return 'CRAM-MD5';
26  }
27 
37  public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password)
38  {
39  try {
40  $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", array(334));
41  $challenge = base64_decode(substr($challenge, 4));
42  $message = base64_encode(
43  $username.' '.$this->_getResponse($password, $challenge)
44  );
45  $agent->executeCommand(sprintf("%s\r\n", $message), array(235));
46 
47  return true;
48  } catch (Swift_TransportException $e) {
49  $agent->executeCommand("RSET\r\n", array(250));
50 
51  return false;
52  }
53  }
54 
63  private function _getResponse($secret, $challenge)
64  {
65  if (strlen($secret) > 64) {
66  $secret = pack('H32', md5($secret));
67  }
68 
69  if (strlen($secret) < 64) {
70  $secret = str_pad($secret, 64, chr(0));
71  }
72 
73  $k_ipad = substr($secret, 0, 64) ^ str_repeat(chr(0x36), 64);
74  $k_opad = substr($secret, 0, 64) ^ str_repeat(chr(0x5C), 64);
75 
76  $inner = pack('H32', md5($k_ipad.$challenge));
77  $digest = md5($k_opad.$inner);
78 
79  return $digest;
80  }
81 }