3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Filter\Encrypt
;
13 use Zend\Filter\Compress
;
14 use Zend\Filter\Decompress
;
15 use Zend\Filter\Exception
;
16 use Zend\Stdlib\ArrayUtils
;
17 use Zend\Crypt\BlockCipher
as CryptBlockCipher
;
18 use Zend\Crypt\Exception
as CryptException
;
19 use Zend\Crypt\Symmetric\Exception
as SymmetricException
;
22 * Encryption adapter for Zend\Crypt\BlockCipher
24 class BlockCipher
implements EncryptionAlgorithmInterface
27 * Definitions for encryption
29 * 'key' => encryption key string
30 * 'key_iteration' => the number of iterations for the PBKDF2 key generation
31 * 'algorithm => cipher algorithm to use
32 * 'hash' => algorithm to use for the authentication
33 * 'vector' => initialization vector
36 protected $encryption = array(
37 'key_iteration' => 5000,
47 protected $blockCipher;
50 * Internal compression
54 protected $compression;
59 * @param string|array|Traversable $options Encryption Options
60 * @throws Exception\RuntimeException
61 * @throws Exception\InvalidArgumentException
63 public function __construct($options)
66 $this->blockCipher
= CryptBlockCipher
::factory('mcrypt', $this->encryption
);
67 } catch (SymmetricException\RuntimeException
$e) {
68 throw new Exception\
RuntimeException('The BlockCipher cannot be used without the Mcrypt extension');
71 if ($options instanceof Traversable
) {
72 $options = ArrayUtils
::iteratorToArray($options);
73 } elseif (is_string($options)) {
74 $options = array('key' => $options);
75 } elseif (!is_array($options)) {
76 throw new Exception\
InvalidArgumentException('Invalid options argument provided to filter');
79 if (array_key_exists('compression', $options)) {
80 $this->setCompression($options['compression']);
81 unset($options['compress']);
84 $this->setEncryption($options);
88 * Returns the set encryption options
92 public function getEncryption()
94 return $this->encryption
;
98 * Sets new encryption options
100 * @param string|array $options Encryption options
102 * @throws Exception\InvalidArgumentException
104 public function setEncryption($options)
106 if (is_string($options)) {
107 $this->blockCipher
->setKey($options);
108 $this->encryption
['key'] = $options;
112 if (!is_array($options)) {
113 throw new Exception\
InvalidArgumentException('Invalid options argument provided to filter');
116 $options = $options +
$this->encryption
;
118 if (isset($options['key'])) {
119 $this->blockCipher
->setKey($options['key']);
122 if (isset($options['algorithm'])) {
124 $this->blockCipher
->setCipherAlgorithm($options['algorithm']);
125 } catch (CryptException\InvalidArgumentException
$e) {
126 throw new Exception\
InvalidArgumentException("The algorithm '{$options['algorithm']}' is not supported");
130 if (isset($options['hash'])) {
132 $this->blockCipher
->setHashAlgorithm($options['hash']);
133 } catch (CryptException\InvalidArgumentException
$e) {
134 throw new Exception\
InvalidArgumentException("The algorithm '{$options['hash']}' is not supported");
138 if (isset($options['vector'])) {
139 $this->setVector($options['vector']);
142 if (isset($options['key_iteration'])) {
143 $this->blockCipher
->setKeyIteration($options['key_iteration']);
146 $this->encryption
= $options;
152 * Returns the initialization vector
156 public function getVector()
158 return $this->encryption
['vector'];
162 * Set the inizialization vector
164 * @param string $vector
166 * @throws Exception\InvalidArgumentException
168 public function setVector($vector)
171 $this->blockCipher
->setSalt($vector);
172 } catch (CryptException\InvalidArgumentException
$e) {
173 throw new Exception\
InvalidArgumentException($e->getMessage());
175 $this->encryption
['vector'] = $vector;
180 * Set the encryption key
184 * @throws Exception\InvalidArgumentException
186 public function setKey($key)
189 $this->blockCipher
->setKey($key);
190 } catch (CryptException\InvalidArgumentException
$e) {
191 throw new Exception\
InvalidArgumentException($e->getMessage());
193 $this->encryption
['key'] = $key;
198 * Get the encryption key
202 public function getKey()
204 return $this->encryption
['key'];
208 * Returns the compression
212 public function getCompression()
214 return $this->compression
;
218 * Sets a internal compression for values to encrypt
220 * @param string|array $compression
223 public function setCompression($compression)
225 if (is_string($this->compression
)) {
226 $compression = array('adapter' => $compression);
229 $this->compression
= $compression;
234 * Defined by Zend\Filter\FilterInterface
236 * Encrypts $value with the defined settings
238 * @param string $value The content to encrypt
239 * @throws Exception\InvalidArgumentException
240 * @return string The encrypted content
242 public function encrypt($value)
244 // compress prior to encryption
245 if (!empty($this->compression
)) {
246 $compress = new Compress($this->compression
);
247 $value = $compress($value);
251 $encrypted = $this->blockCipher
->encrypt($value);
252 } catch (CryptException\InvalidArgumentException
$e) {
253 throw new Exception\
InvalidArgumentException($e->getMessage());
259 * Defined by Zend\Filter\FilterInterface
261 * Decrypts $value with the defined settings
263 * @param string $value Content to decrypt
264 * @return string The decrypted content
266 public function decrypt($value)
268 $decrypted = $this->blockCipher
->decrypt($value);
270 // decompress after decryption
271 if (!empty($this->compression
)) {
272 $decompress = new Decompress($this->compression
);
273 $decrypted = $decompress($decrypted);
280 * Returns the adapter name
284 public function toString()
286 return 'BlockCipher';