oauth2-server/src/CryptTrait.php

78 lines
1.7 KiB
PHP
Raw Normal View History

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
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;
2016-03-17 20:07:21 +05:30
trait CryptTrait
2016-01-15 05:14:39 +05:30
{
/**
* @var string|Key
*/
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
{
try {
if ($this->encryptionKey instanceof Key) {
return Crypto::encrypt($unencryptedData, $this->encryptionKey);
}
2018-03-01 02:03:19 +05:30
return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
} catch (\Exception $e) {
throw new \LogicException($e->getMessage());
}
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
{
try {
if ($this->encryptionKey instanceof Key) {
return Crypto::decrypt($encryptedData, $this->encryptionKey);
}
2018-03-01 02:03:19 +05:30
return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
} catch (\Exception $e) {
throw new \LogicException($e->getMessage());
}
2016-01-15 05:14:39 +05:30
}
/**
* Set the encryption key
*
* @param string|Key $key
*/
public function setEncryptionKey($key = null)
{
$this->encryptionKey = $key;
}
2016-03-17 20:07:48 +05:30
}