Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Filter / Encrypt / BlockCipher.php
blob1415ed7a2cdd69c85b15a3e41a627d72e701dbdf
1 <?php
2 /**
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
8 */
10 namespace Zend\Filter\Encrypt;
12 use Traversable;
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;
21 /**
22 * Encryption adapter for Zend\Crypt\BlockCipher
24 class BlockCipher implements EncryptionAlgorithmInterface
26 /**
27 * Definitions for encryption
28 * array(
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
34 * )
36 protected $encryption = array(
37 'key_iteration' => 5000,
38 'algorithm' => 'aes',
39 'hash' => 'sha256',
42 /**
43 * BlockCipher
45 * @var BlockCipher
47 protected $blockCipher;
49 /**
50 * Internal compression
52 * @var array
54 protected $compression;
56 /**
57 * Class constructor
59 * @param string|array|Traversable $options Encryption Options
60 * @throws Exception\RuntimeException
61 * @throws Exception\InvalidArgumentException
63 public function __construct($options)
65 try {
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);
87 /**
88 * Returns the set encryption options
90 * @return array
92 public function getEncryption()
94 return $this->encryption;
97 /**
98 * Sets new encryption options
100 * @param string|array $options Encryption options
101 * @return self
102 * @throws Exception\InvalidArgumentException
104 public function setEncryption($options)
106 if (is_string($options)) {
107 $this->blockCipher->setKey($options);
108 $this->encryption['key'] = $options;
109 return $this;
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'])) {
123 try {
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'])) {
131 try {
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;
148 return $this;
152 * Returns the initialization vector
154 * @return string
156 public function getVector()
158 return $this->encryption['vector'];
162 * Set the inizialization vector
164 * @param string $vector
165 * @return self
166 * @throws Exception\InvalidArgumentException
168 public function setVector($vector)
170 try {
171 $this->blockCipher->setSalt($vector);
172 } catch (CryptException\InvalidArgumentException $e) {
173 throw new Exception\InvalidArgumentException($e->getMessage());
175 $this->encryption['vector'] = $vector;
176 return $this;
180 * Set the encryption key
182 * @param string $key
183 * @return self
184 * @throws Exception\InvalidArgumentException
186 public function setKey($key)
188 try {
189 $this->blockCipher->setKey($key);
190 } catch (CryptException\InvalidArgumentException $e) {
191 throw new Exception\InvalidArgumentException($e->getMessage());
193 $this->encryption['key'] = $key;
194 return $this;
198 * Get the encryption key
200 * @return string
202 public function getKey()
204 return $this->encryption['key'];
208 * Returns the compression
210 * @return array
212 public function getCompression()
214 return $this->compression;
218 * Sets a internal compression for values to encrypt
220 * @param string|array $compression
221 * @return self
223 public function setCompression($compression)
225 if (is_string($this->compression)) {
226 $compression = array('adapter' => $compression);
229 $this->compression = $compression;
230 return $this;
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);
250 try {
251 $encrypted = $this->blockCipher->encrypt($value);
252 } catch (CryptException\InvalidArgumentException $e) {
253 throw new Exception\InvalidArgumentException($e->getMessage());
255 return $encrypted;
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);
276 return $decrypted;
280 * Returns the adapter name
282 * @return string
284 public function toString()
286 return 'BlockCipher';