PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/core/Associates/SwiftMailer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/Esmtp/Auth/CramMd5Authenticator.php

https://gitlab.com/fiesta-framework/Documentation
PHP | 81 lines | 37 code | 10 blank | 34 comment | 2 complexity | abbf9cebe66ca81562bb8f0e973692c0 MD5 | raw file
  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. /**
  10. * Handles CRAM-MD5 authentication.
  11. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_Transport_Esmtp_Auth_CramMd5Authenticator implements Swift_Transport_Esmtp_Authenticator
  15. {
  16. /**
  17. * Get the name of the AUTH mechanism this Authenticator handles.
  18. *
  19. * @return string
  20. */
  21. public function getAuthKeyword()
  22. {
  23. return 'CRAM-MD5';
  24. }
  25. /**
  26. * Try to authenticate the user with $username and $password.
  27. *
  28. * @param Swift_Transport_SmtpAgent $agent
  29. * @param string $username
  30. * @param string $password
  31. *
  32. * @return bool
  33. */
  34. public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password)
  35. {
  36. try {
  37. $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", array(334));
  38. $challenge = base64_decode(substr($challenge, 4));
  39. $message = base64_encode(
  40. $username.' '.$this->_getResponse($password, $challenge)
  41. );
  42. $agent->executeCommand(sprintf("%s\r\n", $message), array(235));
  43. return true;
  44. } catch (Swift_TransportException $e) {
  45. $agent->executeCommand("RSET\r\n", array(250));
  46. return false;
  47. }
  48. }
  49. /**
  50. * Generate a CRAM-MD5 response from a server challenge.
  51. *
  52. * @param string $secret
  53. * @param string $challenge
  54. *
  55. * @return string
  56. */
  57. private function _getResponse($secret, $challenge)
  58. {
  59. if (strlen($secret) > 64) {
  60. $secret = pack('H32', md5($secret));
  61. }
  62. if (strlen($secret) < 64) {
  63. $secret = str_pad($secret, 64, chr(0));
  64. }
  65. $k_ipad = substr($secret, 0, 64) ^ str_repeat(chr(0x36), 64);
  66. $k_opad = substr($secret, 0, 64) ^ str_repeat(chr(0x5C), 64);
  67. $inner = pack('H32', md5($k_ipad.$challenge));
  68. $digest = md5($k_opad.$inner);
  69. return $digest;
  70. }
  71. }