From d84521532def4074b39c3de48207e87129d78326 Mon Sep 17 00:00:00 2001 From: "Jake A. Smith" Date: Thu, 26 Feb 2015 14:31:11 -0600 Subject: [PATCH] Added EncryptionTrait and CipherInterface with tests --- lib/CipherInterface.php | 24 +++++++++ lib/Utilities/EncryptionTrait.php | 73 ++++++++++++++++++++++++++ tests/Utilities/EncryptionTest.php | 84 ++++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 lib/CipherInterface.php create mode 100644 lib/Utilities/EncryptionTrait.php create mode 100644 tests/Utilities/EncryptionTest.php diff --git a/lib/CipherInterface.php b/lib/CipherInterface.php new file mode 100644 index 0000000..c78efe8 --- /dev/null +++ b/lib/CipherInterface.php @@ -0,0 +1,24 @@ +cipher = $cipher; + } + + protected function encrypt($plain_string) + { + return $this->getCipher()->encrypt($plain_string); + } + + protected function decrypt($encrypted_string) + { + return $this->getCipher()->decrypt($encrypted_string); + } + + protected function defineEncrypted($app_name, $ext_name) + { + $to_app = function ($encrypted_string) { + return $this->decrypt($encrypted_string); + }; + $to_ext = function ($plain_string) { + return $this->encrypt($plain_string); + }; + $this->define($app_name, $ext_name, $to_app, $to_ext); + } + + protected function defineSerializedEncrypted($app_name, $ext_name) + { + $to_app = function ($encrypted_string) { + return unserialize($this->decrypt($encrypted_string)); + }; + $to_ext = function ($plain_string) { + return $this->encrypt(serialize($plain_string)); + }; + $this->define($app_name, $ext_name, $to_app, $to_ext); + } + + private function getCipher() + { + if (empty($this->cipher)) { + throw new \RuntimeException( + 'You must supply a cipher before using encryption. ' + . 'See ' . get_class() + ); + } + + return $this->cipher; + } +} diff --git a/tests/Utilities/EncryptionTest.php b/tests/Utilities/EncryptionTest.php new file mode 100644 index 0000000..33b05e0 --- /dev/null +++ b/tests/Utilities/EncryptionTest.php @@ -0,0 +1,84 @@ +assertSame( + 'abc', + $this->transformer->toApp('YWJj', 'ext_encrypted') + ); + } + + public function testEncryptedToExt() + { + $this->assertSame( + 'YWJj', + $this->transformer->toExt('abc', 'app_encrypted') + ); + } + + public function testSerializedEncryptedToApp() + { + $this->assertSame( + 'abc', + $this->transformer->toApp('czozOiJhYmMiOw==', 'ext_serialized_encrypted') + ); + } + + public function testSerializedEncryptedToExt() + { + $this->assertSame( + 'czozOiJhYmMiOw==', + $this->transformer->toExt('abc', 'app_serialized_encrypted') + ); + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage You must supply a cipher before using encryption. See Aol\Transformers\Tests\Utilities\EncryptionTransformer + */ + public function testEncryptionWithoutCipherShouldThrowException() + { + (new EncryptionTransformer())->toApp('123', 'ext_encrypted'); + } + + protected function setUp() + { + $this->transformer = new EncryptionTransformer; + $this->transformer->setCipher(new EncryptionCipher); + } +} + +class EncryptionTransformer extends Transformer +{ + use EncryptionTrait; + + protected function definitions() + { + $this->defineEncrypted('app_encrypted', 'ext_encrypted'); + $this->defineSerializedEncrypted('app_serialized_encrypted', 'ext_serialized_encrypted'); + } +} + +class EncryptionCipher implements CipherInterface +{ + function encrypt($plain_text) + { + return base64_encode($plain_text); + } + + function decrypt($encrypted_text) + { + return base64_decode($encrypted_text); + } +}