Skip to content
Merged
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"symfony/options-resolver": "^2.7 || ^3.0"
},
"require-dev": {
"defuse/php-encryption": "^2.0",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best encryption package for us?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the best i know, you know a other one?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea. =)

I trust that you have done some research to find a popular and robust package. And also one that you like.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like you can read https://paragonie.com/blog/2016/05/defuse-security-s-php-encryption-library-version-2-0-0-released and if @paragonie-scott recommend it, so it musst be really secure :D

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from libsodium, you'll be hard-pressed to do better. @defuse really knocked it out of the park. :)

"phpunit/phpunit": "^4.0 || ^5.1",
"mockery/mockery": "^0.9",
"cache/integration-tests": "^0.11",
Expand Down
9 changes: 9 additions & 0 deletions src/Adapter/Common/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,19 @@ public function expiresAfter($time)
return $this;
}

/**
* {@inheritdoc}
*/
public function getTags()
{
$this->initialize();

return $this->tags;
}

/**
* {@inheritdoc}
*/
public function addTag($tag)
{
$this->initialize();
Expand All @@ -177,6 +183,9 @@ public function addTag($tag)
return $this;
}

/**
* {@inheritdoc}
*/
public function setTags(array $tags)
{
$this->initialize();
Expand Down
5 changes: 5 additions & 0 deletions src/Encryption/.github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is a READ ONLY repository.

Please make your pull request to https://github.com/php-cache/cache

Thank you for contributing.
2 changes: 2 additions & 0 deletions src/Encryption/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.lock
vendor
22 changes: 22 additions & 0 deletions src/Encryption/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: php
sudo: false

matrix:
include:
- php: 7.0

cache:
directories:
- "$HOME/.composer/cache"

install:
- composer update

script:
- ./vendor/bin/phpunit --coverage-clover=coverage.xml

after_success:
- pip install --user codecov && codecov

notifications:
email: false
7 changes: 7 additions & 0 deletions src/Encryption/Changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Change Log

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## UNRELEASED


137 changes: 137 additions & 0 deletions src/Encryption/EncryptedCachePool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/*
* This file is part of php-cache organization.
*
* (c) 2015-2016 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/


namespace Cache\Encryption;

use Defuse\Crypto\Key;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

/**
* Wrapps a CacheItemInterface with EncryptedItemDecorator.
*
* @author Daniel Bannert <d.bannert@anolilab.de>
*/
class EncryptedCachePool implements CacheItemPoolInterface
{
/**
* @type CacheItemPoolInterface
*/
private $cachePool;

/**
* @type Key
*/
private $key;

/**
* @param CacheItemPoolInterface $cachePool
* @param Key $key
*/
public function __construct(CacheItemPoolInterface $cachePool, Key $key)
{
$this->cachePool = $cachePool;
$this->key = $key;
}

/**
* {@inheritdoc}
*/
public function getItem($key)
{
$item = $this->cachePool->getItem($key);

if (!($item instanceof EncryptedItemDecorator)) {
return new EncryptedItemDecorator($item, $this->key);
}

return $item;
}

/**
* {@inheritdoc}
*/
public function getItems(array $keys = [])
{
return array_map(function (CacheItemInterface $inner) {
if (!($inner instanceof EncryptedItemDecorator)) {
return new EncryptedItemDecorator($inner, $this->key);
}

return $inner;
}, $this->cachePool->getItems($keys));
}

/**
* {@inheritdoc}
*/
public function hasItem($key)
{
return $this->cachePool->hasItem($key);
}

/**
* {@inheritdoc}
*/
public function clear()
{
return $this->cachePool->clear();
}

/**
* {@inheritdoc}
*/
public function deleteItem($key)
{
return $this->cachePool->deleteItem($key);
}

/**
* {@inheritdoc}
*/
public function deleteItems(array $keys)
{
return $this->cachePool->deleteItems($keys);
}

/**
* {@inheritdoc}
*/
public function save(CacheItemInterface $item)
{
if (!($item instanceof EncryptedItemDecorator)) {
$item = new EncryptedItemDecorator($item, $this->key);
}

return $this->cachePool->save($item);
}

/**
* {@inheritdoc}
*/
public function saveDeferred(CacheItemInterface $item)
{
if (!($item instanceof EncryptedItemDecorator)) {
$item = new EncryptedItemDecorator($item, $this->key);
}

return $this->cachePool->saveDeferred($item);
}

/**
* {@inheritdoc}
*/
public function commit()
{
return $this->cachePool->commit();
}
}
179 changes: 179 additions & 0 deletions src/Encryption/EncryptedItemDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

/*
* This file is part of php-cache organization.
*
* (c) 2015-2016 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/


namespace Cache\Encryption;

use Cache\Adapter\Common\HasExpirationDateInterface;
use Cache\Taggable\TaggableItemInterface;
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;
use Psr\Cache\CacheItemInterface;

/**
* Encrypt and Decrypt all the stored items.
*
* @author Daniel Bannert <d.bannert@anolilab.de>
*/
class EncryptedItemDecorator implements CacheItemInterface, HasExpirationDateInterface, TaggableItemInterface
{
/**
* @type CacheItemInterface
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC these should all be var. I think type was removed from that spec

Copy link
Copy Markdown
Contributor Author

@prisis prisis Nov 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can fix it with styleci, we use it in all packages @type

*/
private $cacheItem;

/**
* @type Key
*/
private $key;

/**
* @param CacheItemInterface $cacheItem
* @param Key $key
*/
public function __construct(CacheItemInterface $cacheItem, Key $key)
{
$this->cacheItem = $cacheItem;
$this->key = $key;
}

/**
* {@inheritdoc}
*/
public function getKey()
{
return $this->cacheItem->getKey();
}

/**
* {@inheritdoc}
*/
public function set($value)
{
$type = gettype($value);

if ($type === 'object') {
$value = serialize($value);
}

$json = json_encode(['type' => $type, 'value' => $value]);

$this->cacheItem->set(Crypto::encrypt($json, $this->key));

return $this;
}

/**
* {@inheritdoc}
*/
public function get()
{
if (!$this->isHit()) {
return;
}

$item = json_decode(Crypto::decrypt($this->cacheItem->get(), $this->key), true);

return $this->transform($item);
}

/**
* {@inheritdoc}
*/
public function isHit()
{
return $this->cacheItem->isHit();
}

/**
* {@inheritdoc}
*/
public function getExpirationDate()
{
return $this->cacheItem->getExpirationDate();
}

/**
* {@inheritdoc}
*/
public function expiresAt($expiration)
{
$this->cacheItem->expiresAt($expiration);

return $this;
}

/**
* {@inheritdoc}
*/
public function expiresAfter($time)
{
$this->cacheItem->expiresAfter($time);

return $this;
}

/**
* {@inheritdoc}
*/
public function getTags()
{
return $this->cacheItem->getTags();
}

/**
* {@inheritdoc}
*/
public function addTag($tag)
{
$this->cacheItem->addTag($tag);

return $this;
}

/**
* {@inheritdoc}
*/
public function setTags(array $tags)
{
$this->cacheItem->setTags($tags);

return $this;
}

/**
* Creating a copy of the orginal CacheItemInterface object.
*/
public function __clone()
{
$this->cacheItem = clone $this->cacheItem;
}

/**
* Transfrom value back to it orginal type.
*
* @param array $item
*
* @return mixed
*/
private function transform(array $item)
{
if ($item['type'] === 'object') {
return unserialize($item['value']);
}

$value = $item['value'];

settype($value, $item['type']);

return $value;
}
}
Loading