Completed
Push — master ( 06424f...aac467 )
by Alex
86:38 queued 51:34
created

CryptTrait::encrypt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Public/private key encryption.
4
 * @author      Alex Bilbie <[email protected]>
5
 * @copyright   Copyright (c) Alex Bilbie
6
 * @license     http://mit-license.org/
7
 * @link        https://github.com/thephpleague/oauth2-server
8
 */
9
10
namespace League\OAuth2\Server;
11
12
use Defuse\Crypto\Crypto;
13
14
trait CryptTrait
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $encryptionKey;
20
21
    /**
22
     * Encrypt data with a private key.
23
     *
24
     * @param string $unencryptedData
25
     *
26
     * @throws \LogicException
27
     * @return string
28
     */
29
    protected function encrypt($unencryptedData)
30
    {
31
        try {
32
            return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
33
        } catch (\Exception $e) {
34
            throw new \LogicException($e->getMessage());
35
        }
36
    }
37
38
    /**
39
     * Decrypt data with a public key.
40
     *
41
     * @param string $encryptedData
42
     *
43
     * @throws \LogicException
44
     * @return string
45
     */
46
    protected function decrypt($encryptedData)
47
    {
48
        try {
49
            return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
50
        } catch (\Exception $e) {
51
            throw new \LogicException($e->getMessage());
52
        }
53
    }
54
55
    /**
56
     * Set the encryption key
57
     *
58
     * @param string $key
59
     */
60
    public function setEncryptionKey($key = null)
61
    {
62
        $this->encryptionKey = $key;
63
    }
64
}
65