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