EncryptingDecorator::hmac()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
namespace Jsq\Cache;
3
4
use Doctrine\Common\Cache\Cache;
5
6
abstract class EncryptingDecorator implements Cache
7
{
8
    use EncryptionStatsTrait;
9
10
    /** @var Cache */
11
    protected $decorated;
12
13
    /**
14 177
     * @param Cache $decorated
15
     */
16 177
    public function __construct(Cache $decorated)
17 177
    {
18
        $this->decorated = $decorated;
19
    }
20
21
    /**
22 78
     * {@inheritdoc}
23
     */
24 78
    public function fetch($id)
25 78
    {
26 36
        $stored = $this->decorated->fetch($id);
27
        if ($this->isDataDecryptable($stored, $id)) {
28
            return $this->returnHit(
29 42
                $this->callAndTime([$this, 'decrypt'], [$stored])
30
            );
31
        }
32
33
        return $this->returnMiss(false);
34
    }
35 72
36
    /**
37 72
     * {@inheritdoc}
38 72
     */
39
    public function save($id, $data, $ttl = 0)
40
    {
41
        return $this->decorated->save(
42
            $id,
43
            $this->callAndTime([$this, 'encrypt'], [$data, $id]),
44 42
            $ttl
45
        );
46 42
    }
47 36
48
    /**
49
     * {@inheritdoc}
50 6
     */
51
    public function contains($id)
52
    {
53
        if ($stored = $this->decorated->fetch($id)) {
54
            return $this->isDataDecryptable($stored, $id);
55
        }
56 6
57
        return false;
58 6
    }
59 6
60 3
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getStats()
64
    {
65 6
        return $this->getEncryptionStats($this->decorated->getStats() ?: []);
66
    }
67 6
68 6
    /**
69
     * {@inheritdoc}
70
     */
71 36
    public function delete($id)
72
    {
73 36
        return $this->decorated
74
            ->delete($id);
75
    }
76
77
    protected function hmac(string $encrypted, string $id): string
78
    {
79
        return hash_hmac('sha256', $encrypted, $id);
80
    }
81
82 72
    protected function generateIv(string $method): string
83
    {
84 72
        return random_bytes(openssl_cipher_iv_length($method));
85 72
    }
86 72
87
    protected function encipher(
88
        string $string,
89 75
        string $method,
90
        string $key,
91 75
        string $iv
92
    ): string {
93
        return openssl_encrypt($string, $method, $key, 0, $iv);
94 36
    }
95
96 36
    protected function decipher(
97
        string $string,
98
        string $method,
99
        string $key,
100
        string $iv
101
    ): string {
102
        return openssl_decrypt($string, $method, $key, 0, $iv);
103
    }
104
105
    abstract protected function encrypt($data, string $id): EncryptedValue;
106
107
    abstract protected function decrypt($data);
108
109
    abstract protected function isDataDecryptable($data, string $id): bool;
110
}
111