Skip to content

Commit

Permalink
Satisfy coding standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
JASchilz committed Feb 26, 2016
1 parent 807ba34 commit 00e0929
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
64 changes: 40 additions & 24 deletions src/Cipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
*
* @package Athens\Encryption
*/
class Cipher {
class Cipher
{

const IV_SIZE = 16;
const ENCRYPTION_METHOD = "aes-256-cbc";
Expand All @@ -23,17 +24,19 @@ class Cipher {
/**
* @param string $passphrase
*/
protected function __construct($passphrase) {
protected function __construct($passphrase)
{
$this->passphrase = $passphrase;
}

/**
* Converts a plain-text string into an encrypted string
*
* @param string $string plain-text to encrypt
* @return string the encrypted string
* @param string $string Plain-text to encrypt.
* @return string The encrypted string.
*/
public function encrypt($string) {
public function encrypt($string)
{
$iv = mcrypt_create_iv(self::IV_SIZE, MCRYPT_RAND);
return $this->doEncrypt($string, $iv);
}
Expand All @@ -55,37 +58,51 @@ public function encrypt($string) {
* This method is employed for encrypting Propel columns that are designated as 'searchable'
* in the included EncryptionBehavior.
*
* @param string $string plain-text to encrypt
* @return string the encrypted string
* @param string $string Plain-text to encrypt.
* @return string The encrypted string.
*/
public function deterministicEncrypt($string) {
public function deterministicEncrypt($string)
{
$iv = str_repeat("0", self::IV_SIZE);
return $this->doEncrypt($string, $iv);
}


protected function doEncrypt($string, $iv) {
/**
* @param string $string
* @param string $iv
* @return string
*/
protected function doEncrypt($string, $iv)
{
return $iv.openssl_encrypt($string, self::ENCRYPTION_METHOD, $this->passphrase, 0, $iv);
}

/**
* Converts an encrypted string into a plain-text string
*
* @param string $encryptedMessage the encrypted string
* @return string the plaint-text string
* @param string $encryptedMessage The encrypted string.
* @return string The plaint-text string.
*/
public function decrypt($encryptedMessage) {
public function decrypt($encryptedMessage)
{
$iv = substr($encryptedMessage, 0, self::IV_SIZE);
return openssl_decrypt(substr($encryptedMessage, self::IV_SIZE), self::ENCRYPTION_METHOD, $this->passphrase, 0, $iv);
return openssl_decrypt(
substr($encryptedMessage, self::IV_SIZE),
self::ENCRYPTION_METHOD,
$this->passphrase,
0,
$iv
);

}

/**
* @param resource $encryptedStream
* @return null|string
*/
public function decryptStream($encryptedStream) {
if (is_null($encryptedStream)) {
public function decryptStream($encryptedStream)
{
if ($encryptedStream === null) {
return null;
} else {
return self::decrypt(stream_get_contents($encryptedStream, -1, 0));
Expand All @@ -98,8 +115,9 @@ public function decryptStream($encryptedStream) {
* @throws \Exception If you attempt to initialize the cipher more than one time
* in a page-load via ::createInstance.
*/
public static function createInstance($passphrase) {
if (!empty(self::$instance)) {
public static function createInstance($passphrase)
{
if (self::$instance !== null) {
throw new \Exception(
'Cipher::createInstance() called more than once. ' .
'Only one cipher instance may be created. '
Expand All @@ -110,18 +128,16 @@ public static function createInstance($passphrase) {

/**
* @return Cipher
* @throws \Exception if ::getInstance is called before cipher is initialized via ::createInstance
* @throws \Exception if ::getInstance is called before cipher is initialized via ::createInstance.
*/
public static function getInstance() {
if (empty(self::$instance)) {
public static function getInstance()
{
if (self::$instance === null) {
throw new \Exception(
'Cipher::getInstance() called before initialization. ' .
'Call Cipher::createInstance($passphrase) before ::getInstance().'
);
}
return self::$instance;
}



}
22 changes: 13 additions & 9 deletions src/EncryptionBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
*
* @package Athens\Encryption
*/
class EncryptionBehavior extends Behavior {
class EncryptionBehavior extends Behavior
{

/** @var array */
protected $parameters = [
Expand Down Expand Up @@ -57,7 +58,7 @@ public function tableMapFilter(&$script)
foreach ($this->getColumnRealNames() as $realColumnName) {
static::insertEncryptedColumnName($script, $realColumnName);

if ($this->isSearchable()) {
if ($this->isSearchable() === true) {
static::insertSearchableEncryptedColumnName($script, $realColumnName);
}
}
Expand Down Expand Up @@ -136,7 +137,7 @@ protected function getColumnNames()
{
$columnNames = [];
foreach ($this->getParameters() as $key => $columnName) {
if (strpos($key, "column_name") !== false && $columnName) {
if (strpos($key, "column_name") !== false && empty($columnName) !== true) {
$columnNames[] = $columnName;
}
}
Expand All @@ -151,7 +152,7 @@ protected function getColumnPhpNames()
$table = $this->getTable();

return array_map(
function($columnName) use ($table) {
function ($columnName) use ($table) {
return $table->getColumn($columnName)->getPhpName();
},
$this->getColumnNames()
Expand All @@ -166,7 +167,7 @@ protected function getColumnRealNames()
$tableName = $this->getTable()->getName();

return array_map(
function($columnName) use ($tableName) {
function ($columnName) use ($tableName) {
return "$tableName.$columnName";
},
$this->getColumnNames()
Expand Down Expand Up @@ -258,7 +259,8 @@ protected static function insertEncryptedColumnsDeclaration(&$script, $position)
* @param string $realColumnName
* @return void
*/
public static function insertEncryptedColumnName(&$script, $realColumnName) {
public static function insertEncryptedColumnName(&$script, $realColumnName)
{
$insertContent = "\n '$realColumnName', ";

$insertLocation = strpos($script, '$encryptedColumns = array(') + strlen('$encryptedColumns = array(');
Expand All @@ -270,10 +272,13 @@ public static function insertEncryptedColumnName(&$script, $realColumnName) {
* @param string $realColumnName
* @return void
*/
public static function insertSearchableEncryptedColumnName(&$script, $realColumnName) {
public static function insertSearchableEncryptedColumnName(&$script, $realColumnName)
{
$insertContent = "\n '$realColumnName', ";

$insertLocation = strpos($script, '$encryptedSearchableColumns = array(') + strlen('$encryptedSearchableColumns = array(');
$insertLocation = strpos($script, '$encryptedSearchableColumns = array(')
+ strlen('$encryptedSearchableColumns = array(');

$script = substr_replace($script, $insertContent, $insertLocation, 0);
}

Expand Down Expand Up @@ -330,5 +335,4 @@ protected function addDecryptionToGetter(&$script, $columnPhpName)

$script = substr_replace($script, $content, $insertionStart, $insertionLength);
}

}

0 comments on commit 00e0929

Please sign in to comment.