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

CryptTrait::decrypt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
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