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

Commit

Permalink
Merge branch 'hotfix/3550' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 28 deletions.
32 changes: 29 additions & 3 deletions src/Encrypt/BlockCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ class BlockCipher implements EncryptionAlgorithmInterface
* 'key_iteration' => the number of iterations for the PBKDF2 key generation
* 'algorithm => cipher algorithm to use
* 'hash' => algorithm to use for the authentication
* 'iv' => initialization vector
* 'vector' => initialization vector
* )
*/
protected $encryption = array(
'key' => 'ZendFramework',
'key_iteration' => 5000,
'algorithm' => 'aes',
'hash' => 'sha256',
'vector' => null,
);

/**
Expand Down Expand Up @@ -178,6 +176,34 @@ public function setVector($vector)
return $this;
}

/**
* Set the encryption key
*
* @param string $key
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
public function setKey($key)
{
try {
$this->blockCipher->setKey($key);
} catch (CryptException\InvalidArgumentException $e) {
throw new Exception\InvalidArgumentException($e->getMessage());
}
$this->encryption['key'] = $key;
return $this;
}

/**
* Get the encryption key
*
* @return string
*/
public function getKey()
{
return $this->encryption['key'];
}

/**
* Returns the compression
*
Expand Down
17 changes: 15 additions & 2 deletions test/DecryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,26 @@ public function testBasicMcrypt()
);

$enc = $filter->getEncryption();
$filter->setVector('1234567890123456');
$this->assertEquals('ZendFramework', $enc['key']);
$filter->setKey('1234567890123456');
foreach ($valuesExpected as $input => $output) {
$this->assertNotEquals($output, $filter($input));
}
}

/**
* Ensures that the encryption works fine
*/
public function testDecryptBlockCipher()
{
if (!extension_loaded('mcrypt')) {
$this->markTestSkipped('Mcrypt extension not installed');
}
$decrypt = new DecryptFilter(array('adapter' => 'BlockCipher', 'key' => 'testkey'));
$decrypt->setVector('1234567890123456890');
$decrypted = $decrypt->filter('ec133eb7460682b0020b736ad6d2ef14c35de0f1e5976330ae1dd096ef3b4cb7MTIzNDU2Nzg5MDEyMzQ1NoZvxY1JkeL6TnQP3ug5F0k=');
$this->assertEquals($decrypted, 'test');
}

/**
* Ensures that the filter follows expected behavior
*
Expand Down
32 changes: 18 additions & 14 deletions test/Encrypt/BlockCipherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ public function testBasicBlockCipher()
*/
public function testGetSetVector()
{

$filter = new BlockCipherEncryption(array('key' => 'testkey'));
$filter->setVector('testvect');
$this->assertEquals('testvect', $filter->getVector());
$filter->setVector('1234567890123456');
$this->assertEquals('1234567890123456', $filter->getVector());
}

public function testWrongSizeVector()
{
$this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException');
$output = $filter->encrypt('test');
$filter = new BlockCipherEncryption(array('key' => 'testkey'));
$filter->setVector('testvect');
}

/**
* Ensures that the filter allows default encryption
*
Expand All @@ -74,13 +78,13 @@ public function testGetSetVector()
public function testDefaultEncryption()
{
$filter = new BlockCipherEncryption(array('key' => 'testkey'));
$filter->setVector('testvect');
$filter->setVector('1234567890123456');
$this->assertEquals(
array('key' => 'testkey',
'algorithm' => 'aes',
'vector' => 'testvect',
array('key' => 'testkey',
'algorithm' => 'aes',
'vector' => '1234567890123456',
'key_iteration' => 5000,
'hash' => 'sha256'),
'hash' => 'sha256'),
$filter->getEncryption()
);
}
Expand All @@ -93,16 +97,16 @@ public function testDefaultEncryption()
public function testGetSetEncryption()
{
$filter = new BlockCipherEncryption(array('key' => 'testkey'));
$filter->setVector('testvect');
$filter->setVector('1234567890123456');
$filter->setEncryption(
array('algorithm' => '3des')
);
$this->assertEquals(
array('key' => 'testkey',
'algorithm' => '3des',
'vector' => 'testvect',
array('key' => 'testkey',
'algorithm' => '3des',
'vector' => '1234567890123456',
'key_iteration' => 5000,
'hash' => 'sha256'),
'hash' => 'sha256'),
$filter->getEncryption()
);
}
Expand Down
14 changes: 14 additions & 0 deletions test/EncryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ public function testBasicBlockCipher()
}
}

/**
* Ensures that the encryption works fine
*/
public function testEncryptBlockCipher()
{
if (!extension_loaded('mcrypt')) {
$this->markTestSkipped('Mcrypt extension not installed');
}
$encrypt = new EncryptFilter(array('adapter' => 'BlockCipher', 'key' => 'testkey'));
$encrypt->setVector('1234567890123456890');
$encrypted = $encrypt->filter('test');
$this->assertEquals($encrypted, 'ec133eb7460682b0020b736ad6d2ef14c35de0f1e5976330ae1dd096ef3b4cb7MTIzNDU2Nzg5MDEyMzQ1NoZvxY1JkeL6TnQP3ug5F0k=');
}

