Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#1470 from denixport/fe…
Browse files Browse the repository at this point in the history
…ature/math-biginteger

Math\BigInteger refactoring
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
30 changes: 9 additions & 21 deletions src/PublicKey/DiffieHellman.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Zend\Crypt\PublicKey;

use Zend\Crypt\Exception;
use Zend\Math\Math;
use Zend\Math;

/**
* PHP implementation of the Diffie-Hellman public key encryption algorithm.
Expand Down Expand Up @@ -67,7 +67,7 @@ class DiffieHellman
/**
* BigInteger support object courtesy of Zend\Math\Math
*
* @var \Zend\Math\Math
* @var \Zend\Math\BigInteger\Adapter\AdapterInterface
*/
private $math = null;

Expand Down Expand Up @@ -108,7 +108,9 @@ public function __construct($prime, $generator, $privateKey = null, $privateKeyF
if ($privateKey !== null) {
$this->setPrivateKey($privateKey, $privateKeyFormat);
}
$this->setBigIntegerMath();

// set up BigInteger adapter
$this->math = Math\BigInteger\BigInteger::factory();
}

/**
Expand Down Expand Up @@ -384,20 +386,6 @@ public function hasPrivateKey()
return isset($this->privateKey);
}

/**
* Setter to pass an extension parameter which is used to create
* a specific BigInteger instance for a specific extension type.
* Allows manual setting of the class in case of an extension
* problem or bug.
*
* @param string $extension
* @return void
*/
public function setBigIntegerMath($extension = null)
{
$this->math = new Math($extension);
}

