mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-22 21:19:46 +05:30
78 lines
1.7 KiB
PHP
78 lines
1.7 KiB
PHP
<?php
|
|
/**
|
|
* Public/private key encryption.
|
|
*
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
|
* @copyright Copyright (c) Alex Bilbie
|
|
* @license http://mit-license.org/
|
|
*
|
|
* @link https://github.com/thephpleague/oauth2-server
|
|
*/
|
|
|
|
namespace League\OAuth2\Server;
|
|
|
|
use Defuse\Crypto\Crypto;
|
|
use Defuse\Crypto\Key;
|
|
|
|
trait CryptTrait
|
|
{
|
|
/**
|
|
* @var string|Key
|
|
*/
|
|
protected $encryptionKey;
|
|
|
|
/**
|
|
* Encrypt data with a private key.
|
|
*
|
|
* @param string $unencryptedData
|
|
*
|
|
* @throws \LogicException
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function encrypt($unencryptedData)
|
|
{
|
|
try {
|
|
if ($this->encryptionKey instanceof Key) {
|
|
return Crypto::encrypt($unencryptedData, $this->encryptionKey);
|
|
}
|
|
|
|
return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
|
|
} catch (\Exception $e) {
|
|
throw new \LogicException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Decrypt data with a public key.
|
|
*
|
|
* @param string $encryptedData
|
|
*
|
|
* @throws \LogicException
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function decrypt($encryptedData)
|
|
{
|
|
try {
|
|
if ($this->encryptionKey instanceof Key) {
|
|
return Crypto::decrypt($encryptedData, $this->encryptionKey);
|
|
}
|
|
|
|
return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
|
|
} catch (\Exception $e) {
|
|
throw new \LogicException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the encryption key
|
|
*
|
|
* @param string|Key $key
|
|
*/
|
|
public function setEncryptionKey($key = null)
|
|
{
|
|
$this->encryptionKey = $key;
|
|
}
|
|
}
|