PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/lib/vendor/symfony/lib/vendor/swiftmailer/classes/Swift/Transport/Esmtp/Auth/CramMd5Authenticator.php

https://github.com/rickycheers/SHARCK-ERP
PHP | 88 lines | 44 code | 11 blank | 33 comment | 2 complexity | f985782564fc00ca3b75f32ff03a4b62 MD5 | raw 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
11//@require 'Swift/Transport/Esmtp/Authenticator.php';
12//@require 'Swift/Transport/SmtpAgent.php';
13//@require 'Swift/TransportException.php';
14
15/**
16 * Handles CRAM-MD5 authentication.
17 * @package Swift
18 * @subpackage Transport
19 * @author Chris Corbyn
20 */
21class Swift_Transport_Esmtp_Auth_CramMd5Authenticator
22  implements Swift_Transport_Esmtp_Authenticator
23{
24  
25  /**
26   * Get the name of the AUTH mechanism this Authenticator handles.
27   * @return string
28   */
29  public function getAuthKeyword()
30  {
31    return 'CRAM-MD5';
32  }
33  
34  /**
35   * Try to authenticate the user with $username and $password.
36   * @param Swift_Transport_SmtpAgent $agent
37   * @param string $username
38   * @param string $password
39   * @return boolean
40   */
41  public function authenticate(Swift_Transport_SmtpAgent $agent,
42    $username, $password)
43  {
44    try
45    {
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)
50        );
51      $agent->executeCommand(sprintf("%s\r\n", $message), array(235));
52      return true;
53    }
54    catch (Swift_TransportException $e)
55    {
56      $agent->executeCommand("RSET\r\n", array(250));
57      return false;
58    }
59  }
60  
61  /**
62   * Generate a CRAM-MD5 response from a server challenge.
63   * @param string $secret
64   * @param string $challenge
65   * @return string
66   */
67  private function _getResponse($secret, $challenge)
68  {
69    if (strlen($secret) > 64)
70    {
71      $secret = pack('H32', md5($secret));
72    }
73    
74    if (strlen($secret) < 64)
75    {
76      $secret = str_pad($secret, 64, chr(0));
77    }
78    
79    $k_ipad = substr($secret, 0, 64) ^ str_repeat(chr(0x36), 64);
80    $k_opad = substr($secret, 0, 64) ^ str_repeat(chr(0x5C), 64);
81
82    $inner  = pack('H32', md5($k_ipad . $challenge));
83    $digest = md5($k_opad . $inner);
84
85    return $digest;
86  }
87  
88}