Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'gwis-use-zend-crypt-hmac-in-crammd5'
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Jun 28, 2013
2 parents 474953f + 9c8b880 commit 2706eec
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
20 changes: 4 additions & 16 deletions library/Zend/Mail/Protocol/Smtp/Auth/Crammd5.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Mail\Protocol\Smtp\Auth;

use Zend\Crypt\Hmac;
use Zend\Mail\Protocol\Smtp;

/**
Expand Down Expand Up @@ -66,8 +67,7 @@ public function __construct($host = '127.0.0.1', $port = null, $config = null)


/**
* @todo Perform CRAM-MD5 authentication with supplied credentials
*
* Performs CRAM-MD5 authentication with supplied credentials
*/
public function auth()
{
Expand Down Expand Up @@ -132,23 +132,11 @@ public function getPassword()
*
* @param string $key Challenge key (usually password)
* @param string $data Challenge data
* @param int $block Length of blocks
* @param int $block Length of blocks (deprecated; unused)
* @return string
*/
protected function _hmacMd5($key, $data, $block = 64)
{
if (strlen($key) > 64) {
$key = pack('H32', md5($key));
} elseif (strlen($key) < 64) {
$key = str_pad($key, $block, "\0");
}

$kIpad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
$kOpad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);

$inner = pack('H32', md5($kIpad . $data));
$digest = md5($kOpad . $inner);

return $digest;
return Hmac::compute($key, 'md5', $data);
}
}
1 change: 1 addition & 0 deletions library/Zend/Mail/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"target-dir": "Zend/Mail",
"require": {
"php": ">=5.3.3",
"zendframework/zend-crypt": "self.version",
"zendframework/zend-loader": "self.version",
"zendframework/zend-mime": "self.version",
"zendframework/zend-stdlib": "self.version"
Expand Down
47 changes: 47 additions & 0 deletions tests/ZendTest/Mail/Protocol/Smtp/Auth/Crammd5Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mail
*/

namespace ZendTest\Mail\Protocol\Smtp\Auth;

use ReflectionClass;
use Zend\Mail\Protocol\Smtp\Auth\Crammd5;

/**
* @category Zend
* @package Zend_Mail
* @subpackage UnitTests
* @group Zend_Mail
*/
class Crammd5Test extends \PHPUnit_Framework_TestCase
{
/**
* @var Crammd5
*/
protected $auth;

public function setUp()
{
$this->auth = new Crammd5();
}

public function testHmacMd5ReturnsExpectedHash()
{
$class = new ReflectionClass('Zend\Mail\Protocol\Smtp\Auth\Crammd5');
$method = $class->getMethod('_hmacMd5');
$method->setAccessible(true);

$result = $method->invokeArgs(
$this->auth,
array('frodo', 'speakfriendandenter')
);

$this->assertEquals('be56fa81a5671e0c62e00134180aae2c', $result);
}
}

0 comments on commit 2706eec

Please sign in to comment.