Created
January 24, 2019 17:19
-
-
Save mstaack/6dc91f6e4514bc8794b8acf5a7b400b6 to your computer and use it in GitHub Desktop.
flysystem encryption adapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MStaack\EncryptedAdapter; | |
use League\Flysystem\AdapterDecorator\DecoratorTrait; | |
use League\Flysystem\AdapterInterface; | |
use League\Flysystem\Config; | |
use phpseclib\Crypt\AES; | |
/** | |
* Class EncryptionDecorator | |
*/ | |
class EncryptionDecorator implements AdapterInterface | |
{ | |
use DecoratorTrait; | |
/** | |
* @var AdapterInterface | |
*/ | |
protected $adapter; | |
/** | |
* @var | |
*/ | |
protected $cipher; | |
/** | |
* @param AdapterInterface $adapter | |
*/ | |
public function __construct(AdapterInterface $adapter, $encryptionKey = null) | |
{ | |
$this->adapter = $adapter; | |
$this->cipher = new AES(); | |
$this->cipher->setKey($encryptionKey); | |
} | |
/** | |
* @return AdapterInterface | |
*/ | |
protected function getDecoratedAdapter() | |
{ | |
return $this->adapter; | |
} | |
/** | |
* @param string $path | |
* @param string $contents | |
* @param Config $config | |
* @return array|false | |
*/ | |
public function write($path, $contents, Config $config) | |
{ | |
$contents = base64_encode($this->cipher->encrypt($contents)); | |
return $this->getDecoratedAdapter()->write($path, $contents, $config); | |
} | |
/** | |
* @param string $path | |
* @param string $contents | |
* @param Config $config | |
* @return array|false | |
*/ | |
public function update($path, $contents, Config $config) | |
{ | |
$contents = base64_encode($this->cipher->encrypt($contents)); | |
return $this->getDecoratedAdapter()->update($path, $contents, $config); | |
} | |
/** | |
* @param string $path | |
* @return array|false | |
*/ | |
public function read($path) | |
{ | |
$response = $this->getDecoratedAdapter()->read($path); | |
$response['contents'] = $this->cipher->decrypt(base64_decode($response['contents'])); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment