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

Commit

Permalink
Merge branch 'hotfix/tests-math-crypt' of https://github.com/ezimuel/zf2
Browse files Browse the repository at this point in the history
 into hotfix/tests-5.3.3
  • Loading branch information
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Hash
* @param string $hash
* @param string $data
* @param string $output
* @throws Exception\InvalidArgumentException
* @return string
*/
public static function compute($hash, $data, $output = self::STRING)
Expand Down
1 change: 1 addition & 0 deletions src/Hmac.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Hmac
* @param string $hash
* @param string $data
* @param string $output
* @throws Exception\InvalidArgumentException
* @return string
*/
public static function compute($key, $hash, $data, $output = self::STRING)
Expand Down
1 change: 1 addition & 0 deletions src/Key/Derivation/Pbkdf2.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Pbkdf2
* @param string $salt
* @param integer $iterations The number of iterations
* @param integer $length The output size
* @throws Exception\InvalidArgumentException
* @return string
*/
public static function calc($hash, $password, $salt, $iterations, $length)
Expand Down
1 change: 1 addition & 0 deletions src/Key/Derivation/SaltedS2k.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SaltedS2k
* @param string $password The source password/key
* @param integer $bytes The output size in bytes
* @param string $salt The salt of the algorithm
* @throws Exception\InvalidArgumentException
* @return string
*/
public static function calc($hash, $password, $salt, $bytes)
Expand Down
11 changes: 10 additions & 1 deletion src/Password/Bcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Zend\Math\Math;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Math\Exception as MathException;

/**
* Bcrypt algorithm using crypt() function of PHP
Expand All @@ -37,6 +38,7 @@ class Bcrypt implements PasswordInterface
* Constructor
*
* @param array|Traversable $options
* @throws Exception\InvalidArgumentException
*/
public function __construct($options = array())
{
Expand Down Expand Up @@ -65,12 +67,17 @@ public function __construct($options = array())
* Bcrypt
*
* @param string $password
* @throws Exception\RuntimeException
* @return string
*/
public function create($password)
{
if (empty($this->salt)) {
$salt = Math::randBytes(self::MIN_SALT_SIZE, true);
try {
$salt = Math::randBytes(self::MIN_SALT_SIZE, true);
} catch (MathException\RuntimeException $e) {
throw new Exception\RuntimeException($e->getMessage());
}
} else {
$salt = $this->salt;
}
Expand Down Expand Up @@ -98,6 +105,7 @@ public function verify($password, $hash)
* Set the cost parameter
*
* @param integer|string $cost
* @throws Exception\InvalidArgumentException
* @return Bcrypt
*/
public function setCost($cost)
Expand Down Expand Up @@ -128,6 +136,7 @@ public function getCost()
* Set the salt value
*
* @param string $salt
* @throws Exception\InvalidArgumentException
* @return Bcrypt
*/
public function setSalt($salt)
Expand Down
45 changes: 27 additions & 18 deletions src/Symmetric/Mcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,38 +70,40 @@ class Mcrypt implements SymmetricInterface
* @var array
*/
protected $supportedAlgos = array(
'aes' => MCRYPT_RIJNDAEL_128,
'blowfish' => MCRYPT_BLOWFISH,
'des' => MCRYPT_DES,
'3des' => MCRYPT_TRIPLEDES,
'tripledes' => MCRYPT_TRIPLEDES,
'cast-128' => MCRYPT_CAST_128,
'cast-256' => MCRYPT_CAST_256,
'rijndael-128' => MCRYPT_RIJNDAEL_128,
'rijndael-192' => MCRYPT_RIJNDAEL_192,
'rijndael-256' => MCRYPT_RIJNDAEL_256,
'saferplus' => MCRYPT_SAFERPLUS,
'serpent' => MCRYPT_SERPENT,
'twofish' => MCRYPT_TWOFISH
'aes' => 'rijndael-128',
'blowfish' => 'blowfish',
'des' => 'des',
'3des' => 'tripledes',
'tripledes' => 'tripledes',
'cast-128' => 'cast-128',
'cast-256' => 'cast-256',
'rijndael-128' => 'rijndael-128',
'rijndael-192' => 'rijndael-192',
'rijndael-256' => 'rijndael-256',
'saferplus' => 'saferplus',
'serpent' => 'serpent',
'twofish' => 'twofish'
);
/**
* Supported encryption modes
*
* @var array
*/
protected $supportedModes = array(
'cbc' => MCRYPT_MODE_CBC,
'cfb' => MCRYPT_MODE_CFB,
'cbc' => 'cbc',
'cfb' => 'cfb',
'ctr' => 'ctr',
'ofb' => MCRYPT_MODE_OFB,
'nofb' => MCRYPT_MODE_NOFB,
'ofb' => 'ofb',
'nofb' => 'nofb',
'ncfb' => 'ncfb'
);

/**
* Constructor
*
* @param array|Traversable $options
* @param array|Traversable $options
* @throws Exception\RuntimeException
* @throws Exception\InvalidArgumentException
*/
public function __construct($options = array())
{
Expand Down Expand Up @@ -181,6 +183,7 @@ public static function getPaddingBroker()
* Set the symmetric cipher broker
*
* @param string|PaddingBroker $broker
* @throws Exception\InvalidArgumentException
* @return void
*/
public static function setPaddingBroker($broker)
Expand Down Expand Up @@ -218,6 +221,7 @@ public function getKeySize()
* Set the encryption key
*
* @param string $key
* @throws Exception\InvalidArgumentException
* @return Mcrypt
*/
public function setKey($key)
Expand Down Expand Up @@ -246,6 +250,7 @@ public function getKey()
* Set the encryption algorithm (cipher)
*
* @param string $algo
* @throws Exception\InvalidArgumentException
* @return Mcrypt
*/
public function setAlgorithm($algo)
Expand Down Expand Up @@ -295,6 +300,7 @@ public function getPadding()
* Encrypt
*
* @param string $data
* @throws Exception\InvalidArgumentException
* @return string
*/
public function encrypt($data)
Expand Down Expand Up @@ -330,6 +336,7 @@ public function encrypt($data)
* Decrypt
*
* @param string $data
* @throws Exception\InvalidArgumentException
* @return string
*/
public function decrypt($data)
Expand Down Expand Up @@ -381,6 +388,7 @@ public function getSupportedAlgorithms()
* Set the salt (IV)
*
* @param string $salt
* @throws Exception\InvalidArgumentException
* @return Mcrypt
*/
public function setSalt($salt)
Expand Down Expand Up @@ -411,6 +419,7 @@ public function getSalt()
* Set the cipher mode
*
* @param string $mode
* @throws Exception\InvalidArgumentException
* @return Mcrypt
*/
public function setMode($mode)
Expand Down
8 changes: 7 additions & 1 deletion test/Password/BcryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use Zend\Crypt\Password\Bcrypt;
use Zend\Config\Config;
use Zend\Crypt\Password\Exception;

/**
* @category Zend
Expand All @@ -45,7 +46,12 @@ class BcryptTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$this->bcrypt = new Bcrypt();
try {
$this->bcrypt = new Bcrypt();
$password = $this->bcrypt->create('test');
} catch (Exception\RuntimeException $e) {
$this->markTestSkipped('This system doesn\'t support strong random numbers, I cannot run BcryptTest');
}
$this->salt = '1234567890123456';
$this->password = 'test';
$this->bcryptPassword = '$2a$14$MTIzNDU2Nzg5MDEyMzQ1NeWUUefVlefsTbFhsbqKFv/vPSZBrSFVm';
Expand Down

0 comments on commit 6119a19

Please sign in to comment.