2016-01-15 05:14:39 +05:30
|
|
|
<?php
|
|
|
|
/**
|
2018-06-04 19:02:02 +05:30
|
|
|
* encrypt/decrypt with encryptionKey
|
2017-10-23 20:56:10 +05:30
|
|
|
*
|
2016-01-15 05:14:39 +05:30
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
|
|
|
* @copyright Copyright (c) Alex Bilbie
|
|
|
|
* @license http://mit-license.org/
|
2017-10-23 20:56:10 +05:30
|
|
|
*
|
2016-01-15 05:14:39 +05:30
|
|
|
* @link https://github.com/thephpleague/oauth2-server
|
|
|
|
*/
|
2016-07-09 04:30:44 +05:30
|
|
|
|
2016-03-17 20:07:21 +05:30
|
|
|
namespace League\OAuth2\Server;
|
2016-01-15 05:14:39 +05:30
|
|
|
|
2017-07-01 20:27:40 +05:30
|
|
|
use Defuse\Crypto\Crypto;
|
2017-11-20 12:12:09 +05:30
|
|
|
use Defuse\Crypto\Key;
|
2017-07-01 20:27:40 +05:30
|
|
|
|
2016-03-17 20:07:21 +05:30
|
|
|
trait CryptTrait
|
2016-01-15 05:14:39 +05:30
|
|
|
{
|
2017-07-01 20:27:40 +05:30
|
|
|
/**
|
2017-11-20 12:12:09 +05:30
|
|
|
* @var string|Key
|
2017-07-01 20:27:40 +05:30
|
|
|
*/
|
|
|
|
protected $encryptionKey;
|
|
|
|
|
2016-01-15 05:14:39 +05:30
|
|
|
/**
|
2018-06-04 19:02:02 +05:30
|
|
|
* Encrypt data with encryptionKey.
|
2016-01-15 05:14:39 +05:30
|
|
|
*
|
|
|
|
* @param string $unencryptedData
|
|
|
|
*
|
2016-07-09 04:30:44 +05:30
|
|
|
* @throws \LogicException
|
2017-10-23 20:56:10 +05:30
|
|
|
*
|
2016-01-15 05:14:39 +05:30
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-17 20:07:21 +05:30
|
|
|
protected function encrypt($unencryptedData)
|
2016-01-15 05:14:39 +05:30
|
|
|
{
|
2017-07-01 22:37:42 +05:30
|
|
|
try {
|
2018-03-01 01:31:01 +05:30
|
|
|
if ($this->encryptionKey instanceof Key) {
|
2017-11-20 12:12:09 +05:30
|
|
|
return Crypto::encrypt($unencryptedData, $this->encryptionKey);
|
|
|
|
}
|
2018-03-01 02:03:19 +05:30
|
|
|
|
|
|
|
return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
|
2017-07-01 22:37:42 +05:30
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \LogicException($e->getMessage());
|
2017-07-01 20:27:40 +05:30
|
|
|
}
|
2016-01-15 05:14:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-04 19:02:02 +05:30
|
|
|
* Decrypt data with encryptionKey.
|
2016-01-15 05:14:39 +05:30
|
|
|
*
|
|
|
|
* @param string $encryptedData
|
|
|
|
*
|
2016-02-12 15:30:41 +05:30
|
|
|
* @throws \LogicException
|
2017-10-23 20:56:10 +05:30
|
|
|
*
|
2016-01-15 05:14:39 +05:30
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-17 20:07:21 +05:30
|
|
|
protected function decrypt($encryptedData)
|
2016-01-15 05:14:39 +05:30
|
|
|
{
|
2017-07-01 22:37:42 +05:30
|
|
|
try {
|
2018-03-01 01:31:01 +05:30
|
|
|
if ($this->encryptionKey instanceof Key) {
|
2017-11-20 12:12:09 +05:30
|
|
|
return Crypto::decrypt($encryptedData, $this->encryptionKey);
|
|
|
|
}
|
2018-03-01 02:03:19 +05:30
|
|
|
|
|
|
|
return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
|
2017-07-01 22:37:42 +05:30
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \LogicException($e->getMessage());
|
2017-07-01 20:27:40 +05:30
|
|
|
}
|
2016-01-15 05:14:39 +05:30
|
|
|
}
|
2017-07-01 20:27:40 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the encryption key
|
|
|
|
*
|
2017-11-20 12:12:09 +05:30
|
|
|
* @param string|Key $key
|
2017-07-01 20:27:40 +05:30
|
|
|
*/
|
|
|
|
public function setEncryptionKey($key = null)
|
|
|
|
{
|
|
|
|
$this->encryptionKey = $key;
|
|
|
|
}
|
2016-03-17 20:07:48 +05:30
|
|
|
}
|