3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mail\Protocol\Smtp\Auth
;
13 use Zend\Mail\Protocol\Smtp
;
16 * Performs CRAM-MD5 authentication
18 class Crammd5
extends Smtp
35 * All parameters may be passed as an array to the first argument of the
38 * @param string|array $host (Default: 127.0.0.1)
39 * @param null|int $port (Default: null)
40 * @param null|array $config Auth-specific parameters
42 public function __construct($host = '127.0.0.1', $port = null, $config = null)
44 // Did we receive a configuration array?
45 $origConfig = $config;
46 if (is_array($host)) {
47 // Merge config array with principal array, if provided
48 if (is_array($config)) {
49 $config = array_replace_recursive($host, $config);
55 if (is_array($config)) {
56 if (isset($config['username'])) {
57 $this->setUsername($config['username']);
59 if (isset($config['password'])) {
60 $this->setPassword($config['password']);
64 // Call parent with original arguments
65 parent
::__construct($host, $port, $origConfig);
70 * Performs CRAM-MD5 authentication with supplied credentials
72 public function auth()
74 // Ensure AUTH has not already been initiated.
77 $this->_send('AUTH CRAM-MD5');
78 $challenge = $this->_expect(334);
79 $challenge = base64_decode($challenge);
80 $digest = $this->_hmacMd5($this->getPassword(), $challenge);
81 $this->_send(base64_encode($this->getUsername() . ' ' . $digest));
87 * Set value for username
89 * @param string $username
92 public function setUsername($username)
94 $this->username
= $username;
103 public function getUsername()
105 return $this->username
;
109 * Set value for password
111 * @param string $password
114 public function setPassword($password)
116 $this->password
= $password;
125 public function getPassword()
127 return $this->password
;
131 * Prepare CRAM-MD5 response to server's ticket
133 * @param string $key Challenge key (usually password)
134 * @param string $data Challenge data
135 * @param int $block Length of blocks (deprecated; unused)
138 protected function _hmacMd5($key, $data, $block = 64)
140 return Hmac
::compute($key, 'md5', $data);