Skip to content

Commit

Permalink
Create Encryptor class to fix issues with null and empty data been de…
Browse files Browse the repository at this point in the history
  • Loading branch information
bobfloats committed May 9, 2016
1 parent 9467766 commit f311c38
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/config/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ vmelnik_doctrine_encrypt:
encryptor: aes256
# If you want, you can use your own Encryptor. Encryptor must implements EncryptorInterface interface
# Default: VMelnik\DoctrineEncryptBundle\Encryptors\AES256Encryptor
#encryptor_class: ~
encryptor_class: VersionControl\GitControlBundle\Encryptors\AES256Encryptor
# You can optionally provide a service as an encryptor instead of specifying a class. The service
# must implement EncryptorInterface. You do not need to provide encryptor_class if you provide the service.
#encryptor_service: ~
Expand Down
60 changes: 60 additions & 0 deletions src/VersionControl/GitControlBundle/Encryptors/AES256Encryptor.php
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
)));
}

}

0 comments on commit f311c38

Please sign in to comment.