PageRenderTime 18ms CodeModel.GetById 14ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/nosen/jelly2
PHP | 108 lines | 41 code | 13 blank | 54 comment | 4 complexity | 78ec81a76229400054bc8e1a72973543 MD5 | raw file
  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-2011 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 23775 2011-03-01 17:25:24Z ralph $
 21 */
 22
 23
 24/**
 25 * @see Zend_Mail_Protocol_Smtp
 26 */
 27require_once 'Zend/Mail/Protocol/Smtp.php';
 28
 29
 30/**
 31 * Performs CRAM-MD5 authentication
 32 *
 33 * @category   Zend
 34 * @package    Zend_Mail
 35 * @subpackage Protocol
 36 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 37 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 38 */
 39class Zend_Mail_Protocol_Smtp_Auth_Crammd5 extends Zend_Mail_Protocol_Smtp
 40{
 41    /**
 42     * Constructor.
 43     *
 44     * @param  string $host   (Default: 127.0.0.1)
 45     * @param  int    $port   (Default: null)
 46     * @param  array  $config Auth-specific parameters
 47     * @return void
 48     */
 49    public function __construct($host = '127.0.0.1', $port = null, $config = null)
 50    {
 51        if (is_array($config)) {
 52            if (isset($config['username'])) {
 53                $this->_username = $config['username'];
 54            }
 55            if (isset($config['password'])) {
 56                $this->_password = $config['password'];
 57            }
 58        }
 59
 60        parent::__construct($host, $port, $config);
 61    }
 62
 63
 64    /**
 65     * @todo Perform CRAM-MD5 authentication with supplied credentials
 66     *
 67     * @return void
 68     */
 69    public function auth()
 70    {
 71        // Ensure AUTH has not already been initiated.
 72        parent::auth();
 73
 74        $this->_send('AUTH CRAM-MD5');
 75        $challenge = $this->_expect(334);
 76        $challenge = base64_decode($challenge);
 77        $digest = $this->_hmacMd5($this->_password, $challenge);
 78        $this->_send(base64_encode($this->_username . ' ' . $digest));
 79        $this->_expect(235);
 80        $this->_auth = true;
 81    }
 82
 83
 84    /**
 85     * Prepare CRAM-MD5 response to server's ticket
 86     *
 87     * @param  string $key   Challenge key (usually password)
 88     * @param  string $data  Challenge data
 89     * @param  string $block Length of blocks
 90     * @return string
 91     */
 92    protected function _hmacMd5($key, $data, $block = 64)
 93    {
 94        if (strlen($key) > 64) {
 95            $key = pack('H32', md5($key));
 96        } elseif (strlen($key) < 64) {
 97            $key = str_pad($key, $block, "\0");
 98        }
 99
100        $k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
101        $k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
102
103        $inner = pack('H32', md5($k_ipad . $data));
104        $digest = md5($k_opad . $inner);
105
106        return $digest;
107    }
108}