oauth2-server/src/CryptTrait.php

69 lines
1.4 KiB
PHP
Raw Normal View History

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
use Defuse\Crypto\Crypto;
2016-03-17 14:37:21 +00:00
trait CryptTrait
2016-01-14 23:44:39 +00:00
{
/**
* @var string
*/
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
{
try {
return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
} catch (\Exception $e) {
throw new \LogicException($e->getMessage());
}
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
{
try {
return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
} catch (\Exception $e) {
throw new \LogicException($e->getMessage());
}
2016-01-14 23:44:39 +00:00
}
/**
* Set the encryption key
*
* @param string $key
*/
public function setEncryptionKey($key = null)
{
$this->encryptionKey = $key;
}
2016-03-17 10:37:48 -04:00
}