diff --git a/src/Cipher.php b/src/Cipher.php index a476d8e..12f9985 100644 --- a/src/Cipher.php +++ b/src/Cipher.php @@ -9,7 +9,8 @@ * * @package Athens\Encryption */ -class Cipher { +class Cipher +{ const IV_SIZE = 16; const ENCRYPTION_METHOD = "aes-256-cbc"; @@ -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); } @@ -55,28 +58,41 @@ 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 + ); } @@ -84,8 +100,9 @@ public function decrypt($encryptedMessage) { * @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)); @@ -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. ' @@ -110,10 +128,11 @@ 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().' @@ -121,7 +140,4 @@ public static function getInstance() { } return self::$instance; } - - - } diff --git a/src/EncryptionBehavior.php b/src/EncryptionBehavior.php index f697b9f..b9a5f42 100644 --- a/src/EncryptionBehavior.php +++ b/src/EncryptionBehavior.php @@ -9,7 +9,8 @@ * * @package Athens\Encryption */ -class EncryptionBehavior extends Behavior { +class EncryptionBehavior extends Behavior +{ /** @var array */ protected $parameters = [ @@ -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); } } @@ -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; } } @@ -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() @@ -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() @@ -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('); @@ -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); } @@ -330,5 +335,4 @@ protected function addDecryptionToGetter(&$script, $columnPhpName) $script = substr_replace($script, $content, $insertionStart, $insertionLength); } - }