/**
* Ensures that the filter follows expected behavior
*
Expand Down
8 changes: 4 additions & 4 deletions test/File/DecryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testBasic()
dirname(__DIR__).'/_files/newencryption.txt',
$filter->getFilename());

$filter->setVector('1234567890123456');
$filter->setKey('1234567890123456');
$filter->filter(dirname(__DIR__).'/_files/encryption.txt');

$filter = new FileDecrypt();
Expand All @@ -70,7 +70,7 @@ public function testBasic()
'Encryption',
file_get_contents(dirname(__DIR__).'/_files/newencryption.txt'));

$filter->setVector('1234567890123456');
$filter->setKey('1234567890123456');
$this->assertEquals(
dirname(__DIR__).'/_files/newencryption.txt',
$filter->filter(dirname(__DIR__).'/_files/newencryption.txt'));
Expand Down Expand Up @@ -116,7 +116,7 @@ public function testEncryptionWithDecryption()
{
$filter = new FileEncrypt();
$filter->setFilename(dirname(__DIR__).'/_files/newencryption.txt');
$filter->setVector('1234567890123456');
$filter->setKey('1234567890123456');
$this->assertEquals(dirname(__DIR__).'/_files/newencryption.txt',
$filter->filter(dirname(__DIR__).'/_files/encryption.txt'));

Expand All @@ -131,7 +131,7 @@ public function testEncryptionWithDecryption()
dirname(__DIR__).'/_files/newencryption2.txt',
$filter->getFilename());

$filter->setVector('1234567890123456');
$filter->setKey('1234567890123456');
$input = $filter->filter(dirname(__DIR__).'/_files/newencryption.txt');
$this->assertEquals(dirname(__DIR__).'/_files/newencryption2.txt', $input);

Expand Down
10 changes: 5 additions & 5 deletions test/File/EncryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testBasic()
dirname(__DIR__).'/_files/newencryption.txt',
$filter->getFilename());

$filter->setVector('1234567890123456');
$filter->setKey('1234567890123456');
$this->assertEquals(dirname(__DIR__).'/_files/newencryption.txt',
$filter->filter(dirname(__DIR__).'/_files/encryption.txt'));

Expand Down Expand Up @@ -97,7 +97,7 @@ public function testEncryptionWithDecryption()
{
$filter = new FileEncrypt();
$filter->setFilename(dirname(__DIR__).'/_files/newencryption.txt');
$filter->setVector('1234567890123456');
$filter->setKey('1234567890123456');
$this->assertEquals(dirname(__DIR__).'/_files/newencryption.txt',
$filter->filter(dirname(__DIR__).'/_files/encryption.txt'));

Expand All @@ -106,7 +106,7 @@ public function testEncryptionWithDecryption()
file_get_contents(dirname(__DIR__).'/_files/newencryption.txt'));

$filter = new FileDecrypt();
$filter->setVector('1234567890123456');
$filter->setKey('1234567890123456');
$input = $filter->filter(dirname(__DIR__).'/_files/newencryption.txt');
$this->assertEquals(dirname(__DIR__).'/_files/newencryption.txt', $input);

Expand All @@ -121,7 +121,7 @@ public function testEncryptionWithDecryption()
public function testNonExistingFile()
{
$filter = new FileEncrypt();
$filter->setVector('1234567890123456');
$filter->setKey('1234567890123456');

$this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'not found');
echo $filter->filter(dirname(__DIR__).'/_files/nofile.txt');
Expand All @@ -133,7 +133,7 @@ public function testNonExistingFile()
public function testEncryptionInSameFile()
{
$filter = new FileEncrypt();
$filter->setVector('1234567890123456');
$filter->setKey('1234567890123456');

copy(dirname(__DIR__).'/_files/encryption.txt', dirname(__DIR__).'/_files/newencryption.txt');
$filter->filter(dirname(__DIR__).'/_files/newencryption.txt');
Expand Down

0 comments on commit ddb5852

Please sign in to comment.