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

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 51 changed files with 1,419 additions and 348 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.4-dev",
"dev-develop": "2.5-dev"
"dev-master": "2.2-dev",
"dev-develop": "2.3-dev"
}
},
"autoload-dev": {
Expand Down
107 changes: 62 additions & 45 deletions src/BlockCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt;
Expand All @@ -18,9 +17,6 @@

/**
* Encrypt using a symmetric cipher then authenticate using HMAC (SHA-256)
*
* @category Zend
* @package Zend_Crypt
*/
class BlockCipher
{
Expand Down Expand Up @@ -48,32 +44,32 @@ class BlockCipher
protected $hash = 'sha256';

/**
* Salt (IV)
* Check if the salt has been set
*
* @var string
* @var bool
*/
protected $salt;
protected $saltSetted = false;

/**
* The output is binary?
*
* @var boolean
* @var bool
*/
protected $binaryOutput = false;

/**
* User's key
* Number of iterations for Pbkdf2
*
* @var string
*/
protected $key;
protected $keyIteration = 5000;

/**
* Number of iterations for Pbkdf2
* Key
*
* @var string
*/
protected $keyIteration = 5000;
protected $key;

/**
* Constructor
Expand All @@ -88,15 +84,16 @@ public function __construct(SymmetricInterface $cipher)
/**
* Factory.
*
* @param string $adapter
* @param array $options
* @param string $adapter
* @param array $options
* @return BlockCipher
*/
public static function factory($adapter, $options = array())
{
$plugins = static::getSymmetricPluginManager();
$adapter = $plugins->get($adapter, (array) $options);
return new self($adapter);

return new static($adapter);
}

/**
Expand All @@ -116,7 +113,7 @@ public static function getSymmetricPluginManager()
/**
* Set the symmetric cipher plugin manager
*
* @param string|SymmetricPluginManager $plugins
* @param string|SymmetricPluginManager $plugins
* @throws Exception\InvalidArgumentException
*/
public static function setSymmetricPluginManager($plugins)
Expand Down Expand Up @@ -165,19 +162,20 @@ public function getCipher()
/**
* Set the number of iterations for Pbkdf2
*
* @param integer $num
* @param int $num
* @return BlockCipher
*/
public function setKeyIteration($num)
{
$this->keyIteration = (integer)$num;
$this->keyIteration = (int) $num;

return $this;
}

/**
* Get the number of iterations for Pbkdf2
*
* @return integer
* @return int
*/
public function getKeyIteration()
{
Expand All @@ -187,45 +185,59 @@ public function getKeyIteration()
/**
* Set the salt (IV)
*
* @param string $salt
* @param string $salt
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
public function setSalt($salt)
{
if (empty($salt)) {
throw new Exception\InvalidArgumentException("The salt (IV) cannot be empty");
try {
$this->cipher->setSalt($salt);
} catch (Symmetric\Exception\InvalidArgumentException $e) {
throw new Exception\InvalidArgumentException("The salt is not valid: " . $e->getMessage());
}
$this->salt = $salt;
$this->saltSetted = true;

return $this;
}

/**
* Get the salt (IV)
* Get the salt (IV) according to the size requested by the algorithm
*
* @return string
*/
public function getSalt()
{
return $this->salt;
return $this->cipher->getSalt();
}

/**
* Get the original salt value
*
* @return string
*/
public function getOriginalSalt()
{
return $this->cipher->getOriginalSalt();
}

/**
* Enable/disable the binary output
*
* @param boolean $value
* @param bool $value
* @return BlockCipher
*/
public function setBinaryOutput($value)
{
$this->binaryOutput = (boolean)$value;
$this->binaryOutput = (bool) $value;

return $this;
}

/**
* Get the value of binary output
*
* @return boolean
* @return bool
*/
public function getBinaryOutput()
{
Expand All @@ -235,7 +247,7 @@ public function getBinaryOutput()
/**
* Set the encryption/decryption key
*
* @param string $key
* @param string $key
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -245,6 +257,7 @@ public function setKey($key)
throw new Exception\InvalidArgumentException('The key cannot be empty');
}
$this->key = $key;

return $this;
}

Expand All @@ -261,7 +274,7 @@ public function getKey()
/**
* Set algorithm of the symmetric cipher
*
* @param string $algo
* @param string $algo
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -275,19 +288,21 @@ public function setCipherAlgorithm($algo)
} catch (Symmetric\Exception\InvalidArgumentException $e) {
throw new Exception\InvalidArgumentException($e->getMessage());
}

return $this;
}

/**
* Get the cipher algorithm
*
* @return string|boolean
* @return string|bool
*/
public function getCipherAlgorithm()
{
if (!empty($this->cipher)) {
return $this->cipher->getAlgorithm();
}

return false;
}

Expand All @@ -301,13 +316,14 @@ public function getCipherSupportedAlgorithms()
if (!empty($this->cipher)) {
return $this->cipher->getSupportedAlgorithms();
}

return array();
}

/**
* Set the hash algorithm for HMAC authentication
*
* @param string $hash
* @param string $hash
* @return BlockCipher
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -319,6 +335,7 @@ public function setHashAlgorithm($hash)
);
}
$this->hash = $hash;

return $this;
}

Expand All @@ -335,7 +352,7 @@ public function getHashAlgorithm()
/**
* Encrypt then authenticate using HMAC
*
* @param string $data
* @param string $data
* @return string
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -344,23 +361,21 @@ public function encrypt($data)
if (empty($data)) {
throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty');
}
if (empty($this->key)) {
throw new Exception\InvalidArgumentException('No key specified for the encryption');
}
if (empty($this->cipher)) {
throw new Exception\InvalidArgumentException('No symmetric cipher specified');
}
if (empty($this->key)) {
throw new Exception\InvalidArgumentException('No key specified for the encryption');
}
$keySize = $this->cipher->getKeySize();
$salt = $this->getSalt();
// generate a random salt (IV) if empty
if (empty($salt)) {
$salt = Rand::getBytes($this->cipher->getSaltSize(), true);
// generate a random salt (IV) if the salt has not been set
if (!$this->saltSetted) {
$this->cipher->setSalt(Rand::getBytes($this->cipher->getSaltSize(), true));
}
$this->cipher->setSalt($salt);
// generate the encryption key and the HMAC key for the authentication
$hash = Pbkdf2::calc(self::KEY_DERIV_HMAC,
$this->getKey(),
$this->cipher->getSalt(),
$this->getSalt(),
$this->keyIteration,
$keySize * 2);
// set the encryption key
Expand All @@ -376,14 +391,15 @@ public function encrypt($data)
if (!$this->binaryOutput) {
$ciphertext = base64_encode($ciphertext);
}

return $hmac . $ciphertext;
}

/**
* Decrypt
*
* @param string $data
* @return string|boolean
* @param string $data
* @return string|bool
* @throws Exception\InvalidArgumentException
*/
public function decrypt($data)
Expand Down Expand Up @@ -424,6 +440,7 @@ public function decrypt($data)
if (!Utils::compareStrings($hmacNew, $hmac)) {
return false;
}

return $this->cipher->decrypt($ciphertext);
}
}
7 changes: 1 addition & 6 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Exception;

/**
* @category Zend
* @package Zend_Crypt
*/
interface ExceptionInterface
{
}
7 changes: 1 addition & 6 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Exception;

/**
* Invalid argument exception
*
* @category Zend
* @package Zend_Crypt
* @subpackage Exception
*/
class InvalidArgumentException
extends \InvalidArgumentException
Expand Down
7 changes: 1 addition & 6 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Crypt
*/

namespace Zend\Crypt\Exception;

/**
* Runtime argument exception
*
* @category Zend
* @package Zend_Crypt
* @subpackage Exception
*/
class RuntimeException
extends \RuntimeException
Expand Down
Loading

0 comments on commit 414cbca

Please sign in to comment.