PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms 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
Possible License(s): AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0, ISC
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. //@require 'Swift/Transport/Esmtp/Authenticator.php';
  10. //@require 'Swift/Transport/SmtpAgent.php';
  11. //@require 'Swift/TransportException.php';
  12. /**
  13. * Handles CRAM-MD5 authentication.
  14. * @package Swift
  15. * @subpackage Transport
  16. * @author Chris Corbyn
  17. */
  18. class Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  19. implements Swift_Transport_Esmtp_Authenticator
  20. {
  21. /**
  22. * Get the name of the AUTH mechanism this Authenticator handles.
  23. * @return string
  24. */
  25. public function getAuthKeyword()
  26. {
  27. return 'CRAM-MD5';
  28. }
  29. /**
  30. * Try to authenticate the user with $username and $password.
  31. * @param Swift_Transport_SmtpAgent $agent
  32. * @param string $username
  33. * @param string $password
  34. * @return boolean
  35. */
  36. public function authenticate(Swift_Transport_SmtpAgent $agent,
  37. $username, $password)
  38. {
  39. try
  40. {
  41. $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", array(334));
  42. $challenge = base64_decode(substr($challenge, 4));
  43. $message = base64_encode(
  44. $username . ' ' . $this->_getResponse($password, $challenge)
  45. );
  46. $agent->executeCommand(sprintf("%s\r\n", $message), array(235));
  47. return true;
  48. }
  49. catch (Swift_TransportException $e)
  50. {
  51. $agent->executeCommand("RSET\r\n", array(250));
  52. return false;
  53. }
  54. }
  55. /**
  56. * Generate a CRAM-MD5 response from a server challenge.
  57. * @param string $secret
  58. * @param string $challenge
  59. * @return string
  60. */
  61. private function _getResponse($secret, $challenge)
  62. {
  63. if (strlen($secret) > 64)
  64. {
  65. $secret = pack('H32', md5($secret));
  66. }
  67. if (strlen($secret) < 64)
  68. {
  69. $secret = str_pad($secret, 64, chr(0));
  70. }
  71. $k_ipad = substr($secret, 0, 64) ^ str_repeat(chr(0x36), 64);
  72. $k_opad = substr($secret, 0, 64) ^ str_repeat(chr(0x5C), 64);
  73. $inner = pack('H32', md5($k_ipad . $challenge));
  74. $digest = md5($k_opad . $inner);
  75. return $digest;
  76. }
  77. }