Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix XORMixer double XOR issue #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions lib/RandomLib/Mixer/XorMixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@
*/

/**
* The Hash medium strength mixer class
*
* This class implements a mixer based upon the recommendations in RFC 4086
* section 5.2
* XOR mixer
*
* PHP version 5.3
*
* @see http:https://tools.ietf.org/html/rfc4086#section-5.2
*
* @category PHPCryptLib
* @package Random
* @subpackage Mixer
Expand All @@ -34,12 +29,7 @@
use SecurityLib\Strength;

/**
* The Hash medium strength mixer class
*
* This class implements a mixer based upon the recommendations in RFC 4086
* section 5.2
*
* @see http:https://tools.ietf.org/html/rfc4086#section-5.2
* XOR mixer
*
* @category PHPCryptLib
* @package Random
Expand All @@ -51,7 +41,7 @@ class XorMixer extends \RandomLib\AbstractMixer
{

/**
* Return an instance of Strength indicating the strength of the source
* Return an instance of Strength indicating the strength of the mixer
*
* @return \SecurityLib\Strength An instance of one of the strength classes
*/
Expand Down Expand Up @@ -90,7 +80,10 @@ protected function getPartSize()
*/
protected function mixParts1($part1, $part2)
{
return $part1 ^ $part2;
// The XOR operation is done in AbstractMixer; repeating it here will
// cause the previous source to get XORed with itself, setting it to
// zero
return $part2;
}

/**
Expand All @@ -104,6 +97,6 @@ protected function mixParts1($part1, $part2)
protected function mixParts2($part1, $part2)
{
// Both mixers are identical, this is for speed, not security
return $part1 ^ $part2;
return $part2;
}
}
62 changes: 62 additions & 0 deletions test/Unit/RandomLib/Mixer/XorMixerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* The RandomLib library for securely generating random numbers and strings in PHP
*
* @author Anthony Ferrara <[email protected]>
* @copyright 2011 The Authors
* @license http:https://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace RandomLib\Mixer;

use SecurityLib\Strength;

class XorMixerTest extends \PHPUnit_Framework_TestCase
{
public static function provideMix()
{
$data = array(
array(array(), ''),
array(array('1', '1'), '00'),
array(array('a'), '61'),
array(array('a', 'b'), '03'),
array(array('aa', 'ba'), '0300'),
array(array('ab', 'bb'), '0300'),
array(array('aa', 'bb'), '0303'),
array(array('aa', 'bb', 'cc'), '6060'),
array(array('aabbcc', 'bbccdd', 'ccddee'), '606065656262'),
);

return $data;
}

public function testConstructWithoutArgument()
{
$xorMixer = new XorMixer();
$this->assertTrue($xorMixer instanceof \RandomLib\Mixer);
}

public function testGetStrength()
{
$strength = new Strength(Strength::VERYLOW);
$actual = XorMixer::getStrength();
$this->assertEquals($actual, $strength);
}

public function testTest()
{
$actual = XorMixer::test();
$this->assertTrue($actual);
}

/**
* @dataProvider provideMix
*/
public function testMix($parts, $result)
{
$mixer = new XorMixer();
$actual = $mixer->mix($parts);
$this->assertSame($result, bin2hex($actual));
}
}