-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Encryptor class to fix issues with null and empty data been de…
…crypted. See vmelnik-ukraine/DoctrineEncryptBundle#34
- Loading branch information
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/VersionControl/GitControlBundle/Encryptors/AES256Encryptor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace VersionControl\GitControlBundle\Encryptors; | ||
|
||
use VMelnik\DoctrineEncryptBundle\Encryptors\EncryptorInterface; | ||
|
||
/** | ||
* Class for AES256 encryption | ||
* | ||
* @author Paul Schweppe | ||
* @author Victor Melnik <[email protected]> | ||
*/ | ||
class AES256Encryptor implements EncryptorInterface { | ||
|
||
/** | ||
* Secret key for aes algorythm | ||
* @var string | ||
*/ | ||
private $secretKey; | ||
|
||
/** | ||
* Initialization of encryptor | ||
* @param string $key | ||
*/ | ||
public function __construct($key) { | ||
$this->secretKey = $key; | ||
} | ||
|
||
/** | ||
* Implementation of EncryptorInterface encrypt method | ||
* @param string $data | ||
* @return string | ||
*/ | ||
public function encrypt($data) { | ||
if(is_null($data) || !trim($data)){ | ||
return $data; | ||
} | ||
return trim(base64_encode(mcrypt_encrypt( | ||
MCRYPT_RIJNDAEL_256, $this->secretKey, $data, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND | ||
)))); | ||
} | ||
|
||
/** | ||
* Implementation of EncryptorInterface decrypt method | ||
* @param string $data | ||
* @return string | ||
*/ | ||
function decrypt($data) { | ||
if(is_null($data) || !trim($data)){ | ||
return $data; | ||
} | ||
return trim(mcrypt_decrypt( | ||
MCRYPT_RIJNDAEL_256, $this->secretKey, base64_decode($data), MCRYPT_MODE_ECB, mcrypt_create_iv( | ||
mcrypt_get_iv_size( | ||
MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB | ||
), MCRYPT_RAND | ||
))); | ||
} | ||
|
||
} |