/**
* Convert number between formats
*
Expand All @@ -417,7 +405,7 @@ protected function convert($number, $inputFormat = self::FORMAT_NUMBER,
switch ($inputFormat) {
case self::FORMAT_BINARY:
case self::FORMAT_BTWOC:
$number = $this->math->fromBinary($number);
$number = $this->math->binToInt($number);
break;
case self::FORMAT_NUMBER:
default:
Expand All @@ -428,10 +416,10 @@ protected function convert($number, $inputFormat = self::FORMAT_NUMBER,
// convert to output format
switch ($outputFormat) {
case self::FORMAT_BINARY:
return $this->math->toBinary($number);
return $this->math->intToBin($number);
break;
case self::FORMAT_BTWOC:
return $this->math->btwoc($this->math->toBinary($number));
return $this->math->intToBin($number, true);
break;
case self::FORMAT_NUMBER:
default:
Expand All @@ -451,7 +439,7 @@ protected function convert($number, $inputFormat = self::FORMAT_NUMBER,
*/
protected function generatePrivateKey()
{
$rand = $this->math->randBytes(strlen($this->getPrime()), true);
$rand = Math\Math::randBytes(strlen($this->getPrime()), true);
return $rand;
}
}
16 changes: 8 additions & 8 deletions test/PublicKey/DiffieHellmanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class DiffieHellmanTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
try {
$math = new BigInteger();
$math = BigInteger\BigInteger::factory();
} catch (MathException $e) {
if (strpos($e->getMessage(), 'big integer precision math support not detected') !== false) {
if (strpos($e->getMessage(), 'math support is not detected') !== false) {
$this->markTestSkipped($e->getMessage());
} else {
throw $e;
Expand Down Expand Up @@ -101,12 +101,12 @@ public function testWithBinaryFormsAndLargeIntegers()
$alice->generateKeys();
$bob->generateKeys();

//0DmJUe9dr02pAtVoGyLHdC+rfBU3mDCelKGPXRDFHofx6mFfN2gcZCmp/ab4ezDXfpIBOatpVdbn2fTNUGo64DtKE2WGTsZCl90RgrGUv8XW/4WDPXeE7g5u7KWHBG/LCE5+XsilE5P5/GIyqr9gsiudTmk+H/hiYZl9Smar9k0=
$this->assertEquals('ANA5iVHvXa9NqQLVaBsix3Qvq3wVN5gwnpShj10QxR6H8ephXzdoHGQpqf2m+Hsw136SATmraVXW59n0zVBqOuA7ShNlhk7GQpfdEYKxlL/F1v+Fgz13hO4ObuylhwRvywhOfl7IpROT+fxiMqq/YLIrnU5pPh/4YmGZfUpmq/ZN',
//ANA5iVHvXa9NqQLVaBsix3Qvq3wVN5gwnpShj10QxR6H8ephXzdoHGQpqf2m+Hsw136SATmraVXW59n0zVBqOuA7ShNlhk7GQpfdEYKxlL/F1v+Fgz13hO4ObuylhwRvywhOfl7IpROT+fxiMqq/YLIrnU5pPh/4YmGZfUpmq/ZN
$this->assertEquals('0DmJUe9dr02pAtVoGyLHdC+rfBU3mDCelKGPXRDFHofx6mFfN2gcZCmp/ab4ezDXfpIBOatpVdbn2fTNUGo64DtKE2WGTsZCl90RgrGUv8XW/4WDPXeE7g5u7KWHBG/LCE5+XsilE5P5/GIyqr9gsiudTmk+H/hiYZl9Smar9k0=',
base64_encode($alice->getPublicKey(DiffieHellman::FORMAT_BINARY)));

//v8puCBaHdch0stxmkyS/sZvZHyB5f0AVkopAQ5wKSZIyEHHcGn7DXXH2u4WdCL+kMr8BcRpxRBJ0TDwfZPpu53nFNEjUd81WlfaKk95e4a/DC4dhlfBkQMebleobhedQPFAo7F9SkHN7uTLa/glxG+3T9DTb+ikcOVPoH3A1G6g=
$this->assertEquals('AL/KbggWh3XIdLLcZpMkv7Gb2R8geX9AFZKKQEOcCkmSMhBx3Bp+w11x9ruFnQi/pDK/AXEacUQSdEw8H2T6bud5xTRI1HfNVpX2ipPeXuGvwwuHYZXwZEDHm5XqG4XnUDxQKOxfUpBze7ky2v4JcRvt0/Q02/opHDlT6B9wNRuo',
//AL/KbggWh3XIdLLcZpMkv7Gb2R8geX9AFZKKQEOcCkmSMhBx3Bp+w11x9ruFnQi/pDK/AXEacUQSdEw8H2T6bud5xTRI1HfNVpX2ipPeXuGvwwuHYZXwZEDHm5XqG4XnUDxQKOxfUpBze7ky2v4JcRvt0/Q02/opHDlT6B9wNRuo
$this->assertEquals('v8puCBaHdch0stxmkyS/sZvZHyB5f0AVkopAQ5wKSZIyEHHcGn7DXXH2u4WdCL+kMr8BcRpxRBJ0TDwfZPpu53nFNEjUd81WlfaKk95e4a/DC4dhlfBkQMebleobhedQPFAo7F9SkHN7uTLa/glxG+3T9DTb+ikcOVPoH3A1G6g=',
base64_encode($bob->getPublicKey(DiffieHellman::FORMAT_BINARY)));

$aliceSecretKey = $alice->computeSecretKey($bob->getPublicKey(DiffieHellman::FORMAT_BINARY), DiffieHellman::FORMAT_BINARY,
Expand Down Expand Up @@ -149,9 +149,9 @@ public function testWithBinaryFormsAndLargeIntegersAndOpenssl()
$alice->generateKeys();
$bob->generateKeys();

$this->assertEquals('ANA5iVHvXa9NqQLVaBsix3Qvq3wVN5gwnpShj10QxR6H8ephXzdoHGQpqf2m+Hsw136SATmraVXW59n0zVBqOuA7ShNlhk7GQpfdEYKxlL/F1v+Fgz13hO4ObuylhwRvywhOfl7IpROT+fxiMqq/YLIrnU5pPh/4YmGZfUpmq/ZN',
$this->assertEquals('0DmJUe9dr02pAtVoGyLHdC+rfBU3mDCelKGPXRDFHofx6mFfN2gcZCmp/ab4ezDXfpIBOatpVdbn2fTNUGo64DtKE2WGTsZCl90RgrGUv8XW/4WDPXeE7g5u7KWHBG/LCE5+XsilE5P5/GIyqr9gsiudTmk+H/hiYZl9Smar9k0=',
base64_encode($alice->getPublicKey(DiffieHellman::FORMAT_BINARY)));
$this->assertEquals('AL/KbggWh3XIdLLcZpMkv7Gb2R8geX9AFZKKQEOcCkmSMhBx3Bp+w11x9ruFnQi/pDK/AXEacUQSdEw8H2T6bud5xTRI1HfNVpX2ipPeXuGvwwuHYZXwZEDHm5XqG4XnUDxQKOxfUpBze7ky2v4JcRvt0/Q02/opHDlT6B9wNRuo',
$this->assertEquals('v8puCBaHdch0stxmkyS/sZvZHyB5f0AVkopAQ5wKSZIyEHHcGn7DXXH2u4WdCL+kMr8BcRpxRBJ0TDwfZPpu53nFNEjUd81WlfaKk95e4a/DC4dhlfBkQMebleobhedQPFAo7F9SkHN7uTLa/glxG+3T9DTb+ikcOVPoH3A1G6g=',
base64_encode($bob->getPublicKey(DiffieHellman::FORMAT_BINARY)));

$aliceSecretKey = $alice->computeSecretKey($bob->getPublicKey(DiffieHellman::FORMAT_BINARY), DiffieHellman::FORMAT_BINARY,
Expand Down

0 comments on commit 5c63415

Please sign in to comment.