PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Mail/Protocol/Smtp/Auth/Crammd5.php

https://bitbucket.org/ksekar/campus
PHP | 108 lines | 41 code | 13 blank | 54 comment | 4 complexity | a87cfec54f2468bed24b00bda1b4d4ad MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage Protocol
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Crammd5.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. /**
  23. * @see Zend_Mail_Protocol_Smtp
  24. */
  25. require_once 'Zend/Mail/Protocol/Smtp.php';
  26. /**
  27. * Performs CRAM-MD5 authentication
  28. *
  29. * @category Zend
  30. * @package Zend_Mail
  31. * @subpackage Protocol
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Mail_Protocol_Smtp_Auth_Crammd5 extends Zend_Mail_Protocol_Smtp
  36. {
  37. /**
  38. * Constructor.
  39. *
  40. * @param string $host (Default: 127.0.0.1)
  41. * @param int $port (Default: null)
  42. * @param array $config Auth-specific parameters
  43. * @return void
  44. */
  45. public function __construct($host = '127.0.0.1', $port = null, $config = null)
  46. {
  47. if (is_array($config)) {
  48. if (isset($config['username'])) {
  49. $this->_username = $config['username'];
  50. }
  51. if (isset($config['password'])) {
  52. $this->_password = $config['password'];
  53. }
  54. }
  55. parent::__construct($host, $port, $config);
  56. }
  57. /**
  58. * @todo Perform CRAM-MD5 authentication with supplied credentials
  59. *
  60. * @return void
  61. */
  62. public function auth()
  63. {
  64. // Ensure AUTH has not already been initiated.
  65. parent::auth();
  66. $this->_send('AUTH CRAM-MD5');
  67. $challenge = $this->_expect(334);
  68. $challenge = base64_decode($challenge);
  69. $digest = $this->_hmacMd5($this->_password, $challenge);
  70. $this->_send(base64_encode($this->_username . ' ' . $digest));
  71. $this->_expect(235);
  72. $this->_auth = true;
  73. }
  74. /**
  75. * Prepare CRAM-MD5 response to server's ticket
  76. *
  77. * @param string $key Challenge key (usually password)
  78. * @param string $data Challenge data
  79. * @param string $block Length of blocks
  80. * @return string
  81. */
  82. protected function _hmacMd5($key, $data, $block = 64)
  83. {
  84. if (strlen($key) > 64) {
  85. $key = pack('H32', md5($key));
  86. } elseif (strlen($key) < 64) {
  87. $key = str_pad($key, $block, "\0");
  88. }
  89. $k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
  90. $k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
  91. $inner = pack('H32', md5($k_ipad . $data));
  92. $digest = md5($k_opad . $inner);
  93. return $digest;
  94. }
  95. }