Skip to content
This repository was archived by the owner on Jul 11, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/CipherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Aol\Transformers;

interface CipherInterface
{
/**
* Encrypt the given text.
*
* @param string $plain_text The text to encrypt.
*
* @return string The encrypted text.
*/
function encrypt($plain_text);

/**
* Decrypt the given text.
*
* @param string $encrypted_text The text to decrypt.
*
* @return string The decrypted text.
*/
function decrypt($encrypted_text);
}
73 changes: 73 additions & 0 deletions lib/Utilities/EncryptionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
namespace Aol\Transformers\Utilities;

use Aol\Transformers\CipherInterface;

trait EncryptionTrait
{
/** @var \Aol\Transformers\CipherInterface */
private $cipher;

/**
* This trait requires the define method.
*
* @see \Aol\Transformers\Transformer
*/
abstract public function define(
$app_name,
$ext_name,
callable $app_func = null,
callable $ext_func = null,
$app_args = [],
$ext_args = []
);

public function setCipher(CipherInterface $cipher)
{
$this->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;
}
}
84 changes: 84 additions & 0 deletions tests/Utilities/EncryptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Aol\Transformers\Tests\Utilities;

use Aol\Transformers\CipherInterface;
use Aol\Transformers\Transformer;
use Aol\Transformers\Utilities\EncryptionTrait;

class EncryptionTest extends \PHPUnit_Framework_TestCase
{
/** @var EncryptionTransformer */
private $transformer;

public function testEncryptedToApp()
{
$this->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);
}
}