Completed
Push — master ( 7dba56...6875ae )
by Oscar
10:21
created

CryptTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 7 1
A encrypt() 0 8 2
A decrypt() 0 8 2
1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
use phpseclib\Crypt\AES;
6
7
/**
8
 * Trait used by all middlewares that needs encrypt/decrypt functions.
9
 */
10
trait CryptTrait
11
{
12
    private $cipher;
13
14
    /**
15
     * Set the key.
16
     * 
17
     * @param string $key
18
     *
19
     * @return self
20
     */
21
    public function key($key)
22
    {
23
        $this->cipher = new AES();
24
        $this->cipher->setKey($key);
25
26
        return $this;
27
    }
28
29
    /**
30
     * Encrypt the given value.
31
     *
32
     * @param string $value
33
     * 
34
     * @return string
35
     */
36
    private function encrypt($value)
37
    {
38
        if ($this->cipher) {
39
            return bin2hex($this->cipher->encrypt($value));
40
        }
41
42
        return $value;
43
    }
44
45
    /**
46
     * Decrypt the given value.
47
     *
48
     * @param string $value
49
     * 
50
     * @return string
51
     */
52
    private function decrypt($value)
53
    {
54
        if ($this->cipher) {
55
            return $this->cipher->decrypt(hex2bin($value));
56
        }
57
58
        return $value;
59
    }
60
}
61