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

Commit

Permalink
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/BlockCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,19 @@ public function getPbkdf2HashAlgorithm()
public function encrypt($data)
{
// 0 (as integer), 0.0 (as float) & '0' (as string) will return false, though these should be allowed
if (!is_string($data) || $data === '') {
// Must be a string, integer, or float in order to encrypt
if ((is_string($data) && $data === '')
|| is_array($data)
|| is_object($data)
) {
throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty');
}

// Cast to string prior to encrypting
if (!is_string($data)) {
$data = (string) $data;
}

if (empty($this->cipher)) {
throw new Exception\InvalidArgumentException('No symmetric cipher specified');
}
Expand Down
3 changes: 2 additions & 1 deletion src/Symmetric/Mcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ public function getPadding()
*/
public function encrypt($data)
{
if (empty($data)) {
// Cannot encrypt empty string
if (!is_string($data) || $data === '') {
throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty');
}
if (null === $this->getKey()) {
Expand Down
26 changes: 26 additions & 0 deletions test/BlockCipherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,32 @@ public function testEncryptDecryptUsingBinary()
}
}

public function zeroValuesProvider()
{
return array(
'"0"' => array(0),
'"0.0"' => array(0.0),
'"0"' => array('0'),
);
}

/**
* @dataProvider zeroValuesProvider
*/
public function testEncryptDecryptUsingZero($value)
{
$this->blockCipher->setKey('test');
$this->blockCipher->setKeyIteration(1000);
foreach ($this->blockCipher->getCipherSupportedAlgorithms() as $algo) {
$this->blockCipher->setCipherAlgorithm($algo);

$encrypted = $this->blockCipher->encrypt($value);
$this->assertTrue(!empty($encrypted));
$decrypted = $this->blockCipher->decrypt($encrypted);
$this->assertEquals($value, $decrypted);
}
}

public function testDecryptAuthFail()
{
$this->blockCipher->setKey('test');
Expand Down

0 comments on commit 8eb6c65

Please sign in to comment.